/** Shapes.java * @version 1 * @author Robert M. Keller * * Sample applet for displaying Shapes **/ /* $Id: Shapes.java,v 1.2 1997/02/10 06:09:35 keller Exp keller $ */ import java.applet.*; // applet classes import java.awt.*; // Abstract Window Toolkit classes import java.util.*; // utility classes import java.lang.Thread; // Thread class /** * Shapes is a simple skeleton applet with double buffered graphics. * It displays two nested shapes and some text. **/ public class Shapes extends Applet implements Runnable { static public Color backgroundColor = Color.white; static public Font MainFont = new Font("Helvetica", Font.BOLD, 18); static public int loopDelay = 10; // milliseconds for loop delay int maxCycles = 100; // cycles in graphic display static int fontSize[] = {8, 10, 12, 14, 18, 24}; // font sizes available Image image; // image which is repainted Graphics graphics; // graphics buffer int width, height; // width and height of buffer Button quitButton = new Button("Quit"); // button to quit the application Thread mythread; // The applet's thread /** * Initialize the applet. **/ public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); // layout buttons from left setBackground(backgroundColor); // set the background color makeGraphicsBuffer(); addButton(quitButton); } // set instance variables for graphics void makeGraphicsBuffer() { width = size().width; height = size().height; image = createImage(width, height); graphics = image.getGraphics(); } void clearGraphicsBuffer() { graphics.clearRect(0, 0, width, height); } // add a button to the applet void addButton(Button b) { b.setFont(MainFont); add(b); } /** * Run the applet. **/ public void run() { while(true) { for( int cycleNumber = 0; cycleNumber < maxCycles; cycleNumber++ ) { cycle(cycleNumber); sleep(loopDelay); } clearGraphicsBuffer(); } } /** * Do this every cycle. **/ void cycle(int i) { String message = "Hello, world"; // stuff for centering float fraction = ((float)i)/maxCycles; int width = (int)(400*fraction); int height = (int)(300*fraction); int x = (int)(150*(1-fraction)) + 150; int y = (int)(100*(1-fraction)) + 100; // display a centered rectangle graphics.setColor(Color.red); graphics.fillRect(x, y, width, height); // display a centered oval graphics.setColor(Color.blue); graphics.fillOval(x, y, width, height); // display message graphics.setColor(Color.yellow); Font font = new Font("Helvetica", Font.BOLD, fontSize[i/17]); graphics.setFont(font); FontMetrics fontMetrics = graphics.getFontMetrics(); graphics.drawString(message, x+width/2-(fontMetrics.stringWidth(message)/2), y+height/2); repaint(); } /** * update is implicitly called by repaint() * It calls paint(Graphics) **/ public void update(Graphics g) { paint(g); } /** * paint(Graphics) is is called by update(Graphics) **/ public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } /** * action handles events targeted for buttons, etc. * These events do not go to mouseUp etc. **/ public boolean action(Event event, Object arg) { if( event.target == quitButton ) { destroy(); } return super.action(event, arg); // Call super } /** * start is called when the applet is entered, or re-entered. **/ public void start() { if( mythread == null ) { mythread = new Thread(this); mythread.start(); } else mythread.resume(); } /** * stop is called when the applet is left. **/ public void stop() { mythread.suspend(); } /** * sleep makes the applet sleep for argument milliseconds **/ public static void sleep(int delay) { try { Thread.sleep(delay); } catch(InterruptedException e) { } } } // Shapes