Title: Java Methods
1Java Graphics
2Objectives
- Learn about the Java GUI interface.
3Java Graphics
- GUI Graphical User Interface
- AWT Abstract Windowing Toolkit (A collection of
GUI components implemented using native-platform
versions of the components) - Swing GUI components implemented without using
native code. Swing components have uniform
behavior on all platforms. Swing components are
slower than AWT components.
4Heavy Weight Components
5Frame
A Frame is a top-level window with a title and a
border.
6Window
- A Window object is a top-level window with no
borders and no menubar. - A window must have either a frame, dialog, or
another window defined as its owner when its
constructed.
7JFrame
A JFrame is an extended version of the Frame
class that adds support for the JFC/Swing
component architecture.
8JWindow
- A JWindow object is a top-level window with no
borders and no menubar. - A window must have either a frame, dialog, or
another window defined as its owner when its
constructed.
9(No Transcript)
10(No Transcript)
11Light Weight Components
12JPanel
A JPanel is a generic light weight component. It
can be used as a container to hold other light
weight components or as a drawing surface.
13(No Transcript)
14Inheritance
15Inheritance
- Inheritance is used extensively in Java graphics.
- Java components are intended to be extended and
customized.
16Top Down Design
17Java Graphics
- Start JCreator.
- Create a new file called Lab20.java.
- Save the new file in your Lab20 folder.
18Java Graphics
- import java.awt.
- import javax.swing.
- public class Lab20 extends JFrame
-
- public static void main(String args)
- Lab20 lab new Lab20()
- lab.setVisible(true)
-
-
- Lab20()
- setTitle(Boop Boop A Doop)
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
-
19Java Graphics
20Java Graphics
- Drag The Borders To Resize The Window
21Content Panes
22Content Panes
- JFrame class has several layers (panes) root,
content, layered, and glass. Programs normally
reference only the content pane. - There are two programming idioms for using the
content pane (1) using the pre-assigned pane, or
(2) building your own pane.
23Content Panes
- Idiom 1 Use the existing content pane
-
- Every frame (or window) has a preconstructed
content pane of class Container. You can get a
reference to this pane and make changes to it. -
- Container content getContentPane() // Use the
default content pane. - content.setBackground(Color.BLUE)
- All JFrames already have a content pane, so
there's no need to create a new one, just get the
existing pane. And if you're wondering about the
Container type, it's a superclass of JPanel. In
fact, if you look at the actual type of the
object that's currently returned by
getContentPane(), it actually is a JPanel.
24Content Panes
- Idiom 2 Create your own content pane
-
- It's common to create a new panel for the content
pane and tell the window to use this new panel
for its content pane. - JPanel content new JPanel() // Create a new
content pane. - content.setBackground(Color.BLUE)
- setContentPane(content)
25Java Graphics
- public class Lab20 extends JFrame
-
- private MyPanel drawingArea new MyPanel()
- private class MyPanel extends JPanel
-
- public MyPanel()
- setPreferredSize(new Dimension(400, 300))
- setBackground(Color.BLUE)
-
-
- public static void main(String args)
- Lab 20 lab new Lab20()
- lab.setVisible(true)
-
-
- Lab20()
- setTitle(Boop Boop A Doop)
Nested Class Only nested classes can have private
access.
26Java Graphics
27Graphics Class
- The Graphics class is the abstract base class for
all graphics contexts that allow an application
to draw onto components as well as onto
off-screen images.
28Drawing Basic Shapes
g.drawLine (x1, y1, x2, y2) g.clearRect
(x, y, w, h) g.drawRect (x, y, w, h)
g.fillRect (x, y, w, h) g.drawRoundRect (x,
y, w, h, horzD, vertD) g.fillRoundRect (x,
y, w, h, horzD, vertD) g.drawOval (x, y, w,
h) g.fillOval (x, y, w, h) g.drawArc
(x, y, w, h, fromDegr, measureDegrs)
29Basic Shapes (contd)
g.drawPolygon (xCoords, yCoords, nPoints)
g.fillPolygon (xCoords, yCoords, nPoints)
g.drawPolyline (xCoords, yCoords, nPoints)
g.drawString (str, x, y) g.drawImage (img,
x, y, this)
abc
?
ImageObserver, often this
30Coordinate System
31Coordinates
(0, 0)
x
Origin the upper-left corner of the component
Units pixels
y
y-axis points down, as in many other graphics
packages
32Coordinates (contd)
- A GUI component provides getWidth and getHeight
methods that return its respective dimension. - These methods can be used to produce scalable
graphics. - getWidth and getHeight only work after the
component has been placed (do not call them from
a components constructor).
33Coordinates (contd)
- The position of a rectangle, oval, and even an
arc is defined by using its bounding rectangle,
described by x, y, width, height
x, y
34Drawing On A JComponent
35paintComponent Method
- When a window becomes visible (uncovered or
deminimized) or is resized, the "system"
automatically calls the paintComponent() method
for all areas of the screen that have to be
redrawn. - The paintComponent() method paints the component.
If this method is reimplemented,
super.paintComponent() should be called so that
lightweight components are properly rendered. - The paintComponent() method should never be
called directly. Instead, call the repaint()
method.
36paintComponent Method
- private class MyPanel extends JPanel
-
- public MyPanel()
- setBackground(Color.BLUE)
- setPreferredSize(new Dimension(400, 300))
-
- public void paintComponent(Graphics g)
-
-
-
-
37paintComponent Method
If we want the panel use the established
attributes like background color, we need to call
super.paintComponent.
38paintComponent Method
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
-
-
-
39paintComponent Method
40Drawing Basic Shapes
41Drawing Rectangles
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.drawRect(10, 10, 100, 100)
-
-
42Drawing Rectangles
43Drawing Rectangles
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.fillRect(10, 10, 100, 100)
-
-
44Drawing Rectangles
45Drawing Ovals
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.fillRect(10, 10, 100, 100)
- g.setColor(Color.YELLOW)
- g.fillOval(120, 60, 100, 100)
-
-
46Drawing Ovals
47Drawing Polygons
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.fillRect(10, 10, 100, 100)
- g.setColor(Color.YELLOW)
- g.fillOval(120, 60, 100, 100)
- g.setColor(Color.RED)
- int x 30, 70, 90, 50
- int y 30, 30, 90, 90
- g.fillPolygon(x, y, 4)
-
-
48Drawing Polygons
49Drawing Polygons
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.fillRect(10, 10, 100, 100)
- g.setColor(Color.YELLOW)
- g.fillOval(120, 60, 100, 100)
- g.setColor(Color.RED)
- int x 30, 70, 50, 90
- int y 30, 30, 90, 90
- g.fillPolygon(x, y, 4)
-
-
50Drawing Polygons
51Color Class
52Color Class
- The Color class is used to encapsulate colors in
the default RGB color space. - Every color has an implicit alpha value of 1.0 or
an explicit one provided in the constructor. - The alpha value defines the transparency of a
color and can be represented by a float value in
the range 0.0 - 1.0 or 0 - 255. - An alpha value of 1.0 or 255 means that the color
is completely opaque and an alpha value of 0 or
0.0 means that the color is completely
transparent.
53Color Class
- The color attribute is set by calling g.setColor
and stays in effect until changed - You can form a color with specified red, green,
and blue (RGB) values
g.setColor(Color.BLUE) g.draw...
g.draw... g.setColor(Color.LIGHT_GRAY)
...
int rVal 5, gVal 255, bVal 40 Color
yourEyes new Color (rVal, gVal, bVal)
54Color Class
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.fillRect(10, 10, 100, 100)
- g.setColor(Color.YELLOW)
- g.fillOval(120, 60, 100, 100)
- g.setColor(Color.RED)
- int x 30, 70, 50, 90
- int y 30, 30, 90, 90
- g.fillPolygon(x, y, 4)
- g.setColor(new Color(255, 200, 200, 128))
- g.fillRect(50, 50, 120, 120)
-
-
55Color Class
56Drawing Images
57Drawing Images
- The Graphics class has a drawImage method that
will draw an image on the graphics context at a
specified location. - You have been provided with a Utility class that
contains a static loadImage method. This method
will load the specified image file. The file must
be located in a subfolder called resources.
58Java Graphics
- public class Lab20 extends JFrame
-
- private MyPanel drawingArea new MyPanel()
- private Image betty Utility.loadImage(betty.jp
g) - private class MyPanel extends JPanel
-
- public MyPanel()
- setPreferredSize(new Dimension(400, 300))
- setBackground(Color.BLUE)
-
-
59Drawing Images
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
- g.drawRect(10, 10, 100, 100)
- g.setColor(Color.YELLOW)
- g.fillOval(120, 60, 100, 100)
- g.setColor(Color.RED)
- int x 30, 70, 50, 90
- int y 30, 30, 90, 90
- g.fillPolygon(x, y, 4)
- g.setColor(new Color(255, 200, 200, 128))
- g.fillRect(50, 50, 120, 120)
- g.drawImage(betty, 100, 100, null)
-
-
60Drawing Images
61Drawing Text
- The Graphics class has a drawString method that
will draw text on the graphics context at a
specified location.
62Drawing Text (cont)
Font font new Font (name, style, size)
g.setFont (font)
abc "Serif" abc
"SansSerif" abc "Monospaced"
int (pixels)
Font.PLAIN Font.BOLD Font.ITALIC
63Drawing Text (cont)
- public void paintComponent(Graphics g)
-
- super.paintComponent(g)
- g.setColor(Color.WHITE)
-
- g.fillRect(50, 50, 120, 120)
- g.drawImage(king, 100, 100, null)
- g.setColor(Color.RED)
- g.setFont(new Font(Serif, Font.PLAIN, 24))
- g.drawString(Boop Boop A Doop, 100, 50)
-
-
X Position (baseline)
Y Position (baseline)
64Drawing Text (cont)
65Setting The Icon Image
66Setting The Icon Image
- Lab20()
- setTitle(Boop Boop A Doop)
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
- setContentPane(new MyPanel())
- setIconImage(Utility.loadImage(icon.gif))
- pack()
-
67Setting The Icon Image
Icon
68The AudioClip class
69The AudioClip class
- IMPORT STATEMENT
- import java.applet.
- DEFINE AN INSTANCE FIELD
- private AudioClip clip Utility.loadAudio("m
usic.wav") - PLAY THE SOUND
- clip.play() // Plays the sound one time
- OR
- clip.loop() // Plays the sound
continuously - STOP THE SOUND
- clip.stop()
70Playing Sounds
- public class Lab20 extends JFrame
-
- private MyPanel drawingArea new MyPanel()
- private Image betty Utility.loadImage(betty.jp
g) - private AudioClip music Utility.loadAudio(musi
c.wav) -
- private class MyPanel extends JPanel
-
- public MyPanel()
- setPreferredSize(new Dimension(400, 300))
- setBackground(Color.BLUE)
-
-
71Playing Sounds (cont)
-
- Lab20()
- setTitle(Boop Boop A Doop)
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
- setContentPane(drawingArea)
- setIconImage(Utility.loadImage(icon.gif))
- music.play()
- pack()
-
72Playing Sounds (cont)
73Questions?
Helen Kane
74Begin Lab 20