|
|
The lecture notes for Wed and Thu are from an older version of A202. (Two years ago A202 used to study some of the things that now properly belong in A201.) A201 and A202 have evolved a lot in the last five years or so, and so have all of us, and will continue to so forever, as you would probably expect us to. So Wednesday's lecture notes will give you an introduction to terminal I/O, sections 2.8, 3.10 and 13.1-4 in your text. And Thursday's lecture notes will give you an intro to basic networking with Java, which is a breeze (and the right thing to study) if you know terminal I/O.
In class we will discuss:
Here's the code we wrote in class on Wednesday:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Anim extends Applet {
Eye eyeOne, eyeTwo;
public void init() {
eyeOne = new Eye(75, 275, 55, 20);
eyeTwo = new Eye(275, 75, 40, 10);
Broker b = new Broker(eyeOne, eyeTwo, this);
this.addMouseMotionListener(b);
}
public void paint(Graphics g) {
eyeOne.draw(g);
eyeTwo.draw(g);
}
}
class Eye {
int locX, locY; // top-left corner of eye
int R; // size of the eye
int r; // size of pupil
int targetX, targetY; // what the eye is looking at
void draw(Graphics context) {
context.drawOval(locX, locY, 2 * R, 2 * R);
int xC = locX + R;
int yC = locY + R;
// targetX, targetY
double d = Math.sqrt( (targetX - xC) * (targetX - xC) +
(targetY - yC) * (targetY - yC)
);
double t = (R - r) / d;
int x = (int)(xC + t * (targetX - xC));
int y = (int)(yC + t * (targetY - yC));
context.fillOval(x - r, y - r, 2 * r, 2 * r);
context.drawLine(locX + R, locY + R, targetX, targetY);
}
void follow(int tX, int tY) {
targetX = tX;
targetY = tY;
}
Eye(int x, int y, int R, int r) {
locX = x; locY = y; this.R = R; this.r = r;
}
}
class Nose {
}
class Mouth {
}
class Broker implements MouseMotionListener {
Eye eno, owt; Applet telppa;
Broker(Eye one, Eye two, Applet a) {
this.eno = one;
this.owt = two;
this.telppa = a;
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
eno.follow(x, y);
owt.follow(x, y);
telppa.repaint();
}
public void mouseDragged(MouseEvent e) { }
}
Here also is the code from Thursday:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet {
Mouth m;
public void init() {
m = new Mouth();
Umpire u = new Umpire(m, this);
addMouseMotionListener(u);
addMouseListener(u);
}
public void paint(Graphics g) {
m.report();
}
}
class Umpire implements MouseMotionListener, MouseListener {
Mouth m;
Applet a;
Umpire(Mouth m, Applet a) { this.m = m; this.a = a;}
public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) {
m.smile = false;
a.repaint();
}
public void mouseExited(MouseEvent e) {
m.smile = true;
a.repaint();
}
}
class Mouth {
boolean smile = true;
void report() { System.out.println("Broad smile: " + smile); }
}