// file: testGraphics6.java // author: Robert Keller // purpose: faster animation // /* This program is similar to testGraphics5. However, rather than redraw the logo image by drawing its components each time, we make one image then draw that. This makes the update considerable faster. */ import java.awt.*; // to get awt (abstract window toolkit) import java.lang.Thread; class testGraphics6 extends Frame // needed to allow over-ride of paint { // consider this to be magic for now Image image; // image provides graphics buffer Image logo; // Logo to be displayed // 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); } public void update(Graphics g) { paint(g); } // draw stuff at position x, y void drawStuff(int x, int y) { Graphics g = image.getGraphics(); g.drawImage(logo, x, y, null); } // Draw Logo on indicate graphics static void drawLogoOn(Graphics g) { g.setColor(Color.black); // set the color for drawing g.drawRect(50, 50, 400, 300); // draw a rectangle g.setColor(Color.blue); g.fillOval(100, 100, 300, 200); // fill an oval g.setColor(Color.yellow); g.drawOval(100, 100, 300, 200) ; // draw an oval g.setFont(new Font("Times", Font.BOLD, 28)); g.drawString("Harvey Mudd College", 140, 210); // draw a string } public static void main(String[] arg) { testGraphics6 frame = new testGraphics6(); // create the frame frame.setBackground(Color.white); // avoid startup flicker 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); frame.logo = frame.createImage(frame.size().width, frame.size().height); drawLogoOn(frame.logo.getGraphics()); // draw logo for( int i = 0; i < 100; i += 1 ) { frame.drawStuff(i, i); try{Thread.sleep(5);} catch(Exception e){} // delay 5 milliseconds frame.repaint(); } } }