import java.awt.*; import java.awt.event.*; import PositionPanel; public class JavaEyes extends Frame implements Runnable{ Panel my_p; Button quit_button; Point mouse_pos; /* This next line declares the variable my_thread of type Thread. * You will comment this line out during your lab to * Observe the compiler error you get. * Please do not comment it out until the lab asks you to. */ Thread my_thread; Image offscreen_image; public JavaEyes() { /* Note: This is the constructor for the JavaEyes window. * You should NOT look into here for changes */ super("JavaEyes"); my_p = new Panel(); this.add("Center",my_p); // record the latest mouse position mouse_pos = new Point(); my_p.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { JavaEyes.this.setMousePos(e.getX(),e.getY()); } }); // set up a Quit button quit_button = new Button("Quit"); this.add("North",quit_button); quit_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JavaEyes.this.dispose(); System.exit(0); } }); // standard window stuff this.pack(); this.show(); this.setSize(400,300); this.setLocation(1,1); // handle Window Closing events this.addWindowListener(new WindowAdapter() { public void WindowClosed(WindowEvent e) { JavaEyes.this.dispose(); System.exit(0); } }); my_thread= new Thread(this); my_thread.start(); } public static void main(String argv[]) { new JavaEyes(); } public synchronized void setMousePos(int x, int y) { mouse_pos.setLocation(x, y); } public synchronized Point getMousePos() { return mouse_pos.getLocation(); } public void paintEyes(Graphics g) { /* * Note: a lot of this is grungy math to position the eyes. * there are some interesting things that you can change, though. */ /* This paints onto a graphic that is only 200 by 80 pixels, but it is using the coordinates * of the mouse in a much larger panel. Thus, we are going to be painting using coordinates * within the (200,80) rectangle, but calculating the position of the pupils using greater * coordinates */ // it's a 200 by 80 rectangle int g_width= 200; int g_height= 80; // Get the dimensions of the central Panel Dimension d= my_p.getSize(); int w= d.width; int h= d.height; // The offset of the (200,80) rectangle in the Panel int x_offset= (w-g_width)/2; int y_offset= h-g_height; // Get the mouse position Point p= getMousePos(); // Set the color to white, and draw the background rectangle for the eyes // POSSIBLE MODIFICATION: You can change the color of the background rectangle. g.setColor(Color.white); g.fillRect(0,0,g_width,g_height); // Set the color to black, to draw the eyes. // POSSIBLE MODIFICATION: You can change the color of the eyes. g.setColor(Color.black); // draw the outline of the eyes g.drawOval(35,20,50,50); g.drawOval(105,20,50,50); // the centers of the eyes, using the small rectangle coordinates int eye1_x= 55 ; int eye1_y= 40; int eye2_x= 125; int eye2_y= 40; // the centers of the eyes, with offsets, in the panel coordinates int eye1_offset_x= eye1_x + x_offset; int eye1_offset_y= eye1_y + y_offset; int eye2_offset_x= eye2_x + x_offset; int eye2_offset_y= eye2_y + y_offset; int x1,y1,x2,y2,dist; dist= (int)Math.sqrt((p.x-eye1_offset_x)*(p.x-eye1_offset_x)+(p.y-eye1_offset_y)*(p.y-eye1_offset_y)); x1=(int) ((p.x-eye1_offset_x) * 16 / dist); y1=(int) ((p.y-eye1_offset_y) * 16 / dist); dist= (int)Math.sqrt((p.x-eye2_offset_x)*(p.x-eye2_offset_x)+(p.y-eye2_offset_y)*(p.y-eye2_offset_y)); x2=(int) ((p.x-eye2_offset_x) * 16/dist); y2=(int) ((p.y-eye2_offset_y) * 16/dist); // draw the eyes!! g.fillOval(eye1_x+x1,eye1_y+y1,12,12); g.fillOval(eye2_x+x2,eye2_y+y2,12,12); } public void updateEyes() { /* Note: This implements a graphical technique called Double Buffering. * You need not worry about what this does, although it will be * explained later on in the course */ // If no offscreen image has yet been created, create one. if (offscreen_image==null) { offscreen_image= createImage(200,80); } paintEyes(offscreen_image.getGraphics()); // paint the offscreen image onto the Panel Dimension d= my_p.getSize(); my_p.getGraphics().drawImage(offscreen_image,d.width/2-100,d.height-80,this); repaint(); } public void run() { /* Note: DON'T try to modify this. */ while(true) { updateEyes(); try { Thread.sleep(100); } catch (Throwable e) { } //Point p= getMousePos(); } } }