Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are."— Presentation transcript:

1 ©2004 Brooks/Cole Applets Graphics & GUIs

2 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are graphical The user controls what happens when by interacting with components like buttons and menus. Two types of graphical application in java –GUI application –Applet

3 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Applets 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 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

4 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The HTML applet Tag My Applet <applet code="Einstein.class" width=350 height=175>

5 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Applets The class that defines an applet extends the Applet class –The class inherits all the behavior of the Applet class –You can add whatever specific behavior you need for your applet An applet doesn't have a main method. Instead, there are several instance methods that serve specific purposes –public void paint( Graphics g) –public void init()

6 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphics Class Frames and Applets 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 is in the java.awt package. The state of a Graphics object includes such properties as height, width, foreground and background color and font. Positions within the Graphics context are measured from the upper left corner and they have units of pixels.

7 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphics Methods

8 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing a Line X Y 10 20 150 45 g.drawLine (10, 20, 150, 45); g.drawLine (150, 45, 10, 20); or

9 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing a Rectangle

10 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Bounding Rectangles How do we specify the size of a shape that isn't rectangular? A bounding rectangle is the smallest rectangle that completely surrounds the shape. 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;

11 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing an Oval X Y g.drawOval (175, 20, 50, 80); 175 20 50 80 bounding rectangle

12 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing an Arc An arc is a part of an oval To draw you need to specify –the bounding rectangle for the oval –the starting angle (measured from the x-axis in degrees –the angle the arc subtends

13 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing an Arc g.drawArc (75, 20, 50, 80, 90, 90);

14 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing a String The drawString method can be used to display text in a Graphics context. void drawString( String text, int x, int y; The position of the text relative to x and y is shown below

15 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 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)

16 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 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

17 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 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);

18 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 GUI Applications GUI applications usually start with a JFrame –Create a class that extends JFrame Each frame has a content pane which is where the GUI components go –Use a LayoutManager to control the orgranization of components The frame also has a MenuBar

19 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Typical Swing-Based Containment Hierarchy

20 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Control Flow in a GUI Application Components used to interact with the program generate Events Your program needs to listen for the events generated by the components –There are a number of different kinds of events –There are Listener interfaces for each type of event

21 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Figure 11.2: An Event “Triggers” the Initiation of an Event Object

22 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Various Registration Configurations


Download ppt "©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are."

Similar presentations


Ads by Google