Running an Applet |
|
|
We need to use html code to run applets. The minimum html required to run applet with a browser (java host) is as follows: TitleName CODE = Classname.class CODEBASE = . directory of class file WIDTH = 50 width of window in pixels HEIGHT = 50 height of window in pixels Note that in the applet tag you include the. class bytecode file and not the .java. Graphics Class - Browser or appletviewer sends a Graphics object to the paint method. - The Graphics object represents the applet window, current font, and current color and provides methods to draw shapes (rectangles, triangles, circles, etc.) and text on the window. The Graphics Class Coordinate System - Never use 0,0 because the applet itself uses it for the title bar. Graphics class methods - No return type so they are all void - Draw ()… methods draw an outlined share - Fill ()… methods draw a solid shapeDisplaying text use g.drawString( "string", x, y); Need to draw the strings. - Draw a lin g.drawLine( xStart, yStart, xEnd, yEnd ); - How do you draw a square? You need to make multiple lines that attach at the points. Drawing a rectangle, Use pixels for height and width and an anchoring corner. Drawing an oval, drawn inside an invisible rectangle. Give rectangle coordinates. Graphics methods, Use offsets to make your figure easier to move resize. import javax.swing.JApplet; import java.awt.Graphics; public class ShapesApplet extends JApplet { public void paint( Graphics g ) { super.paint( g ); g.drawRect( 900, 40, 30, 900 ); g.fillRect( 200, 70, 80, 80 ); g.fillOval( 100, 50, 40, 100 ); g.drawOval( 100, 200, 100, 40 ); int X = 250, Y = 225; int r = 25; g.drawOval( X - r, Y - r, r * 2, r * 2 ); } } - Draw a rectangle g.drawRect( x, y, width, height ); - Draw a solid recta ngle: g.fillRect( x, y, width, height ); - Draw am oval: g.drawOval( x, y, width, height ); |
|
|