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 { /** * We remember our label, so can redraw */ protected String label; /** * We remember our location, for redraw *and* for testing mouse input */ protected RectangularShape loc; public Rawbutton (String label, int x, int y) { // Stash arg this.label = label; // You tell us where, but we determine size loc = new Rectangle (x, y, 80, 30); } /** * Our parent must call this when it's time to draw us */ public void draw (Graphics g) { Graphics2D g2 = (Graphics2D) g; // Background g2.setColor (Color.WHITE); g2.fill (loc); // Outline g2.setColor (Color.BLACK); g2.draw (loc); // Label g.drawString (label, (int)loc.getX()+10, (int)loc.getY()+25); } /** * Our parent must call this when it gets a mouse click */ public void domouse (int x, int y) { if (loc.contains (x, y)) System.out.println ("You pressed the button labeled " + label); } }