Lab Examples - makawan - Dec 12, 2016
Q1.
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.
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
}
* - Stealthâ„¢ - Dec 12, 2016
Code if (check = true){
//HELP
}
Oh boy. What is this for?
* - makawan - Dec 12, 2016
It is where you put the code to enlarge the circle. If the point is in the circle then it will execute the code below check=true
* - av00 - Dec 12, 2016
(Dec 12, 2016 12:26 PM)makawan Wrote: It is where you put the code to enlarge the circle. If the point is in the circle then it will execute the code below check=true
= is for assignment, use == for comparison.
But it's best to only leave the boolean check in your if statement.
if (check) would already execute if check were true, because you would have if (true) and viceversa it would not if check is false.
* - makawan - Dec 12, 2016
Oh i c. Thanks for the help. Any ideas to resize the selected circle?
* - JavaGuy - Dec 12, 2016
Quote: This program should initially show a dot on the canvas, and enlarge the dot when a mouse grabs and drags it.
Okay, not so hard I see. Lets tackle it thinking in logical steps. I see The code to draw the dot is already given to you (or you have already completed it). Nice.
You have a method setting a boolean to either true or false. Since the Code onMousePress(Location point)
method is only called when you press the mouse, this check will only set to true when you press the mouse and it's inside the dot. Nice.
Lets look at this next part without any knowledge of the onMouseDrag (since I don't know which library this mouse controller is coming from specifically).
First step, find what Location is being passed in through that argument "point". You can either print out some variables from point (probably and x and y) or just print out the object itself:
Code System.out.println(point.toString())
or
Code System.out.println(point.getX() + " " + point.getY())
Okay, so I'm assuming this point passed in is the current position of your mouse when you have the button pressed down.
First off, the "=" operator is an assignment while "==" is conditional, so change:
Code if (check = true) {}
to orCode if (check == true) {}
To change the size of the dot, we want to change its radius. Following a simple rule, we can say as the mouse is being dragged away from the center, we should increase the radius.
To do this, have global (class) variables storing the initial X and Y (center) location of the original dot. As the user drags their mouse, find the X and Y position of the mouse (through the point argument) and calculate the distance (distance formula) and set that distance to the radius of the circle.
|