// file: MyApplet1.java // author: Robert Keller // purpose: Applet version of MyFrame1 import java.applet.*; import java.awt.*; // to get awt (abstract window toolkit) public class MyApplet1 extends Applet { // 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 setVisible(true); // show the frame } // draw items in Graphics void drawStuff(Graphics g) { // dimensions and offsets int xCenter = 250; int yCenter = 200; int rectWidth = 400; int rectHeight = 300; int ovalWidth = 300; int ovalHeight = 200; String hmc = "Harvey Mudd College"; g.setFont(new Font("Times", Font.BOLD, 20)); int stringOffset = xCenter - g.getFontMetrics().stringWidth(hmc)/2; int stringBase = 10 + yCenter; // background color g.setColor(Color.white); g.fillRect(0, 0, size().width, size().height); // shapes g.setColor(Color.black); g.drawRect(xCenter-rectWidth/2, yCenter-rectHeight/2, rectWidth, rectHeight); g.fillOval(xCenter-ovalWidth/2, yCenter-ovalHeight/2, ovalWidth, ovalHeight); // lettering g.setColor(Color.yellow); g.drawString("Harvey Mudd College", stringOffset, stringBase); } // paint(Graphics) will be called by the system to paint the Frame when // necessary, e.g. when setVisible(true) 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) { drawStuff(g); } }