Chapter 5 Graphics
Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color
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 The paint method, for instance, is executed automatically and is used to draw the applet’s contents The paint method accepts a parameter that is an object of the Graphics class
Applets The class that defines an applet extends the Applet class An applet is embedded into an HTML file using a tag that references the bytecode file of the applet class The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser
The HTML applet Tag The Einstein Applet
Graphics Classes We introduce four standard classes related to drawing geometric shapes on a window: –java.awt.Graphics –java.awt.Color –java.awt.Point –java.awt.Dimension
Graphics Class Frames and windows have a graphics context associated with them. A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes We can draw geometric shapes on a frame window by calling appropriate methods of the java.awt.Graphics class.
Drawing a Line X Y page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or
drawRect How the position of the rectangle is determined by the drawRect method. g.drawRect(50, 50, 100, 30);
Graphics Methods
Drawing an Oval X Y g.drawOval (175, 20, 50, 80); bounding rectangle
Drawing an Arc An arc is a part of an oval To draw you need to specify –the bounding rectangle for the entire oval –the starting angle (measured from the x-axis in degrees –the angle the arc subtends g.drawArc (175, 20, 50, 80, 20, 90);
Color Class The java.awt.Color class allows us to designate the color of an object. The RGB scheme combines three values ranging from 0 to 255 for red, green, and blue. Color pinkColor; pinkColor = new Color(255,175,175)
RGB Color The Color class also contains several static predefined colors, including: Object Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow RGB Value 0, 0, 0 0, 0, 255 0, 255, , 200, 0 255, 255, , 255, 0
Color Class There are public class constants defined in the Color class for common colors: –Color.black –Color.blue –Color.green –Color.magenta –Color.orange
The Color Class Every drawing surface has a background color g.setBackground(Color.white); Every graphics context has a current foreground color –The most recently set foreground color is what is used for drawing g.setColor(Color.blue); g.drawRect(50, 50, 100, 30);
Point class A java.awt.Point object designates a point in two-dimensional space. The Point class has public instance variables To assign the position (10, 20) to a Point: Point pt = new Point(); pt.x = 10; pt.y = 20;
Dimension Class The java.awt.Dimension class can be used to create bounding rectangles that surround shapes. Dimension dim = new Dimension(); dim.width = 40; dim.height = 70; Bounding rectangles