Graphics - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Graphics

Description:

Random is the class used to generate pseudo-random numbers. ... Anonymous Inner Class. An anonymous inner class is a class declared inside of another class without a ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 16
Provided by: rob1125
Category:
Tags: anon | graphics

less

Transcript and Presenter's Notes

Title: Graphics


1
Graphics
  • Robert Salkin
  • CSI 205 OOP using Java
  • Summer 2005, 6W1

2
Quick Review
  • Random is the class used to generate
    pseudo-random numbers.
  • Random objects should be seeded with a different
    number each time the program is run in order to
    get a greater level of randomness.
  • The nextInt() method should be passed a maximum
    integer one greater than the actual maximum
    number that should be returned.

3
Lightweight vs. Heavyweight
  • Lightweight GUI components dont have direct
    interaction with the system.
  • They should appear the same anywhere.
  • Swing was written to add lightweight GUI
    components.
  • GUI components starting with J are mostly
    lightweight, however some might have underlying
    heavyweight interaction.
  • Heavyweight GUI components have some direct
    interaction with the system.
  • These components can have problems on some
    systems.
  • AWT components are heavyweight.

4
Component
  • The class Component is a direct or indirect
    superclass of most GUI components.
  • A GUI component is a discrete GUI object, like an
    instantiation of JButton, JPanel, or JTextField.

5
Container
  • The class Container is a superclass of GUI
    components that can hold other GUI components.
  • Example classes that are normally used as
    containers are JPanel and JFrame.
  • A Container object is a Component object, so
    anywhere a Component object may be used, a
    Container object may be used.

6
X Y
  • Shapes are drawn using X and Y coordinates.
  • X goes from 0 to greater than 0 from left to
    right.
  • Y goes from 0 to greater than 0 from top to
    bottom (not bottom to top as in mathematics).

7
Graphics
  • A Graphics object manages Javas ability to draw
    on the screen in a graphics context.
  • Graphics is an abstract class, therefore only
    subclasses may be instantiated.

8
Color
  • A Color object is required when a non-default
    color is desired.
  • Color objects are set to a specific color by
    passing their Red, Green, and Blue (or RGB)
    components to the constructor.
  • Methods are available to set the color directly.
  • Alternatively, public static final Color objects
    are available for use, like Color.RED or
    Color.LIGHT_GRAY.

9
Paint and Repaint
  • GUI components need to be drawn on the screen
    using a Graphics object referencing the
    appropriate graphics context.
  • The paintComponent method is overridden to draw
    objects on the screen.
  • public void paintComponent(Graphics g)
  • Always call super.paintComponent(g)
  • The repaint() method is calle when the programmer
    wants objects to be updated on the screen.

10
Graphics Methods
  • setColor(Color c)
  • Sets the current color.
  • fillRect(int x, int y, int width, int height)
  • Draws a filled in rectangle.
  • drawString(String str, int x, int y)
  • Draws a line of text.
  • setBackground(Color c)
  • Sets the background color of a JComponent.
  • NOT part of Graphics, so use the JComponent
    reference.
  • Many others

11
Anonymous Inner Class
  • An anonymous inner class is a class declared
    inside of another class without a name and
    usually appears in a method call relating to a
    listener.

12
Adapter Class
  • An adapter class implements an interface using
    empty methods.
  • An adapter is useful when only certain methods of
    a class are important to an implementation.

13
MouseListener
  • A MouseListener listens for MouseEvents similar
    to the way an ActionListener listens for
    ActionEvents on GUI components.
  • The MouseAdapter class is an adapter that is
    useful to use when only one or a few mouse events
    are relevant to the program.
  • When motion of the mouse matters,
    MouseMotionListener and MouseMotionAdapter may be
    used.

14
MouseEvent Methods
  • getPoint()
  • Gets a Point representing the X and Y position of
    the mouse.
  • getX()
  • Gets the X coordinate of the position of the
    mouse.
  • getY()
  • Gets the Y coordinate of the position of the
    mouse.

15
Problem
  • How do all of these classes work together?
  • Example
Write a Comment
User Comments (0)
About PowerShow.com