Computer Science 252
Problem Solving with Java
Spring 2016, The College of Saint Rose
DoNotEnter BlueJ Project
Click here to download a BlueJ project for DoNotEnter.
DoNotEnter Source Code
The Java source code for DoNotEnter is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
/*
* Example DoNotEnter: manipulate some "Do Not Enter" signs
*
* Jim Teresco, The College of Saint Rose, CSC 252, Spring 2014
*
* $Id: DoNotEnter.java 2431 2014-09-19 02:14:45Z terescoj $
*/
public class DoNotEnter extends WindowController {
private DoNotEnterSign sign1, sign2;
private boolean draggingSign1, draggingSign2;
private Location lastMouse;
public void begin() {
sign1 = new DoNotEnterSign(new Location(100, 100), canvas);
sign2 = new DoNotEnterSign(new Location(300, 150), canvas);
}
public void onMousePress(Location point) {
lastMouse = point;
draggingSign1 = sign1.contains(point);
draggingSign2 = sign2.contains(point);
}
public void onMouseRelease(Location point) {
if (draggingSign1) {
sign1.move(point.getX() - lastMouse.getX(),
point.getY() - lastMouse.getY());
}
if (draggingSign2) {
sign2.move(point.getX() - lastMouse.getX(),
point.getY() - lastMouse.getY());
}
}
public void onMouseMove(Location point) {
}
public void onMouseDrag(Location point) {
if (draggingSign1) {
sign1.move(point.getX() - lastMouse.getX(),
point.getY() - lastMouse.getY());
}
if (draggingSign2) {
sign2.move(point.getX() - lastMouse.getX(),
point.getY() - lastMouse.getY());
}
lastMouse = point;
}
public void onMouseEnter(Location point) {
sign1.showCircle();
sign2.showCircle();
}
public void onMouseExit(Location point) {
sign1.hideCircle();
sign2.hideCircle();
}
}
/**
* A sign that can be "do not enter" (symbolic) or blank, also
* supports operations needed for dragging.
*
* @author Jim Teresco
*/
import objectdraw.*;
import java.awt.*;
public class DoNotEnterSign {
// some named constants for dimensions
private static final double SIGN_SIZE = 80;
private static final double CIRCLE_INSET = 5;
private static final double CIRCLE_SIZE = SIGN_SIZE - (CIRCLE_INSET * 2);
private static final double BAR_INSET = 10;
private static final double BAR_HEIGHT = 10;
private static final double BAR_WIDTH = SIGN_SIZE - (BAR_INSET * 2);
private static final double POST_HEIGHT = 120;
private static final double POST_WIDTH = 5;
// sign components
private FilledRect background;
private FramedRect border;
private FilledOval redCircle;
private FilledRect whiteBar;
private FilledRect post;
public DoNotEnterSign(Location signBase, DrawingCanvas c) {
// signBase is the point at the base of the sign where the post meets,
// so some extra work is needed to compute positions
background = new FilledRect(signBase.getX() - SIGN_SIZE/2, signBase.getY() - SIGN_SIZE,
SIGN_SIZE, SIGN_SIZE, c);
background.setColor(Color.white);
border = new FramedRect(signBase.getX() - SIGN_SIZE/2, signBase.getY() - SIGN_SIZE,
SIGN_SIZE, SIGN_SIZE, c);
redCircle = new FilledOval(signBase.getX() - SIGN_SIZE/2 + CIRCLE_INSET,
signBase.getY() - SIGN_SIZE + CIRCLE_INSET,
CIRCLE_SIZE, CIRCLE_SIZE, c);
redCircle.setColor(Color.red);
whiteBar = new FilledRect(signBase.getX() - SIGN_SIZE/2 + BAR_INSET,
signBase.getY() - SIGN_SIZE/2 - BAR_HEIGHT/2,
BAR_WIDTH, BAR_HEIGHT, c);
whiteBar.setColor(Color.white);
post = new FilledRect(signBase.getX() - POST_WIDTH/2, signBase.getY(),
POST_WIDTH, POST_HEIGHT, c);
post.setColor(Color.gray);
}
// we need to be able to move
public void move(double dx, double dy) {
background.move(dx, dy);
border.move(dx, dy);
redCircle.move(dx, dy);
whiteBar.move(dx, dy);
post.move(dx, dy);
}
// we need to be able to answer the contains question
public boolean contains(Location p) {
return border.contains(p) || post.contains(p);
}
// show the (possibly hidden) circle part of the sign
public void showCircle() {
redCircle.show();
}
// hide the circle part of the sign
public void hideCircle() {
redCircle.hide();
}
}