每秒填充一个椭圆形

时间:2017-08-07 17:06:05

标签: java swing

我有8个椭圆都设置为相同的颜色。一秒后我想要第一个椭圆改变颜色,然后又一秒后我希望第一个椭圆回到原来的颜色,然后改变第二个椭圆颜色。我已经画了圈子,我已经尝试过实施一个帖子,但我认为它没有执行......

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Circle extends JPanel implements Runnable
{

  Graphics g;

  Thread t = new Thread();

  int[][] fillCircles = new int[8][4];


@Override
public void paintComponent(Graphics g)
{
    this.g = g;
    super.paintComponent(this.g);

    this.g.setColor(new java.awt.Color(237, 54, 26));

    this.g.drawOval(300, 50, 100, 100);
    this.g.drawOval(450, 125, 100, 100);
    this.g.drawOval(500, 250, 100, 100);
    this.g.drawOval(425, 375, 100, 100);
    this.g.drawOval(300, 400, 100, 100);
    this.g.drawOval(175, 350, 100, 100);
    this.g.drawOval(125, 225, 100, 100);
    this.g.drawOval(175, 100, 100, 100);

    this.g.fillOval(300, 50, 100, 100);
    this.g.fillOval(450, 125, 100, 100);
    this.g.fillOval(500, 250, 100, 100);
    this.g.fillOval(425, 375, 100, 100);
    this.g.fillOval(300, 400, 100, 100);
    this.g.fillOval(175, 350, 100, 100);
    this.g.fillOval(125, 225, 100, 100);
    this.g.fillOval(175, 100, 100, 100);

    fillCircles[0][0] = 300;
    fillCircles[0][1] = 50;
    fillCircles[0][2] = 100;
    fillCircles[0][3] = 100;

    fillCircles[1][0] = 450;
    fillCircles[1][1] = 125;
    fillCircles[1][2] = 100;
    fillCircles[1][3] = 100;

    fillCircles[2][0] = 500;
    fillCircles[2][1] = 250;
    fillCircles[2][2] = 100;
    fillCircles[2][3] = 100;

    fillCircles[3][0] = 425;
    fillCircles[3][1] = 375;
    fillCircles[3][2] = 100;
    fillCircles[3][3] = 100;

    fillCircles[4][0] = 300;
    fillCircles[4][1] = 400;
    fillCircles[4][2] = 100;
    fillCircles[4][3] = 100;

    fillCircles[5][0] = 175;
    fillCircles[5][1] = 350;
    fillCircles[5][2] = 100;
    fillCircles[5][3] = 100;

    fillCircles[6][0] = 125;
    fillCircles[6][1] = 225;
    fillCircles[6][2] = 100;
    fillCircles[6][3] = 100;

    fillCircles[7][0] = 175;
    fillCircles[7][1] = 100;
    fillCircles[7][2] = 100;
    fillCircles[7][3] = 100;


}

Circle () {
    t.start();
}


public void run () {

    int circle = 0;

    try {
        for (;;) {

            Thread.sleep(1000);

            if (circle > 0) {
                this.g.setColor(new java.awt.Color(237, 54, 26));
                circle--;
                this.g.fillOval(fillCircles[circle][0], fillCircles[circle][1], fillCircles[circle][2], fillCircles[circle][2]);
                circle++;
            }
            this.g.setColor(Color.red);
            this.g.fillOval(fillCircles[circle][0], fillCircles[circle][1], fillCircles[circle][2], fillCircles[circle][2]);


            circle++;

            if (circle == 8) {
                circle = 0;
            }

        }
    } catch (InterruptedException e) {
        System.out.println ("Thread Interrupted");
    }

}


public static void main(String[] args) {
    Circle c;

    JFrame application = new JFrame();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(c=new Circle());
    application.setSize(1200, 900);
    application.setVisible(true); 


}

}

2 个答案:

答案 0 :(得分:2)

你只是在没有给Thread的情况下实例化Runnable,所以它没有运行你想要的东西。

Thread t = new Thread();

应该是

Thread t = new Thread(this);

这是因为CircleRunnable,而run()方法就在其中。 关于换色,我会给你一个想法。 绘制圆圈时,请检查当前圆圈编号/索引与另一个每秒更改colorChangeIndex

的变量
if(circleIndex == colorChangeIndex){
  // another color
else
 //the default color
colorChangeIndex++; (modulo number of cricles to reset it)

答案 1 :(得分:1)

您的代码的基本结构是错误的。绘画应该只在paintComponent()方法中完成。你永远不应该在你的线程中引用Graphics对象。

因此,您可能想要做的是保留一个“圆”对象的ArrayList。此自定义对象将包含要绘制的圆及其颜色。然后在paintComponent()方法中,您只需遍历ArrayList并绘制ArrayList中的所有圆。查看Custom Painting Approches中的DrawOnComponent示例。

  

一秒后,我想要第一个椭圆改变颜色,然后又一秒钟后,我希望第一个椭圆回到原来的颜色,然后改变第二个椭圆颜色。

所以现在你需要使用Swing Timer为这幅画设置动画。当计时器触发时,您重置相应圆圈的颜色,然后重新绘制面板。