// file: MyFrame2.java // author: Robert Keller // purpose: Flicker removal using off-screen buffer import java.awt.*; public class MyFrame2 extends Frame // Customize Frame class { Image buffer; // off-screen buffer // test Program public static void main(String[] arg) { new MyFrame2("My Frame", 50, 50); } // construct frame with title and position MyFrame2(String title, int x, int y) { setTitle(title); setBackground(Color.white); // set the background color reshape(x, y, 500, 400); // set the location and size setVisible(true); // show the frame toFront(); } // over-ride update public void update(Graphics g) { paint(g); } // draw items in buffer Graphics 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", 120, 210); // draw a string } // 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. // The Graphics of the frame will then be passed as an argument. public void paint(Graphics g) { if( buffer == null ) buffer = createImage(getWidth(), getHeight()); drawStuff(); g.drawImage(buffer, 0, 0, null); } }