Java Methods - PowerPoint PPT Presentation

1 / 74
About This Presentation
Title:

Java Methods

Description:

Container content = getContentPane(); // Use the default content pane. content.setBackground(Color.BLUE); All JFrames already have a content pane, ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 75
Provided by: MariaL214
Category:
Tags: java | jframes | methods

less

Transcript and Presenter's Notes

Title: Java Methods


1
Java Graphics
2
Objectives
  • Learn about the Java GUI interface.

3
Java 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.

4
Heavy Weight Components
5
Frame
A Frame is a top-level window with a title and a
border.
6
Window
  • 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.

7
JFrame
A JFrame is an extended version of the Frame
class that adds support for the JFC/Swing
component architecture.
8
JWindow
  • 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)
11
Light Weight Components
12
JPanel
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)
14
Inheritance
15
Inheritance
  • Inheritance is used extensively in Java graphics.
  • Java components are intended to be extended and
    customized.

16
Top Down Design
17
Java Graphics
  • Start JCreator.
  • Create a new file called Lab20.java.
  • Save the new file in your Lab20 folder.

18
Java 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)

19
Java Graphics
20
Java Graphics
  • Drag The Borders To Resize The Window

21
Content Panes
22
Content 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.

23
Content 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.

24
Content 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)

25
Java 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.


26
Java Graphics
27
Graphics 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.

28
Drawing 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)
29
Basic 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
30
Coordinate System
31
Coordinates
(0, 0)
x
Origin the upper-left corner of the component
Units pixels
y
y-axis points down, as in many other graphics
packages
32
Coordinates (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).

33
Coordinates (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
34
Drawing On A JComponent
35
paintComponent 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.

36
paintComponent Method
  • private class MyPanel extends JPanel
  • public MyPanel()
  • setBackground(Color.BLUE)
  • setPreferredSize(new Dimension(400, 300))
  • public void paintComponent(Graphics g)


37
paintComponent Method
If we want the panel use the established
attributes like background color, we need to call
super.paintComponent.
38
paintComponent Method
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)

39
paintComponent Method
40
Drawing Basic Shapes
41
Drawing Rectangles
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • g.setColor(Color.WHITE)
  • g.drawRect(10, 10, 100, 100)


42
Drawing Rectangles
43
Drawing Rectangles
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • g.setColor(Color.WHITE)
  • g.fillRect(10, 10, 100, 100)

44
Drawing Rectangles
45
Drawing 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)


46
Drawing Ovals
47
Drawing 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)


48
Drawing Polygons
49
Drawing 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)

50
Drawing Polygons
51
Color Class
52
Color 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.

53
Color 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)
54
Color 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)


55
Color Class
56
Drawing Images
57
Drawing 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.

58
Java 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)


59
Drawing 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)


60
Drawing Images
61
Drawing Text
  • The Graphics class has a drawString method that
    will draw text on the graphics context at a
    specified location.

62
Drawing 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
63
Drawing 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)
64
Drawing Text (cont)
65
Setting The Icon Image
66
Setting The Icon Image
  • Lab20()
  • setTitle(Boop Boop A Doop)
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • setContentPane(new MyPanel())
  • setIconImage(Utility.loadImage(icon.gif))
  • pack()


67
Setting The Icon Image
Icon
68
The AudioClip class
69
The 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()

70
Playing 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)


71
Playing Sounds (cont)
  • Lab20()
  • setTitle(Boop Boop A Doop)
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • setContentPane(drawingArea)
  • setIconImage(Utility.loadImage(icon.gif))
  • music.play()
  • pack()

72
Playing Sounds (cont)
73
Questions?
Helen Kane
74
Begin Lab 20
Write a Comment
User Comments (0)
About PowerShow.com