板级:
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class Board extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
super.setBackground(Color.BLACK);
drawDonut(g, Color.WHITE);
}
private void drawDonut(Graphics g, Color c) {
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh
= new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
Dimension size = getSize();
double w = size.getWidth();
double h = size.getHeight();
Ellipse2D e = new Ellipse2D.Double(0, 0, 160, 260);
g2d.setStroke(new BasicStroke(3));
//RainbowColors rb = new RainbowColors(6);
for (double deg = 0; deg < 360; deg += 5) {
AffineTransform at
= AffineTransform.getTranslateInstance(w/2, h/2);
at.rotate(Math.toRadians(deg));
g2d.setColor(Color.RED/*rb.getNewColor()*/);
g2d.draw(at.createTransformedShape(e));
}
}
}
当我启动它时,我在屏幕上首先出现白色圆圈,其余为紫色。我做了一些调试,并在控制台程序中从类“RainbowColors”的构造函数中写入了两次消息
但是对象只生成一次。