import java.applet.Applet; import java.awt.*; public class BufferedEyes extends Applet{ double lx=150, ly=150, rx=260, ry=150; // Buffering to avoid flickering Dimension offDimension; Image offImage; Graphics offGraphics; public void update(Graphics g) { Dimension d = size(); // Size of the applet as it now is. // Create the offscreen graphics context, if no good one exists. if ( (offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height) ) { offDimension = d; offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); } //Erase the previous image. offGraphics.setColor(getBackground()); offGraphics.fillRect(0, 0, d.width, d.height); // Paint as we would do without buffering offGraphics.setColor(Color.white); offGraphics.fillOval(100, 100, 100, 100); offGraphics.fillOval(210, 100, 100, 100); offGraphics.setColor(Color.black); offGraphics.fillOval((int)(lx-8), (int)(ly-8), 16, 16); offGraphics.fillOval((int)(rx-8), (int)(ry-8), 16, 16); // Map the image onto the screen. g.drawImage(offImage, 0, 0, this); } // Adapt paint by refering to update. public void paint (Graphics g) { update(g); } public boolean mouseMove (Event evt, int x, int y){ double X=x, Y=y; double k = scale(X-150, Y-150); lx = k*(X-150)+150; ly = k*(Y-150)+150; k = scale(X-260, Y-150); rx = k*(X-260)+260; ry = k*(Y-150)+150; repaint(); return true; } public double scale (double x, double y){ return 42 / Math.sqrt(x*x+y*y+1764); } }