// file: MyFrame2Applet.java // author: Robert Keller // purpose: Applet version of MyFrame2 import java.applet.*; import java.awt.*; // to get awt (abstract window toolkit) public class MyFrame2Applet extends Applet { Image buffer; // off-screen buffer // init is called from the outside to initialize the Applet public void init() { setup("My Frame", 50, 50); } // setup frame with title and position void setup(String title, int x, int y) { setBackground(Color.white); // set the background color reshape(x, y, 500, 400); // set the location and size show(); // show the frame } // start the applet public void start() { if( buffer == null ) buffer = createImage(size().width, size().height); drawStuff(); } // over-ride update public void update(Graphics g) { paint(g); } // draw items in Graphics specified in argument; called by paint(g) void drawStuff() { Graphics g = buffer.getGraphics(); g.setColor(Color.black); // set the color for drawing g.drawRect(50, 50, 400, 300); // draw a rectangle g.fillOval(100, 100, 300, 200); // fill an oval g.setFont(new Font("Times", Font.BOLD, 20)); g.setColor(Color.yellow); g.drawString("Harvey Mudd College", 115, 210); // draw a string } // paint(Graphics) will be called by the system to paint the Frame when // necessary, e.g. when show() is called. This call is done // implicitly; we do not see it in the source. // It is also called when the window is opened or moved. // The Graphics of the frame will then be passed as an argument. public void paint(Graphics g) { g.drawImage(buffer, 0, 0, null); } }