Useful+Methods

The following is a list of methods commonly used in Objectdraw, Greenfoot, BlueJ, or possibly all three. Feel free to add, make corrections, or improve the organization of this page.

yourVariable.getX; //returns the x coordinate of an object (Objectdraw)

yourVariable.getY;// returns the y coordinate of an object (Objectdraw)

yourVariable.getWidth; //returns the width of an object (Objectdraw//)

//yourVariable.getHeight;// returns height of an object

//yourVariable.setWidth(amount);// sets the width of your object to the given amount

//yourVariable.setHeight(amount);// sets the height of your object to the given amount

yourRand.nextValue; //assigns the next value for a random number//

yourObject.setColor( new Color ( #, #, #)); changes the color of an object to whatever you want using rgb

yourObject.setColor(Color.red);

yourVariable = scan.nextInt;

yourObject.move(x, y); //moves an object a set amount x and y units away from initial coordinates

yourObject.moveTo(x, y);// moves an object to new coordinates

getRotation;

setLocation(#, #);

setRotation(x);

delay; (greenfoot)

getRandomNumber; (greenfoot) //works the same way as random generator

boolean isKeyDown(String keyName); (greenfoot)

drawLine(x1, y1, x2, y2); (greenfoot)


 * Math Functions**//


 * 1) Math.sqrt(x) gives square root of value
 * 2) Math.pow(x, y) //this would be x to the power of y//
 * 3) Math.exp(x) e to the power of x
 * 4) Math.min(x, y) //finds the smaller value of the two//
 * 5) Math.max(x, y) finds the larger of the two values
 * 6) Math.sin(x) //finds the sine of x, when x is in radians//
 * 7) Math.cos(x)
 * 8) Math.tan(x)
 * 9) Math.toRadians(x) converts degrees to radians
 * 10) Math.toDegrees(x) //converts radians to degrees

code format="java5" // ************************************************************* //  Distance.java // //  Computes the distance between two points // *************************************************************

import java.util.Scanner; import java.util.Random;

public class Distance {   public static void main (String[] args) {   double x1, y1, x2, y2; // coordinates

Scanner scan = new Scanner(System.in); Random generator = new Random;

System.out.print ("Enter the X and Y coordinates of the first point; press enter in between coordinates."); x1 = scan.nextDouble; y1 = scan.nextDouble;

System.out.print ("Enter the X and Y coordinates of the second point: "); x2 = scan.nextDouble; y2 = scan.nextDouble;

// Compute the distance double sumOfSquares = Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2); double distance = Math.sqrt(sumOfSquares);

} }

code To drag and object in BlueJ with your cursor: code format="java" Location lastMouse;  //make sure this is under the public class //it creates the location of the cursor so you can //drag the object

public void onMousePress(Location point) {    lastMouse = point;      //it makes the point your mouse clicks the //the first point }

public void onMouseDrag(Location point) {       double changeX = point.getX - lastMouse.getX; double changeY = point.getY - lastMouse.getY; object.move(changeX,changeY); object2.move(changeX,changeY); lastMouse = point; }                         //it gets the x and y of the primary point your //cursor was at when you made the location //point and the x and y of the point your cursor //is at now. The changeX and Y are the //differences. The object moves the difference //and the point it is now is stored as the //primary point.

code