CSCI A201/A597 and I210

Lab Notes Fourteen

Second semester 2000-2001


Finishing project 6 (last one)

Lab 14 practice problems in QuizSite have been posted late, on Monday, April 23.

They will stay open all week.

Last (large) batch will be posted next week, to help you with the final exam.

We already have the Device that we developed in lecture.

import java.awt.*; 

public class Device {
    int x, y, r, R, targetX, targetY;
    public Device(int x, int y, int r, int R) {
	this.x = x;
	this.y = y;
	this.r = r;
	this.R = R;
	targetX = x + R;
	targetY = y + R;
    }
    public void draw(Graphics g) {
	g.drawOval(x, y, 2 * R, 2 * R);
	int xA = x + R, yA = y + R, xB = targetX, yB = targetY;
	g.drawLine(xA, yA, xB, yB);
	double distance = Math.sqrt((xA - xB) * (xA - xB) + 
				    (yA - yB) * (yA - yB));
	int xC, yC; 

	if (distance < R - r) {
	    xC = targetX; yC = targetY;
	} else {
	    double percent = (R - r)/ distance;

	    xC = (int) (percent * (xB - xA)) + xA;
	    yC = (int) (percent * (yB - yA)) + yA;

	}

	g.fillOval(xC-r, yC-r, 2 * r, 2 * r);
    }
}
Let's set up an HTML file:
<html>
  <head>
    <title>All eyes on the mouse!</title>
  </head>
  <body>
    <applet code=Proj6.class height=300 width=300>

    </applet>
  </body>
</html> 
Let's put together the framework for a mouse watching applet:
import java.applet.*;
import java.awt.*;
import java.awt.event.*; 

public class Proj6 extends Applet implements MouseListener, 
					     MouseMotionListener {

    public void init() {
	addMouseListener(this); 
	addMouseMotionListener(this); 
    } 

    public void mouseClicked (MouseEvent e) {
	System.out.println("Mouse clicked at: "  + e.getX() + " " + e.getY());
    }
    public void mousePressed (MouseEvent e) {
	System.out.println("Mouse pressed at: "  + e.getX() + " " + e.getY());
    }
    public void mouseReleased(MouseEvent e) {
	System.out.println("Mouse released at: " + e.getX() + " " + e.getY());
    }
    public void mouseEntered (MouseEvent e) {
	System.out.println("Mouse entered at: "  + e.getX() + " " + e.getY());
    }
    public void mouseExited  (MouseEvent e) {
	System.out.println("Mouse exited at: "   + e.getX() + " " + e.getY());
    }
    public void mouseDragged  (MouseEvent e) {
	System.out.println("Mouse dragged at: "  + e.getX() + " " + e.getY());
    }
    public void mouseMoved (MouseEvent e) {
	System.out.println("Mouse moved at: "    + e.getX() + " " + e.getY());
    }
} 
Please note this is a framework.

That means it's given, we need to know it.

Later you will understand this framework is very general.

For the time being we learn to use it as it is.

Let's start using the Devices we developed.

import java.applet.*;
import java.awt.*;
import java.awt.event.*; 

public class Proj6 extends Applet implements MouseListener, 
					     MouseMotionListener {

    Device eye1 = new Device(90, 60, 6, 20), 
	   eye2 = new Device(140, 60, 6, 20); 

    public void paint(Graphics g) {
	eye1.draw(g); 
	eye2.draw(g); 
    }

    public void init() {
	addMouseListener(this); 
	addMouseMotionListener(this); 
    } 

    public void mouseClicked (MouseEvent e) {
	System.out.println("Mouse clicked at: "  + e.getX() + " " + e.getY());
    }
    public void mousePressed (MouseEvent e) {
	System.out.println("Mouse pressed at: "  + e.getX() + " " + e.getY());
    }
    public void mouseReleased(MouseEvent e) {
	System.out.println("Mouse released at: " + e.getX() + " " + e.getY());
    }
    public void mouseEntered (MouseEvent e) {
	System.out.println("Mouse entered at: "  + e.getX() + " " + e.getY());
    }
    public void mouseExited  (MouseEvent e) {
	System.out.println("Mouse exited at: "   + e.getX() + " " + e.getY());
    }
    public void mouseDragged  (MouseEvent e) {
	System.out.println("Mouse dragged at: "  + e.getX() + " " + e.getY());
    }
    public void mouseMoved (MouseEvent e) {
	int x = e.getX(), y = e.getY(); 
	eye1.targetX = x; eye1.targetY = y; 
	eye2.targetX = x; eye2.targetY = y; 
	repaint(); 
    }
} 
Well, we're almost there.

Now we need a nose and a mouth.

import java.applet.*;
import java.awt.*;
import java.awt.event.*; 

public class Proj6 extends Applet implements MouseListener, 
					     MouseMotionListener {

    Device eye1 = new Device(90, 60, 6, 20), 
	eye2 = new Device(140, 60, 6, 20); 

    public void paint(Graphics g) {
	eye1.draw(g); 
	eye2.draw(g); 
	g.drawOval(100, 120, 70, 25);  
	if (true) {
	    g.drawArc(-15, -110, 300, 300, 240, 60); 
	} 
    }

    public void init() {
	addMouseListener(this); 
	addMouseMotionListener(this); 
    } 

    public void mouseClicked (MouseEvent e) {
	System.out.println("Mouse clicked at: "  + e.getX() + " " + e.getY());
    }
    public void mousePressed (MouseEvent e) {
	System.out.println("Mouse pressed at: "  + e.getX() + " " + e.getY());
    }
    public void mouseReleased(MouseEvent e) {
	System.out.println("Mouse released at: " + e.getX() + " " + e.getY());
    }
    public void mouseEntered (MouseEvent e) {
	System.out.println("Mouse entered at: "  + e.getX() + " " + e.getY());
    }
    public void mouseExited  (MouseEvent e) {
	System.out.println("Mouse exited at: "   + e.getX() + " " + e.getY());
    }
    public void mouseDragged  (MouseEvent e) {
	System.out.println("Mouse dragged at: "  + e.getX() + " " + e.getY());
    }
    public void mouseMoved (MouseEvent e) {
	int x = e.getX(), y = e.getY(); 
	eye1.targetX = x; eye1.targetY = y; 
	eye2.targetX = x; eye2.targetY = y; 
	repaint(); 
    }
} 
Basically we're done, only we need to keep track where the mouse is.

