HTML Links Tutorials http://www.htmlcodetutorial.com/ http://www.w3.org/markup/guide/ Quick Reference http://werbach.com/barebones/barebones.html Applets A Java application is a stand-alone program with a main method A Java applet is a Java program that is intended to transported over the web and executed using a web browser
Applets An applet doesn't have a main method Instead, there are several special methods that serve specific purposes paint method -- automatically executed and is used to draw the applets contents When we write applets, we need to include a couple of import statements: import java.applet.applet; import java.awt.*; Applets An applet is embedded into an HTML file using a tag that references the bytecode file (nameofclass.class) of the applet class It is actually the bytecode version of the program that is transported across the web The applet is executed by a Java interpreter that is part of the browser
Applet Methods Several methods from Applet class that are invoked automatically at certain points in an applet's life init - executed only once when the applet is initially loaded start - called when applet becomes active (when browser loads / returns to the page) stop - called when applet becomes inactive (when browser leaves the page) paint - automatically executed and is used to draw the applets contents Applet Methods The Applet class also contains other methods that generally assist in applet processing getcodebase getdocumentbase getaudioclip getimage
Applets The paint method accepts a parameter that is an object of the Graphics class A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes The class that defines the applet extends the Applet class we can use all of the methods in the Applet class as if they were in the class that defines our applet Applets and the Web Basic HTML file for an applet: <html> <body> <applet code = "ClassName.class"> </applet> </body> </html> Can also specify size of applet window <applet code="classname.class" height=200 width=300> </applet> Put the applet HTML file (named something.html) and your Java applet bytecode (named ClassName.class) in your public_html folder.
The Color Class A color is defined in a Java program using an object created from the Color class (defined in the java.awt package) contains several static predefined colors Every graphics context has a current foreground color Every drawing surface has a background color Representing Color A color picture requires more information every color can be represented as a mixture of the three primary colors red, green, and blue In Java, each color is represented by three numbers between 0 and 255 that are collectively called an RGB value 1 byte (8 bits) to store the contribution of each color (red, green, blue) Carolina Blue: R 172 G 194 B 227
Drawing Shapes Graphics class is defined in the java.awt package drawarc drawline drawoval drawrect drawstring fillarc filloval fillrect getcolor setcolor Drawing Shapes A shape can be filled or unfilled, depending on which method is invoked The method parameters specify coordinates and sizes Many shapes with curves, like an oval, are drawn by specifying its bounding rectangle An arc can be thought of as a section of an oval
Coordinate Systems Each pixel can be identified using a two-dimensional coordinate system In Java, we use a coordinate system with the origin in the upper left corner (0, 0) 112 X 40 (112, 40) Y Drawing a Line 10 150 X 20 45 page is a Graphics object Y page.drawline (10, 20, 150, 45); or page.drawline (150, 45, 10, 20);
Drawing a Rectangle 50 X 20 40 100 Y page.drawrect (50, 20, 100, 40); Drawing an Oval 175 X 20 80 Y bounding rectangle 50 page.drawoval (175, 20, 50, 80); page.drawarc (175, 20, 50, 80, 0, 90);
Want to Learn More? Creating a GUI with Swing http://java.sun.com/docs/books/tutorial/uiswing/ Creating Applets http://java.sun.com/docs/books/tutorial/applet/