//Overrides methods paint(), addActionListener(), removeActionListener(), processMouseEvent() // //Presents to the user the methods //public void paintImage(int arr[][], Color c) //public void removeImage() //public void resetColor(Color c) // //Reacts to mouse events MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_ENTERED, MOUSE_EXITED //by calling the actionPerformed() method of the registered action listener with a newly //generated ActionEvent as a parameter. The ActionEvent is constructed using the constructor //ActionEvent(Object src, int id, String cmd) //as //new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "pressed/released/entered/exited") import java.awt.*; import java.awt.event.*; public class GoodButton extends Component { private int width,height; private String text; private Color color = new Color(100,100,100); private Color imageColor = new Color(0,0,0); public int shape[][] = null; private ActionListener actionListener; public GoodButton() { width = 10; height = 10; //enableEvents(AWTEvent.MOUSE_EVENT_MASK);enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); } public GoodButton(int w, int h) { width = w; height = h; //enableEvents(AWTEvent.MOUSE_EVENT_MASK);enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); } public GoodButton(String s) { text = s; width = height = 10; //enableEvents(AWTEvent.MOUSE_EVENT_MASK);enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); } public GoodButton(String s, int w, int h) { text = s; width = w; height = h; //enableEvents(AWTEvent.MOUSE_EVENT_MASK);enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); } public GoodButton(Color c, int w, int h) { color = c; width = w; height = h; //enableEvents(AWTEvent.MOUSE_EVENT_MASK);enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); } //lets the caller change background color of the button public void resetColor(Color c) {color = c;repaint();} //gets called by the container when container is painted; container provides graphics context public void paint(Graphics g) //overrides Component's paint() { g.setColor(color); g.fillRect(0,0,width,height); if (text != null) //there is a string to be drawn on top { g.drawString(text,0,height/2); } if (shape != null) //there is an image to be painted on top { g.setColor(imageColor); for (int i = 0; i < 32; i++) for (int j = 0; j < 32; j++) if (shape[i][j] == 1) g.drawRect(j,i,1,1); g.setColor(color); } } //called by the user who wants to put an image on top of the button; //image is defined by an array of int, //where 1 means a "paint a dot with Color c", and 0 means "leave it with background color" public void paintImage(int arr[][], Color c) { shape = arr; imageColor = c; repaint(); } //clear the image on top public void removeImage() { shape = null; repaint(); } //these methods are necessary in order to correctly display the width and height of the button //It appears that LayoutManager uses them and the default version will select the size automatically public Dimension getPreferredSize() {return getMinimumSize();} public Dimension getMinimumSize() {return new Dimension(width,height);} //this is the minimum! public void addActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.add(actionListener, listener); enableEvents(AWTEvent.MOUSE_EVENT_MASK); enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); } public void removeActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.remove(actionListener, listener); } public void processMouseEvent(MouseEvent e) //overrides Component; we will respond to mouse events { if (e.getID() == MouseEvent.MOUSE_PRESSED) { if(actionListener != null) actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "" + "pressed")); repaint(); } /*if (e.getID() == MouseEvent.MOUSE_DRAGGED) it does not work { if(actionListener != null) actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "" + "dragged")); repaint(); } */ else if (e.getID() == MouseEvent.MOUSE_RELEASED) { if(actionListener != null) actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "" + "released")); repaint(); } else if (e.getID() == MouseEvent.MOUSE_ENTERED) { if(actionListener != null) actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "" + "entered")); } else if (e.getID() == MouseEvent.MOUSE_EXITED) { if(actionListener != null) actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "" + "exited")); repaint(); } super.processMouseEvent(e); } }