【问题标题】:adding squares to an animation在动画中添加方块
【发布时间】:2013-05-11 09:15:42
【问题描述】:
  1. 我正在尝试创建一个程序来运行类似于this video 上的动画,但我无法添加更多方块。我试图将所有方块添加到数组列表中,但我无法弄清楚它的去向。

  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();
    
         }
    
    
    }
    
    }
    

【问题讨论】:

  • 1) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。 2) 你已经描述了一个问题,但到目前为止还没有提出任何问题(更不用说一个具体的、可回答的问题了)。你的问题是什么
  • 看看thisthisthis,如果你真的很勇敢,this 这几乎展示了你想要实现的基本理念......
  • 谢谢各位。我需要知道如何添加多个正方形

标签: java swing animation awt


【解决方案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);
}

【讨论】:

  • 我应该把列表放在哪里?
  • 在我想象的CrazySquares 类中。
【解决方案2】:

这是目前为止的代码。我知道这很讨厌,但这是因为我一直在尝试不同的东西,但它们都不起作用。我真的很感谢你的帮助。谢谢。 @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();

  }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多