2 回答

TA貢獻1784條經(jīng)驗 獲得超8個贊
繪畫方法僅用于繪畫。它不應(yīng)該:
創(chuàng)建對象。也就是說,不要創(chuàng)建 Circle 對象。
停止定時器。
繪制代碼將簡單地遍歷 ArrayList 來繪制所有 Circlce 對象。
Timer 的 ActionListener 用于:
創(chuàng)建 Cirlce 對象并將它們一次一個地添加到 ArrayList 中。
確定每個對象的顏色
創(chuàng)建所需數(shù)量的對象后停止計時器。
所以基本上在你的 Board 類中你需要一個像addCircle(....)
.?這會將 Circle 對象添加到 ArrayList,然后repaint()
對其自身進行調(diào)用。然后,您更改ActionListener
代碼以創(chuàng)建 Circle 對象并調(diào)用該addCirle(...)
方法。
我想問題是顏色并不與每個對象相關(guān)聯(lián)
正確的。您將需要一個自定義對象來進行繪畫。這個自定義對象將包含兩個屬性:
圓對象
Circle 對象的顏色。
或者另一種選擇是直接繪制 BufferedImage,然后在面板中顯示 BufferedImage。

TA貢獻1875條經(jīng)驗 獲得超5個贊
我得到了相同的結(jié)果,但有一個例外,代碼如下:
public class Board extends JPanel {
? ? private static final long serialVersionUID = 6676924870723702476L;
? ? private Timer timer;
? ? private final int DELAY = 30;
? ? private ActionListener drawLine;
? ? private RCircle circ;
? ? private ArrayList<RCircle> circles = new ArrayList<>();
? ? private double circDiam = 80;
? ? private double circX = 0;
? ? private double circY = 0;
? ? private short circNum = 1;
? ? private double height = 800;
? ? private double width = 800;
? ? public Board() {
? ? ? ? drawLine = (ActionEvent e) -> {
? ? ? ? ? ? if(circX >= width - circDiam)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? circY += circDiam;
? ? ? ? ? ? ? ? circX = 0;
? ? ? ? ? ? ? ? circDiam *= 0.865? ?;
? ? ? ? ? ? }
? ? ? ? ? ? if(circY >= height - circDiam) {
? ? ? ? ? ? ? ? timer.stop();
? ? ? ? ? ? }
? ? ? ? ? ? circ = new RCircle(circX, circY, circDiam, circNum);
? ? ? ? ? ? circNum++;
? ? ? ? ? ? addCircle();
? ? ? ? ? ? circX += circDiam;
? ? ? ? ? ? if(circNum == 5)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? circNum = 1;? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? timer = new Timer(DELAY, drawLine);
? ? ? ? timer.start();
? ? }
? ? private void addCircle() {
? ? ? ? circles.add(circ);
? ? ? ? repaint();
? ? }
? ? protected void paintComponent(Graphics g) {
? ? ? ? super.paintComponent(g);
? ? ? ? Graphics2D g2d = (Graphics2D) g;
? ? ? ? RenderingHints rHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
? ? ? ? rHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
? ? ? ? g2d.setRenderingHints(rHints);
? ? ? ? g2d.setColor(circ.getColor());
? ? ? ? for(RCircle circ : circles) {
? ? ? ? ? ? g2d.fill(circ.getCirc());
? ? ? ? }
? ? }
}
public class RCircle {
? ? private Ellipse2D circ;
? ? private Color color;
? ? public RCircle(double x, double y, double circDiam, short circNum) {
? ? ? ? ?this.circ = new Ellipse2D.Double(x, y, circDiam, circDiam);
? ? ? ? ?this.color = getCircColor(circNum);
? ? }
? ? private Color getCircColor(short circNum) {?
? ? ? ? switch(circNum) {
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? return Color.blue;
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? return Color.red;
? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? return Color.yellow;
? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? return Color.green;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? return Color.gray;
? ? ? ? }
? ? }
? ? public Ellipse2D getCirc() {
? ? ? ? return circ;
? ? }
? ? public void setCirc(Ellipse2D circ) {
? ? ? ? this.circ = circ;
? ? }
? ? public Color getColor() {
? ? ? ? return color;
? ? }
? ? public void setColor(Color color) {
? ? ? ? this.color = color;
? ? }
}
public class Execute extends JFrame {
? ? private static final long serialVersionUID = -3483390877436935589L;
? ? public Execute() {
? ? ? ? add(new Board());
? ? ? ? setSize(800, 800);
? ? ? ? setResizable(false);
? ? ? ? setLocationRelativeTo(null);
? ? ? ? setDefaultCloseOperation(EXIT_ON_CLOSE);
? ? }
? ? public static void main(String[] args) {
? ? ? ? EventQueue.invokeLater(() -> {
? ? ? ? ? ? Execute ex = new Execute();
? ? ? ? ? ? ex.setVisible(true);
? ? ? ? });
? ? }
}
添加回答
舉報