Basic applets

advertisement
Lec 15
Writing an Applet Class
Agenda
• Writing an Applet class
• Java Graphics class
Two Ways to Use Java
• Applet—runs on the web
– uses graphical interfaces (usually)
– can't access files on local computer
– downloaded from website by browser
– no main method in class!
• Application—runs on a local computer
– has main method
– Can run "stand alone" on local computer
– can use graphical or nongraphical interfaces
Drawing on Computer
• Normal vs
Computer Graphics Coordinates
Simple Drawing Demo Applet
import java.applet.*; //so we can extend the Applet class
import java.awt.*; //so we can use graphics commands
public class DrawDemo extends Applet {
public void paint(Graphics g) {
//draws a blue oval on the screen
g.setColor(new Color(0, 0, 255));
g.fillRect(100,100,30,50);
g.setColor(new Color(0, 255, 0));
g.drawLine(115,125, 315,125);
g.setColor(new Color(255, 0, 0));
g.drawOval(275, 85, 80, 80);
}
}
Java Graphics Class
• http://download.oracle.com/javase/1.4.2/doc
s/api/java/awt/Graphics.html
Demo – drawing stick figure
Graph Paper can help
Lab15House
• Write an Applet that draws a house
• should at least have a window, a door, and a
roof.
• Use different colors and make it look good.
How to draw a filled Triangle?
• Arrays are lists of data
• Use an array of x and y values
– Hand them off to g.fillPolygon
int [] x = {100, 200, 300};
int [] y = {100, 500, 100};
g.fillPolygon(x,y,3);
Download