// Program 16.3: Panel Example import java.applet.Applet; import java.awt.Graphics; import java.awt.Canvas; import java.awt.Label; import java.awt.Color; import java.awt.Dimension; import java.awt.Event; import java.awt.Font; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Panel; public class Panic extends Applet { public void init () { setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); p1.add(new Label("Don't Panic", Label.CENTER)); add("North", p1); Panel p2 = new Panel(); p2.setLayout(new FlowLayout()); p2.add(new PanicButton()); add("Center", p2); } public boolean action(Event e, Object arg) { if (e.target instanceof PanicButton) { System.exit(1); return true; } else { return false; } } } class PanicButton extends Canvas { int radius = 100; public void paint(Graphics g) { g.setFont(new Font("Helvetica", Font.BOLD, 24)); g.setColor(Color.red); g.fillOval(0, 0, 2*radius, 2*radius); g.setColor(Color.yellow); g.drawString("Panic", 65, radius+12); } public boolean mouseUp(Event e, int x, int y) { // Was the click inside the circle?? if (Math.sqrt( (x-radius)*(x-radius) + (y-radius)*(y-radius)) <= radius) { postEvent(new Event(this, Event.ACTION_EVENT, "Panic")); return true; } else { return false; } } public Dimension minimumSize() { return new Dimension(2*radius,2*radius); } public Dimension preferredSize() { return minimumSize(); } }