import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; /** * A failed attempt to illustrate the paintComponent() method. * * @author Samuel A. Rebelsky * @version 1.0 of October 2004 */ public class FunnyButtons extends JFrame { /** All the buttons in the program. */ PaintedButton[] buttons; /** The number of buttons in the program. */ int numButtons; private FunnyButtons(String _title, int width, int height) { super(_title); // Exit nicely when closing this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Get the useful part of this frame. Container pane = this.getContentPane(); pane.setLayout(new GridLayout(width,height)); // Build and add the buttons this.numButtons = width*height; buttons = new PaintedButton[this.numButtons]; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { int pos = h*width + w; buttons[pos] = new PaintedButton(Integer.toString(pos)); buttons[pos].addActionListener(new MyListener(buttons[pos], this)); pane.add(buttons[pos]); } // inner for } // outer for //Display the window. this.pack(); this.setVisible(true); } // FunnyButtons(String) public static void main(String[] args) { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { new FunnyButtons("3x4", 3, 4); // new FunnyButtons("5x1", 5, 1); } }); } // main(String[]) } // class FunnyButtons class MyListener implements ActionListener { /** The number of the button we're listening to. */ PaintedButton button; /** The frame that contains that button. */ FunnyButtons owner; /** Guess. */ public MyListener(PaintedButton _button, FunnyButtons _owner) { this.button = _button; this.owner = _owner; } // MyListener(int, FunnyButtons) public void actionPerformed(ActionEvent ae) { System.out.println(this.button.getText()); PaintedButton target = owner.buttons[(Integer.parseInt(this.button.getText()) + 1) % owner.numButtons]; target.setText(Integer.toString((Integer.parseInt(target.getText()) + Integer.parseInt(button.getText())) % owner.numButtons)); } // actionPerformed(ae) } // class MyListener /** * Buttons that paint themselves in interesting ways */ class PaintedButton extends JButton { public PaintedButton(String name) { super(name); this.setMinimumSize(new Dimension(100,150)); this.setPreferredSize(new Dimension(100,150)); } // PaintedButton(string) public void paintComponent(Graphics g) { // super.paintComponent(g); System.out.print(this.getText() + ":"); System.out.print(" BOUNDS[" + this.getBounds()+ "]"); System.out.println(" CLIP[" + g.getClipBounds()+ "]"); g.setColor(Color.BLUE); g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); g.setColor(Color.RED); g.fillOval(this.getX(), this.getY(), this.getWidth(), this.getHeight()); } // paintComponent(Graphics) } // class PaintedButton