T - 1
Graphics
T - 2
Applet Class
Applets are Java programs that can be invoked in web
- pages. To run an applet, Java creates an instance of the
applet class. Certain methods run automatically:
init() start() stop() paint()
As with all methods, we can overwrite these for our own purposes.
T - 3
Using Applets
To use applets, use Java applet code:
import java.applet.*; import java.awt.*; // graphics library in Java Abstract Windows Toolkit
General form for a graphics drawing is:
public class GraphicsWorld extends Applet { public void paint(Graphics g) { // your drawing instructions here } // other methods } Represents a graphics object. It is provided by the applet. We will send our drawing commands to it.
T - 4
Coordinate System
(0,0) x y x and y are in pixels
T - 5
Basic Drawing Commands
public void drawLine(int x1, int y1, int x2, int y2) public void drawRect(int x, int y, int width, int height) public void fillRect(int x, int y, int width, int height) public void setColor(Color c) public void drawOval(int x, int y, int width, int height) public void fillOval(int x, int y, int width, int height)
T - 6
Basic Drawing Commands
public void drawPolygon(Polygon p)
Before we can draw the polygon, we must first create it!
Polygon tri = new Polygon(); tri.addPoint(50,50); tri.addPoint(75,50); tri.addPoint(50,75); g.drawPolygon(tri); g.fillPolygon(tri);