Creating Graphics in Java - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Creating Graphics in Java

Description:

Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 19
Provided by: HarryE155
Category:

less

Transcript and Presenter's Notes

Title: Creating Graphics in Java


1
Creating Graphics in Java
  • CSE301
  • University of Sunderland
  • Harry R Erwin, PhD

2
Resources
  • Flanagan, David (2000). Java Foundation Classes
    in a Nutshell, OReilly.
  • Darwin, Ian F. (2004). Java Cookbook, OReilly.
  • Flanagan, David (2004). Java Examples in a
    Nutshell, OReilly.

3
Java GUI Frameworks
  • Abstract Window Toolkit (java.awt)
  • The original set of windowing components designed
    for Java. These were implemented as separate
    processes using the GUI features of the
    underlying operating system.
  • Swing (or Java Foundation Classesjavax.swing)
  • A second generation set of light-weight
    windowing components introduced in Java 1.2.
    These were implemented as Java threads and so
    avoided the overhead of process context switches
    and enforced a common look and feel.
  • SWT (part of Eclipse)
  • Discussed at http//java.sun.com/docs/books/tutor
    ial/uiswing/index.html

4
Goals
  • The goal of these two lectures is to provide the
    student an understanding of graphics in Java.
  • GUI1 explores the Java 2D API, the basic graphics
    primitives.
  • GUI2 explores image processing, image I/O, and
    animation.

5
Review of AWT
  • The foundation of GUI in Java
  • Implemented in separate processes
  • Rudimentary
  • Supported by most web browsers, so that most
    applets should use AWT instead of Swing.

6
AWT Graphics Features
  • Basic facilities for creating a graphical user
    interface.
  • Drawing graphics
  • java.awt.Graphics
  • java.awt.Color
  • java.awt.Font
  • Java 2D API (added in Java 1.2)
  • java.awt.Graphics2D

7
Using AWT
  • Import
  • java.awt.
  • java.awt.event.
  • Everything inherits from java.awt.Component,
    including graphics.
  • The AWT GUI is organized by containers,
    inheriting from java.awt.Container (which
    inherits from Component).
  • The arrangement of controls is managed by various
    layout managers.
  • Notification of changes is handled by events and
    event listeners that run in a GUI thread.

8
Graphics Before Java 1.2
  • Demonstrated in GraphicsSampler.java from Java
    Examples in a Nutshell (library).
  • init() is called to initialise color resources.
  • paint() is called every time the object needs to
    be redrawn, for example when the applet is
    covered by another window and the window moves or
    disappears.

9
The Graphics Object
  • Used for all graphics operations prior to 1.2
  • Represents a drawing surface. Can be used to draw
    on a Component, Image, or printer.
  • Keeps track of the current state.
  • Provides attributes and methods for drawing.

10
java.awt.Graphics
  • An interface.
  • Cannot be created directly. You must
  • use getGraphics() on a Component
  • use getGraphics() on an image or PrintJob
  • copy from an existing Graphics object using
    create().
  • When no longer needed, call dispose() on it to
    free up the system resources it uses.

11
Graphics Attributes
  • Color, the current Color object
  • Font, the current Font object
  • Clipping region, the Rectangle to be painted
  • Origin, a point
  • Paint mode, a boolean
  • Background color, a Color

12
Graphics Operations
  • drawLine() (1 pixel)
  • drawPolyLine() (polygon)
  • drawArc()
  • drawOval()
  • drawPolygon()
  • drawRect()
  • drawRoundRect()
  • draw3DRect()
  • fillArc()
  • fillOval()
  • fillPolygon() (polygon)
  • fillRect()
  • fillRoundRect()
  • fill3DRect()
  • drawBytes() (byte)
  • drawChars() (char)

13
More Graphics Operations
  • drawString()
  • drawImage()
  • various versions, requiring an ImageObserver
    object
  • clearRect()
  • various setters and getters
  • copyArea()
  • clipRect()
  • translate()
  • hitClip()
  • translate()

14
java.awt.Font
  • Represents a font. The constructor is passed a
    name, a style (PLAIN, BOLD, ITALIC, BOLDITALIC),
    and a font size.
  • Names include
  • serif
  • sansserif
  • monospaced
  • Dialog
  • DialogInput
  • Symbol

15
java.awt.Color
  • Represents colors.
  • Can represent a color coded in RGB space defined
    from integers or floating point numbers.
  • Has a static method that allows you to create
    colors in the HSB color space.
  • Provides some standard colors as constants, such
    as Color.black or Color.white.
  • java.awt.SystemColor is a subclass with constants
    for various colors used on the system desktop.

16
java.awt.Rectangle
  • Defines a rectangle using the x and y coordinates
    of the upper left corner and the width and
    height. These four values are public and can be
    manipulated directly.
  • Used to define rectangular regions, usually in an
    image being manipulated.
  • Can be used for other purposes.

17
java.awt.Point
  • A concrete subclass of java.awt.geom.Point2D
  • A Point is defined as two integers, x and y.
    These are public fields and can be manipulated
    directly.
  • equals() is overridden so two points are equal if
    they have the same x and y values.
  • hashCode() doesnt appear to be overridden, so
    dont use a Point as an index in a HashMap with
    the expectation that it will be a value class.

18
This Weeks Exercise
  • Access http//www.oreilly.com/catalog/jenut3
  • Download the sample code and create an Eclipse
    project for GraphicsSampler.java (an Applet)
  • Run the Applet in the debugger so you can observe
    how it works.
Write a Comment
User Comments (0)
About PowerShow.com