2013-05-11 57 views
0
  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(); 
    
        } 
    
    
    } 
    
    } 
    
+0

1)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 2)你描述了一個問題,但至今沒有問過問題(更不用說具體的可回答的問題)。你的問題是什麼? – 2013-05-11 09:18:17

+0

看看[this](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788),[this](http://stackoverflow.com/questions/14593678/multiple- bouncing-balls-thread-issue/14593761#14593761)和[this](http://stackoverflow.com/questions/12642852/the-images-are-not-loading/12648265#12648265),如果你真的感覺到勇敢的,[這](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184)這顯示了你正在努力實現的基本想法... – MadProgrammer 2013-05-11 09:20:49

+0

thaks guys 。 我需要知道如何添加多個方塊 – 2013-05-11 23:43:29

回答

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); 
} 
+0

我應該在哪裏放列表? – 2013-05-11 23:32:51

+0

在我想象的CrazySquares類中。 – christopher 2013-05-12 05:55:13

+0

nope。我試過了,什麼都沒發生 – 2013-05-13 03:41:07

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

    } 

    }