470 likes | 626 Views
Chapter 4. Introduction to Applets and Graphics. Topics. Applet Structure Executing an Applet Drawing Shapes with Graphics Methods Using Colors. Applets. A Java application is a stand-alone program with a main method (like the ones we've seen so far)
E N D
Chapter 4 Introduction to Applets and Graphics
Topics • Applet Structure • Executing an Applet • Drawing Shapes with Graphics Methods • Using Colors
Applets • A Java application is a stand-alone program with a main method (like the ones we've seen so far) • A Java applet is a program that is intended to transported over the Web and executed using a web browser • An applet also can be executed using the appletviewer tool of the Java Software Development Kit • An applet doesn't have a main method • Instead, there are several special methods that serve specific purposes
Applets • Executed by a browser or applet viewer • Opens a window, as well as the Java console • Applet viewer comes with Java Software Development Kit (SDK)
Java Translation & Execution Java source code Across the Internet using HTML Java bytecode Java compiler Web browser Java interpreter Java interpreter Bytecode compiler Remote computer Local computer Machine code
Applet Structure • Do not use main method • Two methods called automatically: • init method • Browser calls init when applet starts • Use to initialize variables and objects • paint method • Browser calls after init and whenever window needs to be redrawn • Use to draw to screen
Example 4.01 ShellApplet.java import javax.swing.JApplet; import java.awt.Graphics; public class ShellApplet extends JApplet { // declare variables here public void init( ) { // initialize data here } public void paint( Graphics g ) { super.paint( g ); // include graphics code here } }
Executing an Applet • A Web page tells the browser to run the applet • HTML tags come in pairs, data goes between start and end tags <HTML> </HTML> start and end of HTML code <HEAD> </HEAD> start and end of header <TITLE> </TITLE> text to display in title bar <BODY> </BODY> start and end of page content
<Applet> Tag <APPLET> CODE = Classname.class CODEBASE = . directory of class file WIDTH = nnn width of window in pixels HEIGHT = nnn height of window in pixels </APPLET>
Minimal HTML File <HTML> <HEAD> <TITLE>TitleName</TITLE> </HEAD> <BODY> <APPLET CODE="ClassName.class" CODEBASE=. WIDTH=nnn HEIGHT=nnn> </APPLET> </BODY> </HTML>
HTML File for FirstApplet <HTML> <HEAD> <TITLE>My First Applet</TITLE> </HEAD> <BODY> <APPLET CODE="FirstApplet.class" CODEBASE=. WIDTH=400 HEIGHT=300> </APPLET> </BODY> </HTML>
Executing an Applet • If HTML file is named FirstApplet.html, you can execute the applet using this command: appletviewer FirstApplet.html • Many IDEs automatically create and launch the HTML file for running an applet
Applets in jGrasp • To run an applet in jGrasp, just click on the “apple” button • An applet viewer will automatically be opened
The 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 • Provides methods to draw shapes and text on the window
Drawing Shapes • Let's explore some of the methods of the Graphics class that draw shapes in more detail • A shape can be filled or unfilled, depending on which method is invoked • The method parameters specify coordinates and sizes • Recall that the Java coordinate system has the origin in the top left corner • Shapes with curves, like an oval, are usually drawn by specifying the shape’s bounding rectangle • An arc can be thought of as a section of an oval
Graphics Class Methods • Methods are available for drawing lines, rectangles, ovals, and other shapes, and for setting the current color • All methods have a void return type, so method calls are standalone statements • draw… methods draw an outlined share • fill… methods draw a solid shape
Displaying Text • Example: g.drawString( "Hello", x, y ); • See Example 4.4 DrawingTextApplet.java
Drawing a Line g.drawLine( xStart, yStart, xEnd, yEnd ); • See Example 4.5 LineDrawingApplet.java
X Y page.drawLine (10, 20, 150, 45); or page.drawLine (150, 45, 10, 20); Drawing a Line 10 150 20 45
Drawing A Rectangle g.drawRect( x, y, width, height );
X 40 100 Y Drawing a Rectangle 50 20 page.drawRect (50, 20, 100, 40);
Drawing A Solid Rectangle g.fillRect( x, y, width, height );
Drawing An Oval g.drawOval( x, y, width, height );
X 80 50 Y Drawing an Oval 175 20 bounding rectangle page.drawOval (175, 20, 50, 80);
Drawing A Solid Oval g.fillOval( x, y, width, height );
Drawing Squares and Circles • To draw a square, use drawRect or fillRect with equal values for width and height. • To draw a circle, use drawOval or fillOval with equal values for width and height
Example 4.6 ShapeDrawingApplet.java public void paint( Graphics g ) { super.paint( g ); g.drawRect( 100, 50, 40, 100 ); // rectangle g.fillRect( 200, 70, 80, 80 ); // solid square g.fillOval( 100, 50, 40, 100 ); // oval inside the rectangle g.drawOval( 100, 200, 100, 40 ); // same-size oval rotated 90 degrees int centerX = 250, centerY = 225; int radius = 25; g.drawOval( centerX - radius, centerY - radius, radius * 2, radius * 2 ); // circle using radius and center } }
When drawing a figure using Graphics methods, specify coordinate values as offsets from a starting (x,y) coordinate. • This will make your figure easier to move or resize.
Example 4.7 Astronaut.java import javax.swing.JApplet; import java.awt.Graphics; public class Astronaut extends JApplet { int sX, sY; // starting x and y coordinates public void init( ) { sX = 95; sY = 20; }
public void paint( Graphics g ) { super.paint( g ); // helmet g.drawOval( sX + 60, sY, 75, 75 ); g.drawOval( sX + 70, sY + 10, 55, 55 ); // face g.drawOval( sX + 83, sY + 27, 8, 8 ); g.drawOval( sX + 103, sY + 27, 8, 8 ); g.drawLine( sX + 97, sY + 35, sX + 99, sY + 43 ); g.drawLine( sX + 97, sY + 43, sX + 99, sY + 43 ); g.drawOval( sX + 90, sY + 48, 15, 6 ); // neck g.drawRect( sX + 88, sY + 70, 20, 10 ); // torso g.drawRect( sX + 65, sY + 80, 65, 85 );
// arms g.drawRect( sX, sY + 80, 65, 20 ); g.drawRect( sX + 130, sY + 80, 65, 20 ); // legs g.drawRect( sX + 70, sY + 165, 20, 80 ); g.drawRect( sX + 105, sY + 165, 20, 80 ); // flag g.drawLine( sX + 195, sY + 80, sX + 195 , sY ); g.drawRect( sX + 195, sY, 75, 45 ); g.drawRect( sX + 195, sY, 30, 25 ); // caption g.drawString( "One small step for man…", sX + 25, sY + 270 ); } }
Representing Color • A black and white picture can be stored using one bit per pixel (0 = white and 1 = black) • A colored picture requires more information; there are several techniques for representing colors • For example, every color can be represented as a mixture of the three additive primary colors Red, Green, and Blue (RGB system) • In Java, each color is represented by three numbers between 0 and 255 that collectively are called an RGB value (one byte per color stored)
Using Color • Every drawing surface has a background color • The Graphics context has a current foreground color • Both can be set explicitly • All drawing is done in current color; the current color is in effect until changed • The default color is black. • To use color, import the Color class from the java.awt package
Object Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow RGB Value 0, 0, 0 0, 0, 255 0, 255, 255 255, 200, 0 255, 255, 255 255, 255, 0 The Color Class • A color is defined in a Java program using an object created from the Color class • The Color class also contains several static predefined colors, including:
staticColor Constants Color.BLACK Color.GRAY Color.WHITE Color.ORANGE Color.RED Color.YELLOW Color.GREEN Color.PINK Color.BLUE Color.MAGENTA Color.CYAN Color.LIGHT_GRAY Color.DARK_GRAY
Setting the Current Color Example: g.setColor( Color.RED );
Custom Colors • Colors consist of red, green, and blue components (RGB). • Color constructor: • Example: Color green = new Color( 0, 255, 0 );
AstronautWithColor.java import javax.swing.JApplet; import javax.swing.JOptionPane; import java.awt.Graphics; import java.awt.Color; public class AstronautWithColor extends JApplet { int sX, sY; Color spacesuit; public void init( ) { spacesuit = new Color( 195, 175, 150 ); sX = Integer.parseInt( JOptionPane.showInputDialog ( null, "Enter the starting x position") ); sY = Integer.parseInt( JOptionPane.showInputDialog ( null, "Enter the starting y position") ); }
public void paint( Graphics g ) { super.paint( g ); // helmet g.setColor( spacesuit ); g.fillOval( sX + 60, sY, 75, 75 ); g.setColor( Color.LIGHT_GRAY ); g.fillOval( sX + 70, sY + 10, 55, 55 ); // face g.setColor( Color.DARK_GRAY ); g.drawOval( sX + 83, sY + 27, 8, 8 ); g.drawOval( sX + 103, sY + 27, 8, 8 ); g.drawLine( sX + 97, sY + 35, sX + 99, sY + 43 ); g.drawLine( sX + 97, sY + 43, sX + 99, sY + 43 ); g.drawOval( sX + 90, sY + 48, 15, 6 ); // neck g.setColor( spacesuit ); g.fillRect( sX + 88, sY + 70, 20, 10 );
// torso g.fillRect( sX + 65, sY + 80, 65, 85 ); // arms g.fillRect( sX, sY + 80, 65, 20 ); g.fillRect( sX + 130, sY + 80, 65, 20 ); // legs g.fillRect( sX + 70, sY + 165, 20, 80 ); g.fillRect( sX + 105, sY + 165, 20, 80 ); // flag g.setColor( Color.BLACK ); g.drawLine( sX + 195, sY + 80, sX + 195 , sY ); g.setColor( Color.RED ); g.fillRect( sX + 195, sY, 75, 45 ); g.setColor( Color.BLUE ); g.fillRect( sX + 195, sY, 30, 25 ); // caption g.setColor( Color.BLACK ); g.drawString( "One small step for man...", sX + 25, sY + 270 ); } }
Graphics class methods • void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) • void drawLine (int x1, int y1, int x2, int y2) • void drawOval (int x, int y, int width, int height) • void drawRect (int x, int y, int width, int height) • void drawString (String str, int x, int y) • void fillArc (int x, int y, int width, int height, int startAngle, int arcAngle) • void fillOval (int x, int y, int width, int height) • void fillRect (int x, int y, int width, int height) • Color getColor( ) • void setColor (Color color)
Applet Methods • In previous examples we've used the paint method of the Applet class to draw on an applet • The Applet class has several methods that are invoked automatically at certain points in an applet's life • The init method, for instance, is executed only once when the applet is initially loaded • The start and stop methods are called when the applet becomes active or inactive • The Applet class also contains other methods that generally assist in applet processing
Applet class methods • void init ( ) • void start ( ) • void stop ( ) • void destroy ( ) • URL getCodeBase ( ) • URL getDocumentBase ( ) • AudioClip getAudioClip (URL url, String name) • Image getImage (URL url, String name)