Using Graphics in Java - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Using Graphics in Java

Description:

Using Graphics in Java. CSE301. University of Sunderland. Harry R ... Java Examples in a Nutshell, ... Foreground color superseded by fill style attribute and ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 21
Provided by: harry66
Category:

less

Transcript and Presenter's Notes

Title: Using Graphics in Java


1
Using 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
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.

4
Topics
  • Gotchas in Java graphics
  • Graphics in Swing
  • Graphics2D
  • Image Processing
  • Image I/O
  • Animation

5
Gotchas in Java graphics
  • Dont use both the fill and draw methods for a
    shape unless you need the outline and interior to
    be different colours.
  • Override the paint() or paintComponent() (in
    Swing) method to draw anything.

6
Graphics in Swing
  • Basically similar to AWT, but
  • Override paintComponent(Graphics g) instead of
    paint(Graphics g). This handles interaction with
    borders and similar graphical elements better.
  • The first thing paintComponent(Graphics g) should
    do is call super.paintComponent(g)
  • Note this is different from paint(Graphics g),
    which should not call super.paint(g)

7
Graphics2D
  • See Knudsen J. Java 2D Graphics, OReilly.
  • Graphics2D is a direct subclass of Graphics.
  • Your paint(Graphics g) and paintComponent(Graphics
    g) are called with a Graphics2D object.
  • Hence, it is convenient to typecast g to a
    Graphics2D object early in your method
  • Graphics2D g2 (Graphics2D)g
  • That allows you to call Graphics2D methods when
    you need them.

8
Important Graphics2D Attributes
  • Foreground colorsuperseded by fill style
    attribute and Paint interface.
  • Clipping regiona Shape, not restricted to
    rectangles.
  • Line stylea Stroke. Allows you to control line
    attributes.
  • Fill stylea Paint object. A Color, TexturePaint,
    or GradientPaint
  • Compositingcombines pixel colours
  • Transformcoordinate system (defined by
    translate(), scale(), rotate(), and shear()
    methods)
  • Hintsantialiasing and other preferences

9
Important Graphics2D Methods
  • draw()outlines an arbitrary Shape
  • fill()fills an arbitrary Shape
  • hit()detects whether a rectangle intersects an
    arbitrary Shape
  • drawString()
  • drawGlyphVector()
  • drawImage(), drawRenderableImage(),
    drawRenderedImage()

10
The Paint Interface
  • Any of the following
  • A Color (solid colour)
  • A TexturePaint (a tiled image)
  • A GradientPaint (a colour gradient)
  • TexturePaint and GradientPaint objects are
    immutable.

11
The Shape Interface
  • A shape
  • Does not have to enclose an area. Can represent a
    path.
  • Fundamental Shape-related methods in Graphics2D
  • draw()
  • fill()
  • clip()
  • hit()

12
Shape Implementations
  • Rectangle (three)
  • Round rectangle (two)
  • Ellipse/circle (two)
  • Polygon
  • Line segment (two)
  • Arc (ellipse segment, two kinds)
  • Bezier curve (four)

13
Stroke
  • Describes the pen or brush used to draw.
  • BasicStroke is usually suitable
  • Lots of details.

14
Fonts and Text
  • You can add fonts to the standard ones.
  • Provides a method to load a TrueType font by
    name.
  • Test for the existence of the font before you try
    to load it.
  • You can define attributes for the font and
    transform it.
  • You can also provide rendering hints.
  • You can define text strings as GlyphVectors that
    can be drawn repeatedly.

15
BufferedImage
  • A subclass of Image.
  • Represents image data in memory (an Image is data
    being transferred over a network).
  • Image processing of a BufferedImage is powerful
    and sophisticated.
  • In Swing applications, load an image as an
    ImageIcon.
  • Image theImage new javax.swing.ImageIcon(theFil
    eName).getImage()

16
Sample Code
  • private BufferedImage the32x32png
  • File the32x32File new File("32x32.png")
  • the32x32png ImageIO.read(the32x32File)
  • ImageIcon the32Symbol new ImageIcon(((BufferedI
    mage)the32x32png).getSubimage(32,0,32,32))

17
More on BufferedImage
  • To create an empty one, call createImage() on a
    component. Cast the returned object as a
    BufferedImage
  • Use createGraphics() on it to get a Graphics2D
    object.
  • Use the Graphics2D object to draw data from an
    Image into the BufferedImage
  • Use the com.sun.image.codec.jpeg package for
    reading jpeg data into a BufferedImage and for
    encoding a BufferedImage as a jpeg.

18
Image Processing
  • BufferedImage supports image processing. Use the
    filter() method.
  • Look at the java.awt.image package.
  • Operations supported include
  • Arbitrary geometric transformations
  • Colour conversions
  • Convolution
  • Lookup table operations in colour space
  • Rescaling in colour space

19
Animation
  • Use a background thread to drive the animation.
  • To avoid flickering, use double buffering.
  • Draw the next frame off-screen, then
  • Copy the frame onto the screen all at once.
  • Try to avoid redrawing the entire frame, too.
  • Note, swing does this automatically.

20
Example Code
  • Java Examples in a Nutshell, chapter 12, example
    19
Write a Comment
User Comments (0)
About PowerShow.com