import javax.swing.*; import java.awt.*; import java.util.*; public class Stapler extends JFrame { // class constants private final int MAX_NUMBER_STAPLES = 100; private static Scanner scanner = new Scanner(System.in); // object attributes private int numberStaplesLeft = 0; private JButton button; // Stapler(): default constructor public Stapler() { System.out.println("Starting default constructor"); sleep(); this.setSize(350, 250); this.button = new JButton( String.valueOf(numberStaplesLeft), new ImageIcon("walking-stapler.gif") ); this.button.setBackground( Stapler.randomColor() ); Font font = button.getFont(); float size = 10*font.getSize(); button.setFont( font.deriveFont(size) ); this.empty(); this.add(button); this.setVisible(true); this.drawStapler(); } // empty(): remove all staples from the stapler public void empty() { System.out.println("Starting empty()"); sleep(); this.numberStaplesLeft = 0; this.drawStapler(); } // punch(): use up a stapler public void punch() { System.out.println("Starting punch()"); sleep(); this.numberStaplesLeft = this.numberStaplesLeft - 1; this.drawStapler(); } // numberLeft(): report the number of staples in the Stapler public int numberLeft() { System.out.println("Starting numberLeft()"); sleep(); int result = this.numberStaplesLeft; this.drawStapler(); return result; } // toString(): produce a representation of the state of the Stapler public String toString() { sleep(); String result = "Stapler: [ " + numberLeft() + " ]"; return result; } // refill(): fully load stapler public void fill() { System.out.println("Starting fill()"); sleep(); this.numberStaplesLeft = MAX_NUMBER_STAPLES; this.drawStapler(); } public void drawStapler() { button.setText( String.valueOf(this.numberStaplesLeft) ); } public static Color randomColor() { Random variate = new Random(); int r = variate.nextInt(256); int g = variate.nextInt(256); int b = variate.nextInt(256); return new Color (r,g, b); } public static void sleep() { scanner.nextLine(); } }