





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Friday, November 30, 2007
Applet graphics 21-
When an applet is created, a main method isnot invoked.
Applet graphics 21-
import java.awt.; import javax.swing.; // for JApplet public class Check extends JApplet { public void paint (Graphics g) { g.setColor(Color.red); g.drawLine(0, 200, 200, 400); g.setColor(Color.blue); g.drawLine(200, 400, 400, 0); } }**
Check.java
Graphics object provided by Applet to which we send all drawing commands
Imagine ‘g’ is a painting machine. You’re telling it what to paint and where. It does all the work.
Check.class Check.html Check.java
Applet graphics 21-
**
**
Check.html
origin (0, 0)
positive x
positive y
Applet window (0,200)
(200,400)
(400,400)
Applet graphics 21-
fillRect (int x, int y, int width, int height)
Applet window
(0,0)
(200,100)
y
x
g.setColor(Color.blue); g.fillRect(200,100,60,200);
200
60
Applet graphics 21-
drawOval (int x, int y, int width, int height)
Applet window
(0,0)
(200,100)
y
x
g.setColor(Color.red); g.drawOval(200,100,60,200);
200
60
Applet graphics 21-
fillOval (int x, int y, int width, int height)
Applet window
(0,0)
(200,100)
y
x
g.setColor(Color.red); g.fillOval(200,100,60,200);
200
60
Applet graphics 21-
drawArc (int x, int y, int width, int height,
int startAngle, int arcAngle)
Applet window
(0,0)
(200,100)
y
x
g.setColor(Color.magenta); g.drawArc(200,100,60,200,0,270);
200
60
Applet graphics 21-
drawString (String str, int x, int y)
Applet window
(0,0)
(100,100)
y
x
g.setColor(Color.darkGray); g.drawString(“There’s no place like home!”,100,100);
There’s no place like home!
Applet graphics 21-
The Polygon class x
y
o A member of the Polygon class has multiple vertices. o We create a new Polygon with, you guessed it, a constructor, then add vertices. o Polygons don’t appear until asked.
Polygon anna = new Polygon(); anna.addPoint(0,400); anna.addPoint(200,400); anna.addPoint(200,200); g.drawPolygon(anna); g.fillPolygon(anna);
(0,0)
Creates an empty Poly
and Poly gets the point
(200,400)
(200,200)
(0,400)
Applet graphics 21-
g.setColor(Color.red); g.drawOval(100,100,50,50); // Head Polygon hat = new Polygon(); hat.addPoint(100,100); hat.addPoint(125,70); hat.addPoint(150,100); g.fillPolygon(hat); g.drawString("If I only had a brain", 80, 60); g.fillRect(100,150,50,80); // Body g.fillRect(105,230,15,50); // Leg g.fillRect(130,230,15,50); // Leg g.fillRect(75,175,100,20); // Arms
The Tin Man
Applet graphics 21-
We don’t need no stinkin’ browser!