3rd Quarter Notes
Applet
Object draw
objectdraw.jar <==add to compiler
jdk/jre/lib/exe
import the following
import objectdraw.*
import java.awt.* abstract windowing toolkit
extends WindowController
Basic Shapes
Rectangle: -FilledRect and FramedRect
oval: -FilledOval and FramedOval
line: -Line
text: -Text
new FilledRect(x, y, width, hight, canvas(face of applet));
drawable area
import objectdraw.*
import java.awt*
public class ClassName exteds WindowController
{
public coid begin()
{
method for initializing the applet
}
}
Making your own color
Color myColor = new Color(R, G, B);
Mouse Methods
Location Point
class object
-stores the current location of the mouse cursor
point.getX
canvas.clear()
Location Class
Location start = new Location(0,0);
Location finish = new Location(100, 200); or instead or 100 canvas.getWidth()
new Line (start, finish, canvas);
Color Class
Methods
.setBlue(int amount)
.setRed()
.serGreen()
Magic number
-any number that appears in a line of code, withour a variable
-Use a variable to represent all values.
Canvas
canvas.clear()
Methods of a Text Object
void setText(String newText)
void setFontSize(int pointSize)
void setBold()
void setItalic()
void setPlain()
void setFont(Font someFont)
void setFont(String font)
Methods for all Drawable objects
void setColor(Color someColor)
void moveTo(double x, double y)
void moveTo(Location some Location)
void hide()
void show()
void removeFromCanvas()
void sentToBack()
void send ToFront()
I. Designing a Class, for use with an Applet
- include Drawing Canvas canvas as a constructor parameter
- store all constants for creating the objects as private final instance variables
(no magic numbers)
- create methods , similar to methods of drawing objects (move(), contains(), hidden()..)
* You do not need extends WindowControler in the class file.
Ex:
Face
public class Face
{
private final int FACE_HEIGHT = 60,
FACE_WIDTH = 60,
EYE_OFFSET = 20,
EYE_RADIUS = 8,
MOUTH_HEIGHT = 10,
MOUTH_WIDTH = FACE_WIDTH /2;
private FramedOval head,
leftEye,
rightEye,
mouth;
public Face(double left, double top, DrawingCanvas canvas)
{
head = new FramedOval(left, top, FACE_WIDTH, FACE_HEIGHT, canvas);
mouth = new FramedOval(left+(FACE_WIDTH/4), top+2*FACE_HEIGHT/3,
MOUTH_WIDTH, MOUTH_HEIGHT, canvas);
leftEye=
RightEye=
}
}
I contains Method
inside of face class
public boolean contains (Location point)
{
if (head.contains(point))
return true;
else
return false;
}
or
public boolean contains (Location point)
{
return head.contains(point);
}
II. MoveTo() method
public void moveTo(Location point)
{
move(pointgetX()-head.getx(), point.getY()-head.getY()
}
III. getColor method
public Color getColor()
{
return head.getColor();
}
I. Over riding the equals() method
Ex
if (str.equals("Bob"))
boolean method
- determines if two objects are equal (comparing relevant private instance variables)
if (str == "Bob")
for objects will compare memory locations
only for primitives
Object has an equals() method
- need to over ride it
equals() method for car
public boolean equals(Car otherCar)
{
return otherCar.getHeight==this.height &&
otherCar.getWidth == this.width &&
otherCar.getColor.equals(this.Color);
from color class
}
I. Loading an Image
1) Create an Applet
2) Import java.awt.Image;
3) Use the Image class
private Image pic1;
begin()
{
pic1 = getImage(" ");
place file in the classes directory
}
4) Placeing Image object onto the canvas:
private VisivleImage thePic;
//get Image
thePic = new VisibleImage(pic1, x, y, canvas);
object of image
5)Re-size using methods of Visible Image
I. Active Objects
-Outline of an active object
public class ClassName extends ActiveObject
{
//constructor
public ClassName(formal parameters)
{
//set private instance variables
start(); //all active have start() at the end of the constructor
triggers the run() method
}
public void run()
{
while(some condition)
{
object.move(#, #);
pause(#);
}
}
}
II. Example
public class movingBox extends ActiveObject
{
public movingBox(double x, double y, DrawingCanvas canvas)
{
box = newFilledRect(x, y, 50, 40, canvas);
XCoor = X;
YCoor = Y;
bWidth = 50;
bHeight = 40;
bColor = black;
canvasWidth = canvas.getWidth();
start();
}
public void run()
{
while(box.getX
~~~~Home~~~~