0
  1. 我正在尝试创建一个运行类似于此视频中的动画的程序,但我无法添加更多方块。我试图将所有方块添加到数组列表中,但我无法弄清楚它的去向。

  2. 到目前为止,这是我的代码:

    public class Animation extends JFrame{
    
    CrazySquares square = new CrazySquares();
    
    Animation(){
    
    add(new CrazySquares());
    }
    
    public static void main (String[] args){
    
    Animation frame = new  Animation();
    frame.setTitle("AnimationDemo");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(250, 250);
    frame.setVisible(true);
    }
    }
    
    class CrazySquares extends JPanel{
    
    
    private final int numberOfRectangles=100;
    
        Color color=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
    
         private int x=1;
           private int y=1;
        private Timer timer = new Timer(30, new TimerListener());
       Random random= new Random();
            int randomNumber=1+(random.nextInt(4)-2);
    
     Random rand= new Random();
     int rando=1+(rand.nextInt(4)-2);          
    
    
       CrazySquares(){
            timer.start();
    
       }
    
     protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    
     int width=getWidth();
     int height=getHeight();
    
    
              g.setColor(color);
    g.fillRect(x+width/2,y+(int)(height*.47), 20, 20);
    
    
     }
    
    
      class TimerListener implements ActionListener {
         @Override 
         public void actionPerformed(ActionEvent e) {
    
        x += rando;
        y+= randomNumber;
           repaint();
    
         }
    
    
    }
    
    }
    
4

2 回答 2

1

你有代码来绘制一个矩形,在这里:

int width=getWidth();
int height=getHeight();

g.setColor(color);
g.fillRect(x+width/2,y+(int)(height*.47), 20, 20);

现在我建议您将这些值移植到一个Square对象中。或者,更好的是,使用Rectangle对象。如果您使用自定义方法:

public class Square
{
     public Square(int x, int y, int height, int width)
     {
         // Store these values in some fields.
     }

     public void paintComponent(Graphics g)
     {
         g.fillRect() // Your code for painting out squares. 
     }
}

然后,您需要做的就是paintComponent在某个列表中调用每个对象的方法。假设您有一些列表:

List<Square> squares = new ArrayList<Square>();


for(Square sq : squares)
{
    sq.paintComponent(g);
}
于 2013-05-11T09:22:51.367 回答
0

这是到目前为止的代码。我知道这很讨厌,但这是因为我一直在尝试不同的东西,但它们都不起作用。我真的很感谢你的帮助。谢谢。@ChrisCooney

 public class SquaresAnimation extends JFrame{
  SquaresAnimation(){
      add(new CrazySquares());



}

public static void main (String[] args){
 SquaresAnimation frame = new  SquaresAnimation();
frame.setTitle("AnimationDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 250);
frame.setVisible(true);
 }
 }

 class Square{
 private int x;
 private int y;
 public int height;
 public int width;




  Square(int x, int y, int height, int width)
 {
     // Store these values in some fields.
     this.x=x;
     this.y=y;
     this.height=height;
     this.width=width;
 }

 public void paintComponent(Graphics g)
 {
    g.setColor(Color.CYAN);
    g.fillRect(x+width/2,y+(int)(height*.47), 20, 20); 
 }
}

class CrazySquares extends JPanel {



 private final int numberOfRectangles=100;

 Color color=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));

  private int x=1;
    private int y=1;
 Random random= new Random();
     int randomNumber=1+(random.nextInt(4)-2);

     Random rand= new Random();
     int rando=1+(rand.nextInt(4)-2);

     private Timer timer = new Timer(30, new TimerListener());

     List<Square> squares = new ArrayList<Square>();


      CrazySquares(Graphics g){
    timer.start();

  for(Square sq : squares){

sq.paintComponent(g);
}


    }

}

//protected void paintComponent(Graphics g) {
//super.paintComponent(g);      

 //         }

 class TimerListener implements ActionListener {
  @Override 
  public void actionPerformed(ActionEvent e) {


    repaint();

  }

    }
于 2013-05-15T08:23:41.547 回答