import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;


class MyPanel extends JPanel {
    int x;
    int y;
    int width;
    int height;
    boolean smaller = true;
    
    public MyPanel(int w, int h) {
        super();
        setBackground(Color.orange);
        x = 4;
        y = 4;
        width = w;
        height = h;
        setPreferredSize(new Dimension(width, height));
    }
    
    public void drawRects() {
        Graphics2D g2D = (Graphics2D)getGraphics();
        if( smaller ) {
            smaller = width > 0 && height > 0;
            Rectangle r = new Rectangle(x,y,width,height);
            x = x + 2;
            y = y + 2;
            width = width - 4;
            height = height - 4;
            g2D.draw(r);
        } else {
            smaller = width >= getSize().getWidth() || height >= getSize().getHeight();
            x = x - 2;
            y = y - 2;
            width = width + 4;
            height = height + 4;
            g2D.setColor(Color.orange);
            g2D.fillRect(x,y,width,height);
        } // end if
    }
}


public class ImplodingRectangles extends JFrame  {
    JButton startButton;
    JScrollPane startPane;
    MyPanel paintPanel;
    JScrollPane paintPane;
    Timer timer;
    
    public ImplodingRectangles(String title) {
        super(title);
        setBackground(Color.red);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.CENTER,200,30));
        startButton = new JButton("Start");
        startButton.setToolTipText("Click the button to start drawing rectangles");
        startButton.addActionListener(new startAction());
        paintPanel = new MyPanel(500,500);
        startPane = new JScrollPane(startButton);
        add(startPane);
        paintPane = new JScrollPane(paintPanel);
        add(paintPane);
        timer = new Timer(100,new paintAction());
    } // end constructor
    
    
    class paintAction implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            // put action code here for JButton or JTextField etc
            paintPanel.drawRects();
            
        }
        
    } // end paintAction
    
    class startAction implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            // put action code here for JButton or JTextField etc
            timer.start();
            startPane.setVisible(false);
            
        }
        
    } // end startAction
    
    public static void main(String[] args) {
        JFrame theFrame = new ImplodingRectangles("Imploding Rectangles");
        theFrame.setSize(700,700);
        theFrame.setVisible(true);
    } // end main
    
} // end ImplodingRectangles

