Transcript ppt
Creating an Object that can draw itself
The paint method can ‘draw’ because it is passed a
graphics environment as a parameter.
If a class method is passed a graphics environment (ie.
object of type Graphics or Graphics2D), then the
method can draw also.
• eg.) public void xxx (Graphics g){
•
g.drawRect(10,10, 200,150);
•
}
Suppose we have a class Dog…
and we want to provide one more behavior………
we want to be able to draw a Dog object.
The draw method will need a Graphics environment if
it is going to draw………….
File DogApplet.java
import java.applet.Applet;
import java.awt.Graphics;
//An applet that draws two Dogs
public class DogApplet extends Applet
{
public void paint(Graphics g){
Dog myDog = new Dog(“Spot”,“beagle”);
myDog.setAge(10);
Dog yourDog = new Dog(“Rufus”);
myDog.draw(g);
yourDog.draw(g);
}
File Dog..java
import java.awt.Graphics;
public class Dog{
//regular class constructors and methods
public void draw(Graphics g) {
int x = 50;
int y = 50; //draw start position
g.drawOval(x,y,100,50);
g.drawOval(x+12,y+12,5,5); //eyes
g.drawOval(x+25,y+12, 5,5);
g.fillOval(x+16,y+25, 10,10); //nose
g.fillOval(x+62,y+13,50,75); //left ear
}
what happens when we draw 3 dogs???
File Dog..java
import java.awt.Graphics;
public class Dog{
private int x; //drawing position
private int y;
public void draw(Graphics g) {
g.drawOval(x,y,100,50);
g.drawOval(x+12,y+12,5,5); //eyes
g.drawOval(x+25,y+12, 5,5);
g.fillOval(x+16,y+25, 10,10); //nose
g.fillOval(x+62,y+13,50,75); //left ear
}
Add position to the class data.
how does this position get initialized???
import java.awt.Graphics;
public class Dog{
// other instance variables
private int x; //drawing position
private int y;
public Dog(String nm, int xpos, int ypos){
nm = name;
x = xpos;
y = ypos;
}
public void draw(Graphics g) {
….
}
Constructor for Dog accepts start position for drawing ……
Updated …File DogApplet.java
import java.applet.Applet;
import java.awt.Graphics;
//An applet that draws two Dogs
public class DogApplet extends Applet
{
public void paint(Graphics g){
Dog myDog = new Dog(“Spot”,“beagle”, 100,100);
myDog.setAge(10);
Dog yourDog = new Dog(“Rufus”, 300,50);
myDog.draw(g);
yourDog.draw(g);
}
look at implementation…………..
DogN.java, DogAppN.java………
What if our draw method needed to be more
elaborate, and use Ellipse2D.Double, or
Rectangle2D.Double??
2 solutions………
Typical methods which can be overridden in Applet..
void paint (Graphics g) -- we know this
void init() – called once by the applet container
when the applet is loaded for execution
Typical actions are to initialize fields, create
GUI components, load sounds and images
Typical methods which can be overridden in Applet..
void start() – called after init. If the browser visits another
website and returns, method start is called again. This
method performs any tasks that must be performed when
the applet is ‘restarted’
Typical methods which can be overridden in Applet..
void stop() - called when the applet should stop executing –
usually when the user of the browser leaves the page.
Any tasks that are required to suspend the applet’s
execution are done here
Typical methods which can be overridden in Applet..
void destroy() - called when the applet is being removed from
memory , usually when browser close. Any tasks needed to
release resources are done here.