Find The Box By Nick
<< Video Poker by Mark | OldProjectsTrailIndex | Maze by Steve and Joe >>
DistractorBox.java
import java.awt.Graphics;
public class DistractorBox extends TargetBox {
public DistractorBox() {
super((int) (Math.random() * 450), (int) (Math.random() * 450));
}
public void draw(Graphics g) {
g.setColor(color);
g.drawRect(r.x, r.y, r.width, r.height);
}
}
TargetBoxTester.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
public class TargetBoxTester extends Applet implements MouseListener,MouseMotionListener
{
private int score = 0;
private String message = "Find The Filled Box!";
private TargetBox box;
private int counter = 100;
private int clickCounter;
private ArrayList<DistractorBox> distractors;
public void init() {
addMouseMotionListener(this);
resize(500, 500);
addMouseListener(this);
box = new TargetBox(450, 450);
makeDistractors();
}
public void makeDistractors() {
distractors = new ArrayList<DistractorBox>();
for (int i = 0; i < clickCounter*20; i++) {
distractors.add(new DistractorBox());
}
}
public void paint(Graphics g) {
g.setFont(new Font("Helvetica", Font.BOLD, 18));
g.drawString("clicks= " + clickCounter, 10, 500);
box.draw(g);
for (int i = 0; i < distractors.size(); i++) {
distractors.get(i).draw(g);
}
g.drawString(message + " " + score, 100, 500);
}
public Color randColor() {
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
return new Color(r, g, b);
}
public void mousePressed(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
box.contains(x, y) ;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (box.contains(e.getX(), e.getY())) {
message = " You Got It! ";
score += 10;
counter += 5;
} else {
message = "You couldn't find the box!";
score -= 10;
}
box = new TargetBox(450, 450);
makeDistractors();
clickCounter++;
repaint();
}
public void mouseDragged(MouseEvent e) {
}
}
TargetBox.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class TargetBox {
protected int width, height, x, y;
protected Color color;
protected Rectangle r;
protected boolean over;
public TargetBox(int w, int h) {
color = randColor();
width = 30 + (int) (Math.random() * 10);
height = 30 + (int) (Math.random() * 10);
x = (int) (Math.random() * 400);
y = (int) (Math.random() * 400);
r = new Rectangle(x, y, width, height);
over = false;
}
public boolean contains(int x, int y) {
if (r.contains(x, y)) {
over = true;
} else {
over = false;
}
return over;
}
public Color randColor() {
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
return new Color(r, g, b);
}
public void draw(Graphics g) {
g.setColor(color);
if (over) {
g.setColor(Color.WHITE);
}
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
}
}
