I have this program where when I click start, it starts painting rectangles one on top of the other by storing each rectangle created by paintComponent in an array and then displaying them on the screen.

When I click stop, the animation stops.

This is all fine and dandy but I have been banging my head against the computer to create a way where when I hit start after hitting stop, it would clear the screen and start the animation anew. I am pasting my code below, any help will be greatly appreciated.

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import java.awt.*;

public class TwoButtonsRandomRec {

    JFrame frame;
    Timer timer;

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TwoButtonsRandomRec test = new TwoButtonsRandomRec();

    public void go() {
    frame = new JFrame();

    JButton startButton = new JButton("Start");
    startButton.addActionListener(new StartListener());
    JButton stopButton = new JButton("Stop");
    stopButton.addActionListener(new StopListener());

    final DrawPanel myDraw = new DrawPanel();

    timer = new Timer(50, new ActionListener() {
        public void actionPerformed(ActionEvent ae) {

    frame.add(startButton, BorderLayout.NORTH);
    frame.add(stopButton, BorderLayout.SOUTH);
    frame.add(myDraw, BorderLayout.CENTER);

    class StartListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

    class StopListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

    class DrawPanel extends JPanel {
    ArrayList<MyRectangle> rects = new ArrayList<>();
    Random rand = new Random();

    public void paintComponent(Graphics g) {



        for(MyRectangle r : rects) {
        g.fillRect(r.x, r.y, r.width, r.height);

    public Dimension getPreferredSize() {
        return new Dimension(500,500);

    public void addRect() {
        int ht = rand.nextInt(getHeight());
        int wd = rand.nextInt(getWidth());

        int x = rand.nextInt(getWidth() - wd);
        int y = rand.nextInt(getHeight() - ht);

        int r = rand.nextInt(256);
        int g = rand.nextInt(256);
        int b = rand.nextInt(256);

        rects.add(new MyRectangle(x, y, wd, ht, new Color(r, b, g)));

class MyRectangle extends Rectangle {
    Color color;
    public MyRectangle(int x, int y, int w, int h, Color c) {
    super(x, y, w, h);
    this.color = c;

    public Color getColor() {
    return color;
4 Years
Discussion Span
Last Post by JamesCherrill

I have tried to do that with:


Sure that would work if only I knew how to bind that with my Start button only the second time that I press it.

I am pretty new at this stuff so I'm sure I'm missing something really simple but for now I can't seem to figure it out at all.


What harm wouild it do if youcleared rects every time you pressed start? The first tim erects would be empty, so clear would do nothing, in a harmless kind of way.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.