|
CSCI A201/A597 and I210
|
Lab 13 practice problems in
QuizSite.
Please look at the problems listed in QuizSite (due Tuesday).
More will be posted next week, to help you with the final exam.
Now we do applets.
Let's start simple.
import java.applet.Applet;
import java.awt.Graphics;
public class Scribble extends Applet {
public void paint(Graphics g) {
g.drawString("Hi, there!", 30, 30);
}
}
You also need an HTML file, but that's a very basic one, always.
Let's draw a Circle.
import java.applet.Applet;
import java.awt.Graphics;
public class Scribble extends Applet {
Circle c = new Circle(40, 40, 40);
public void paint(Graphics g) {
c.draw(g);
}
}
class Circle {
int x, y, radius;
Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw(Graphics g) {
g.drawOval(x, y, radius, radius);
}
}
Do you see how this gets done? Now let's grab mouse events.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Scribble extends Applet {
Circle c = new Circle(40, 40, 40);
public void init() {
MouseSpy m = new MouseSpy();
addMouseListener(m);
addMouseMotionListener(m);
}
public void paint(Graphics g) {
c.draw(g);
}
}
class Circle {
int x, y, radius;
Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw(Graphics g) {
g.drawOval(x, y, radius, radius);
}
}
class MouseSpy implements MouseListener,
MouseMotionListener
{
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());
}
}
Now let's move the circle with the mouse.
We need to change mouseDragged.
But that method does not know about the circle.
So let's reorganize the code a bit to be able to handle that:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Scribble extends Applet implements MouseListener,
MouseMotionListener {
Circle c = new Circle(40, 40, 40);
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g) {
c.draw(g);
}
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());
}
}
class Circle {
int x, y, radius;
Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw(Graphics g) {
g.drawOval(x, y, radius, radius);
}
}
We just eliminated one type, using the applet as a listener. Now we can handle the movement of the circle.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Scribble extends Applet implements MouseListener,
MouseMotionListener {
Circle c = new Circle(40, 40, 20);
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g) {
c.draw(g);
}
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) {
int x = e.getX(), y = e.getY();
System.out.println("Mouse dragged at: " + x + " " + y);
if (c.contains(x, y)) {
c.moveTo(x - c.radius, y - c.radius);
c.draw(this.getGraphics());
}
}
public void mouseMoved (MouseEvent e) {
System.out.println("Mouse moved at: "
+ e.getX() + " " + e.getY());
}
}
class Circle {
int x, y, radius;
Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw(Graphics g) {
g.drawOval(x, y, 2 * radius, 2 * radius);
}
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
}
public boolean contains(int a, int b) {
int dist1 = a - (x + radius),
dist2 = b - (y + radius);
return Math.sqrt(dist1 * dist1 + dist2 * dist2) < radius;
}
}
This is a big program. Notice it has a flaw, but that can be fixed easily.
We'll start from this program next time.