Q1.
Spoiler (Click to View)
Q2.
Spoiler (Click to View)
Spoiler (Click to View)
Code
/**
*
* This program should initially show a dot on the canvas, and
* enlarge the dot when a mouse grabs and drags it.
*
* Your tasks are to write three methods -- begin, onMousePress and onMouseDrag,
* as described before their method headers.
*
* You may need to define additional instance variables and constants.
*
* @author (write your name here please)
*/
import objectdraw.*;
import java.awt.*;
public class EnlargeMe extends WindowController
{
// this should be the initial width and height of your dot
private final int INITIAL_SIZE = 30;
private Location circleLoc;
private FilledOval circle;
private boolean check;
public void begin() {
circle = new FilledOval(200,200,30,30,canvas);
double circleX;
double circleY;
circleX = circle.getX();
circleY = circle.getY();
circleLoc = new Location(circleX,circleY);
}
public void onMousePress(Location point) {
if (circle.contains(point)){
check = true;
}
else{
check = false;
}
}
public void onMouseDrag(Location point) {
if (check = true){
//HELP
}
}
}Q2.
Spoiler (Click to View)
Code
/**
*
* This Voting program let's people vote for A or B.
* To vote, the user must DRAG the circle to either the left side or the right side.
* When the user releases the dragged circle:
* if the released point is to the left of the left line, it's a vote for A
* if the released point is to the right of the right line, it's a vote for B
* otherwise no vote is counted
*
* The voting circle must then return to it's starting point for the next vote!
*
* The two lines drawn will move left and right randomly plus or minus 20 pixels!
*
* Your tasks:
* - Add the functionality to the program to make the above voting system work.
*
*
* @author (write your name here please)
*/
import objectdraw.*;
import java.awt.*;
public class MovingTarget extends WindowController {
// x coordinates of A and B text messages
private static final int TEXT_A_X = 5;
private static final int TEXT_B_X = 225;
private static final int DISPLAY_Y = 220;
// Constants for circles
private static final int CIRCLE_RADIUS = 15;
private static final int CIRCLE_Y = 150;
private static final int CIRCLE_X = 150;
private Text infoA; // display of votes for A
private Text infoB; // display of votes for B
// leftX and rightX will hold the X-coordinates of the two lines.
private int leftX;
private int rightX;
// Set up the program
public void begin() {
// Create a circle to be used to vote
new FramedOval(CIRCLE_X, CIRCLE_Y,
CIRCLE_RADIUS, CIRCLE_RADIUS, canvas);
// Randomly move the lines left or right plus or minus 20 pixels
RandomIntGenerator rnd = new RandomIntGenerator(-20, 20);
leftX = 100 + rnd.nextValue();
rightX = 200 + rnd.nextValue();
// Draw the voting lines themselves
new Line(leftX, 0, leftX, canvas.getHeight() - 1, canvas);
new Line(rightX, 0, rightX, canvas.getHeight() - 1, canvas);
// Create info display boxes.
infoA = new Text( "Votes for A: " + 0,
TEXT_A_X, DISPLAY_Y, canvas );
infoB = new Text( "Votes for B: " + 0,
TEXT_B_X, DISPLAY_Y, canvas );
}
// WRITE YOUR METHODS HERE
}

