ButtonApp1: Main.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** //*9 Javadoc comments
 * Main program for ButtonApp1 //*9
 * This is an example of a javadoc comment //*9
 */ //*9
public class Main extends JFrame
implements ActionListener {
    /** Miscellaneous "global" data, we keep here */ //*9
    private Rectangle square = new Rectangle (100, 50, 50, 50); //*7 Save application data so can redraw
    private boolean filled = false; // currently filled or not? //*7

    /** Remember these for listener callbacks */ //*9
    private Canvas canvas;
    private JButton bleft, bright, bbigger, bsmaller, bfill; //*4 Main is listener for all buttons

    public static void main (String [] args) {
	java.awt.EventQueue.invokeLater (new Runnable() {
            public void run() {
		new Main ();
            }
        });
    }

    public Main () {
	// Window setup
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	setSize (500, 500);
	setLayout (new BorderLayout()); //*1 Nested layout

	// Our canvas
	canvas = new Canvas (this);
	add (canvas, BorderLayout.CENTER); //*1

	// Control panel at bottom
	JPanel controls = new JPanel ();
	controls.setBorder (new LineBorder(Color.BLUE));
	controls.setLayout (new FlowLayout()); //*1
	add (controls, BorderLayout.SOUTH); //*1

	// First button, left arrow, use its public methods to set it up for now
	bleft = new JButton ();
	bleft.setBorder (new LineBorder(Color.GREEN, 2)); //*2 Set these via public methods
	bleft.setIcon (new ImageIcon ("leftArrow.gif")); //*3 Icon buttons
	bleft.addActionListener (this); //*4
	controls.add (bleft); //*1

	// Right arrow button
	bright = new JButton ();
	bright.setBorder (new LineBorder(Color.GREEN, 2)); //*2
	bright.setIcon (new ImageIcon ("rightArrow.gif")); //*3
	bright.addActionListener (this); //*4
	controls.add (bright);

	// "Bigger" button
	bbigger = new JButton ();
	bbigger.setText ("Bigger"); //*2
	bbigger.setFont (new Font("SansSerif", Font.ITALIC, 14)); //*2
	bbigger.setForeground (Color.BLUE); //*2
	bbigger.setBorder (new LineBorder(Color.GREEN, 2)); //*2
	bbigger.addActionListener (this); //*4
	controls.add (bbigger);

	// "Smaller" button
	bsmaller = new JButton ();
	bsmaller.setText ("Smaller"); //*2
	bsmaller.setFont (new Font("SansSerif", Font.ITALIC, 14)); //*2
	bsmaller.setForeground (Color.BLUE); //*2
	bsmaller.setBorder (new LineBorder(Color.GREEN, 2)); //*2
	bsmaller.addActionListener (this); //*4
	controls.add (bsmaller);

	// "Filled" button
	bfill = new JButton ();
	bfill.setText ("Filled"); //*2
	bfill.setFont (new Font("SansSerif", Font.ITALIC, 14)); //*2
	bfill.setForeground (Color.BLUE); //*2
	bfill.setBorder (new LineBorder(Color.GREEN, 2)); //*2
	bfill.addActionListener (this); //*4
	controls.add (bfill);

	setVisible (true);
    }

    /** //*9
     * Canvas calls us to do the job, cause we keep the data //*9
     * We draw, according to current values of square and filled //*9
     * @param g This is how you document arguments //*9
     */
    public void drawSquare (Graphics g) { //*8 Canvas call us, cause we keep the data
	Graphics2D g2 = (Graphics2D) g;

	if (filled) g2.fill (square); //*8
	else g2.draw (square); //*8
    }

    /** Uses button ivars to decode */ //*9
    public void actionPerformed (ActionEvent e) {
	if (e.getSource()==bleft) { //*4
	    square.x -= 10; //*5 Update our application data and draw
	    canvas.repaint (); //*5
	}
	else if (e.getSource()==bright) { //*4
	    square.x += 10; //*5
	    canvas.repaint ();
	}
	else if (e.getSource()==bbigger) { //*4
	    square.width += 10; //*5
	    square.height += 10; //*5
	    canvas.repaint ();
	}
	else if (e.getSource()==bsmaller) { //*4
	    square.width -= 10; //*5
	    square.height -= 10; //*5
	    canvas.repaint ();
	}
	else if (e.getSource()==bfill) { //*4
	    filled = ! filled; //*5
	    canvas.repaint ();

	    // Also update button label
	    if (filled) bfill.setText ("Empty"); //*6 Also updates our label
	    else bfill.setText ("Filled"); //*6
	}
    }
}

[download file]