太极图
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();
}
}
}