import java.applet.*;
import java.awt.*;
import java.awt.event.*; 

public class Proj6 extends Applet implements MouseListener, 
					     MouseMotionListener {

    Device eye1 = new Device(90, 60, 6, 20), 
     	   eye2 = new Device(140, 60, 6, 20); 

    boolean in = false; 

    public void paint(Graphics g) {
	eye1.draw(g); 
	eye2.draw(g); 
	g.drawOval(100, 120, 70, 25); 
	if (! in) {
	    g.drawArc(-15, -110, 300, 300, 240, 60); 
	} else {
	    g.drawArc(-15, -110, 300, 300, 250, 40);
	}
    }

    public void init() {
	addMouseListener(this); 
	addMouseMotionListener(this); 
    } 

    public void mouseClicked (MouseEvent e) {
	System.out.println("Mouse clicked at: "  + e.getX() + " " + e.getY());
    }
    public void mousePressed (MouseEvent e) {
	System.out.println("Mouse pressed at: "  + e.getX() + " " + e.getY());
    }
    public void mouseReleased(MouseEvent e) {
	System.out.println("Mouse released at: " + e.getX() + " " + e.getY());
    }
    public void mouseEntered (MouseEvent e) {
	in = true; 
	repaint(); 
    }
    public void mouseExited  (MouseEvent e) {
	in = false; 
	repaint(); 
    }
    public void mouseDragged  (MouseEvent e) {
	System.out.println("Mouse dragged at: "  + e.getX() + " " + e.getY());
    }
    public void mouseMoved (MouseEvent e) {
	int x = e.getX(), y = e.getY(); 
	eye1.targetX = x; eye1.targetY = y; 
	eye2.targetX = x; eye2.targetY = y; 
	repaint(); 
    }
} 
Now we need a bit more from our Devices, so let's work on them a bit.

import java.awt.*; 

public class Device {
    int x, y, r, R, targetX, targetY;
    public Device(int x, int y, int r, int R) {
	this.x = x;
	this.y = y;
	this.r = r;
	this.R = R;
	targetX = x + R;
	targetY = y + R;
    }
    public void relax() {
	targetX = x + R;
	targetY = y + R;
    }
    public void draw(Graphics g) {
	g.drawOval(x, y, 2 * R, 2 * R);
	int xA = x + R, yA = y + R, xB = targetX, yB = targetY;
	// g.drawLine(xA, yA, xB, yB);
	double distance = Math.sqrt((xA - xB) * (xA - xB) + 
				    (yA - yB) * (yA - yB));
	int xC, yC; 

	if (distance < R - r) {
	    xC = targetX; yC = targetY;
	} else {
	    double percent = (R - r)/ distance;

	    xC = (int) (percent * (xB - xA)) + xA;
	    yC = (int) (percent * (yB - yA)) + yA;

	}

	g.fillOval(xC-r, yC-r, 2 * r, 2 * r);
    }
}
This gives us a chance to look forward when not watching.

It also eliminates the lines.

The main object simply controls the devices accordingly.

import java.applet.*;
import java.awt.*;
import java.awt.event.*; 

public class Proj6 extends Applet implements MouseListener, 
					     MouseMotionListener {

    Device eye1 = new Device(90, 60, 6, 20), 
	eye2 = new Device(140, 60, 6, 20); 

    boolean in = false; 

    public void paint(Graphics g) {
	eye1.draw(g); 
	eye2.draw(g); 
	g.drawOval(100, 120, 70, 25); 
	if (! in) {
	    g.drawArc(-15, -110, 300, 300, 240, 60); 
	} else {
	    g.drawArc(-15, -110, 300, 300, 250, 40);
	}
    }

    public void init() {
	addMouseListener(this); 
	addMouseMotionListener(this); 
    } 

    public void mouseClicked (MouseEvent e) {
	System.out.println("Mouse clicked at: "  + e.getX() + " " + e.getY());
    }
    public void mousePressed (MouseEvent e) {
	System.out.println("Mouse pressed at: "  + e.getX() + " " + e.getY());
    }
    public void mouseReleased(MouseEvent e) {
	System.out.println("Mouse released at: " + e.getX() + " " + e.getY());
    }
    public void mouseEntered (MouseEvent e) {
	in = true; 
	repaint(); 
    }
    public void mouseExited  (MouseEvent e) {
	in = false; 
	eye1.relax(); 
	eye2.relax(); 
	repaint(); 
    }
    public void mouseDragged  (MouseEvent e) {
	System.out.println("Mouse dragged at: "  + e.getX() + " " + e.getY());
    }
    public void mouseMoved (MouseEvent e) {
	int x = e.getX(), y = e.getY(); 
	eye1.targetX = x; eye1.targetY = y; 
	eye2.targetX = x; eye2.targetY = y; 
	repaint(); 
    }
} 
And we get this.


Last updated: Apr 19, 2001 by Adrian German for A201