// file: testGraphics3a.java // author: Robert Keller // purpose: Showing the fact that update repaints the background. // /* This is for comparison to testGraphics3.java. There is some flicker, but because the background is now painted white by 'update', which is also the background of the image, it is less noticeable. See testGraphics4.java for further improvement. */ import java.awt.*; // to get awt (abstract window toolkit) class testGraphics3a extends Frame // needed to allow over-ride of paint { // consider this to be magic for now Image image; // image provides graphics buffer // paint(Graphics) will be called by the system to paint the Frame when // necessary. At that time, the Graphics of the frame will be passed as // an argument. public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } // draw stuff at position x, y void drawStuff(int x, int y) { Graphics g = image.getGraphics(); // get Graphics buffer g.setColor(Color.white); g.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); g.setColor(Color.black); // set the color for drawing g.drawRect(x+50, y+50, 400, 300); // draw a rectangle g.setColor(Color.blue); g.fillOval(x+100, y+100, 300, 200); // fill an oval g.setColor(Color.yellow); g.drawOval(x+100, y+100, 300, 200); // draw an oval g.setFont(new Font("Times", Font.BOLD, 28)); g.setColor(Color.yellow); g.drawString("Harvey Mudd College", x+140, y+210); // draw a string } public static void main(String[] arg) { testGraphics3a frame = new testGraphics3a(); // create the frame frame.setBackground(Color.white); // set the background color frame.reshape(50, 50, 600, 500); // set the location and size frame.show(); // show the frame // Note: the following must be done AFTER the frame is shown. frame.image = frame.createImage(frame.size().width, frame.size().height); for( int i = 0; i < 100; i += 10 ) { frame.drawStuff(i, i); try{Thread.sleep(5);} catch(Exception e){} // delay 5 milliseconds frame.repaint(); } } }