import java.awt.*; import java.awt.geom.*; /** * Screen button, built without using a toolkit * * To use this, you must draw() it and domouse() it when needed */ public class Rawbutton { //*1 Object not JComponent /** * We remember our label, so can redraw */ protected String label; //*2 We remember our label, use for redraw /** * We remember our location, for redraw *and* for testing mouse input */ protected RectangularShape loc; //*3 We remember our location, use for redraw and mouse public Rawbutton (String label, int x, int y) { // Stash arg this.label = label; //*2 // You tell us where, but we determine size loc = new Rectangle (x, y, 80, 30); //*3 } /** * Our parent must call this when it's time to draw us */ public void draw (Graphics g) { //*4 draw() and domouse() are dispatched to us outside of Swing Graphics2D g2 = (Graphics2D) g; // Background g2.setColor (Color.WHITE); //*5 Java2D function, works on other shapes too g2.fill (loc); //*5 // Outline g2.setColor (Color.BLACK); //*5 g2.draw (loc); //*5 // Label g.drawString (label, (int)loc.getX()+10, (int)loc.getY()+25); //*2 } /** * Our parent must call this when it gets a mouse click */ public void domouse (int x, int y) { //*4 if (loc.contains (x, y)) //*5 System.out.println ("You pressed the button labeled " + label); //*5 } }