I'm trying to create a program that runs an animation similar to the one on this video but I'm having trouble adding more squares. I tried to add all the squares to an array list but I couldn't figure out where it goes.
so far this is my code:
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(); } } }
Asked
Active
Viewed 441 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

Rui Monroy
- 1
- 1
- 1
-
1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) You've described a problem, but have so far not asked a question (let alone a specific, answerable question). What *is* your question? – Andrew Thompson May 11 '13 at 09:18
-
Take a look at [this](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788), [this](http://stackoverflow.com/questions/14593678/multiple-bouncing-balls-thread-issue/14593761#14593761) and [this](http://stackoverflow.com/questions/12642852/the-images-are-not-loading/12648265#12648265) and if you're feeling really brave, [this](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184) which demonstrates pretty much the basic idea you are trying to achieve... – MadProgrammer May 11 '13 at 09:20
-
thaks guys. I need to know how to add multiple squares – Rui Monroy May 11 '13 at 23:43
2 Answers
1
You've got code to paint out one rectangle, here:
int width=getWidth();
int height=getHeight();
g.setColor(color);
g.fillRect(x+width/2,y+(int)(height*.47), 20, 20);
Now what I would recommend, would be that you port these values into a Square
object. Or, better yet, use the Rectangle
object. If you went with the custom approach:
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.
}
}
Then, all you need to do, is call each object's paintComponent
method in some list. Let's assume you have some List:
List<Square> squares = new ArrayList<Square>();
for(Square sq : squares)
{
sq.paintComponent(g);
}

christopher
- 26,815
- 5
- 55
- 89
0
this is the code so far. I know its nasty but its because i've been trying different things a none of them work. i really appreciate your help. thanks. @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();
}
}

Rui Monroy
- 1
- 1
- 1