Java绘制图像旋转

太极图

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class DemoMain extends JFrame {

    public DemoMain() {
        DemoPanel demoPanel = new DemoPanel();
        new Thread(demoPanel).start();
        this.setVisible(true);
        this.setResizable(false);
        this.setTitle("旋转太极");
        this.add(demoPanel);
        this.setSize(Global.SIZE, Global.SIZE);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



}

class DemoPanel extends JPanel implements Runnable {

    int i = 0;// 图片旋转的度数

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHints(renderingHints);

        // 填充背景颜色
        g2d.setColor(Color.GRAY);
        g2d.fillRect(0, 0, Global.SIZE, Global.SIZE);

        // 创建一张缓存图片
        BufferedImage image = new BufferedImage(Global.SIZE, Global.SIZE, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();

        // 填充背景颜色
        graphics.setColor(Color.GRAY);
        graphics.fillRect(0, 0, Global.SIZE, Global.SIZE);

        // 第一步:画二个大半圆
        graphics.setColor(Color.WHITE);
        graphics.fillArc(Global.X, Global.Y, Global.R, Global.R, 0, 180);
        graphics.setColor(Color.BLACK);
        graphics.fillArc(Global.X, Global.Y, Global.R, Global.R, 0, -180);

        // 第二步:画二个小半圆
        graphics.setColor(Color.WHITE);
        graphics.fillArc(Global.X + Global.R / 2, Global.Y + Global.R / 4, Global.R / 2, Global.R / 2, 0, -180);
        graphics.setColor(Color.BLACK);
        graphics.fillArc(Global.X, Global.Y + Global.R / 4, Global.R / 2, Global.R / 2, 0, 180);

        // 第三步:画二个小圆
        graphics.setColor(Color.WHITE);
        graphics.fillArc(Global.X + 3 * Global.R / 16, Global.Y + 7 * Global.R / 16, Global.R / 8, Global.R / 8, 0, 360);
        graphics.setColor(Color.BLACK);
        graphics.fillArc(Global.X + 11 * Global.R / 16, Global.Y + 7 * Global.R / 16, Global.R / 8, Global.R / 8, 0, 360);

        i += 2;
        if (i >= 360) {
            i = 0;
        }
        double theta = i * Math.PI / 180;
        g2d.rotate(theta, Global.SIZE / 2 - 8, Global.SIZE / 2 - 18);
        g2d.drawImage(image, 0, 0, Global.SIZE, Global.SIZE, null);

    }

    @Override
    public void run() {
        while (true) {
            repaint();
        }
    }

}

class Global {

    public static final int SIZE = 500;// 窗口的大小
    public static final int R = SIZE - 100;// 图形的大小
    public static final int X = (SIZE - R) / 2 - 8;// 图形初始位置的x坐标
    public static final int Y = (SIZE - R) / 2 - 18;// 图形初始位置的y坐标

}

图片旋转

import javax.swing.*;
import java.awt.*;

public class Demo_Photo_rotate extends JFrame{
    public Demo_Photo_rotate(){

        this.setResizable(false);
        this.setTitle("旋转");
        RotatePanel rp=new RotatePanel("images/1.jpg");
        new Thread(rp).start();
        this.add(rp);
        this.setVisible(true);
        this.setSize(Global.SIZE, Global.SIZE);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



}
class RotatePanel extends JPanel implements Runnable{
    String path;
    int i=0;
    public RotatePanel(String p){
        this.path=p;
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHints(renderingHints);
        i += 2;
        if (i >= 360) {
            i = 0;
        }
        double theta = i * Math.PI / 180;
        g2d.rotate(theta, Global.SIZE / 2 - 8, Global.SIZE / 2 - 18);
        g2d.drawImage(new ImageIcon(this.getClass().getResource(path)).getImage(), 0, 0, Global.SIZE, Global.SIZE, null);
    }





    @Override
    public void run() {
        while (true) {
            repaint();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值