View
216
Download
0
Tags:
Embed Size (px)
Citation preview
©2004 Brooks/Cole
Applets
Graphics & GUIs
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
The HTML applet Tag
<html> <head> <title>My Applet</title> </head>
<body> <applet code="Einstein.class" width=350 height=175> </applet> </body></html>
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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()
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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.
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Graphics Methods
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Drawing a Line
X
Y
10
20
150
45
g.drawLine (10, 20, 150, 45);
g.drawLine (150, 45, 10, 20);or
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Drawing a Rectangle
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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;
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Drawing an Oval
X
Y
g.drawOval (175, 20, 50, 80);
175
20
50
80
boundingrectangle
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Drawing an Arc
g.drawArc (75, 20, 50, 80, 90, 90);
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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)
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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 drawingg.setColor(Color.blue);g.drawRect(50, 50, 100, 30);
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
A Typical Swing-Based Containment Hierarchy
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Figure 11.2: An Event “Triggers” the Initiation of an Event Object
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Various Registration
Configurations