Button1

Main.java  [download]

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

/** //*9 Javadoc comments
 * Main program for Button1 //*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 MyCanvas 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);
	Container content = getContentPane();
	content.setLayout (new BorderLayout()); //*1 Nested layout

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

	// Control panel at bottom
	JPanel controls = new JPanel ();
	controls.setBorder (new LineBorder(Color.BLUE));
	controls.setLayout (new FlowLayout()); //*1
	content.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);
    }

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

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

    /** Uses button ivars to decode */
    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
	}
    }
}

MyCanvas.java  [download]

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

/**
 * Our canvas
 */
public class MyCanvas extends JPanel implements MouseListener {
    private Main parent; //*1 Save parent so can call up to it for drawing

    public MyCanvas (Main parent) {
        this.parent = parent; //*1
	setBorder (new LineBorder(Color.BLUE));
	addMouseListener (this);
    }

    /** We just call our parent, cause it keeps the data */
    public void paintComponent (Graphics g) {
	super.paintComponent(g); // Supposed to do this to clear background, necessary in windowbuilder
	parent.drawSquare (g); //*2 Just call our parent
    }

    /** MouseListener callbacks */
    public void mousePressed (MouseEvent event) {	
	System.out.println ("Mouse down at " + event.getPoint().x + ", " + event.getPoint().y);
    }
    public void mouseClicked (MouseEvent event) {}
    public void mouseReleased (MouseEvent event) {}
    public void mouseEntered (MouseEvent event) {}
    public void mouseExited (MouseEvent event) {}
}