2013-05-04 15:37:57Morris
[JAVA][作業] 視窗繪圖
印出如上圖的圓,附加讓它自動旋轉。
package ce1002.E5.s100502205;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
public class E5 extends JFrame {
public E5() {
this.setTitle("E5");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(450, 450);
this.add(new ArcsPanel());
this.setVisible(true);
}
public static void main(String[] args) {
new E5();
}
}
class ArcsPanel extends JPanel {
Color[] color = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
Color.MAGENTA, Color.RED, Color.YELLOW, Color.PINK };
int theta = 0;
public ArcsPanel() {
Timer timer = new Timer();
timer.schedule(new RunningArcs(), 1000, 50);
}
class RunningArcs extends TimerTask {
public void run() {
theta = (theta + 1) % 360;
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int) (Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
for (Color vv : color) {
g.setColor(vv);
g.fillArc(x, y, 2 * radius, 2 * radius, theta, 45);
theta += 45;
}
}
}
上一篇:[JAVA][作業] 逃離迷宮