Lecture X Swing and Graphics - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Lecture X Swing and Graphics

Description:

What can be done. Java 2D. Sounds. How to play them. Full Screen API ... Plays AIFF, WAV, MIDI, AU and RMF file types. Code is overly complex for what it does ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 29
Provided by: mikef78
Category:
Tags: aiff | graphics | lecture | swing

less

Transcript and Presenter's Notes

Title: Lecture X Swing and Graphics


1
G6DHLL High Level Languages
Lecture 17
Dr. Natalio Krasnogor Natalio.Krasnogor_at_Nottingham
.ac.uk
2
Today
  • Graphics
  • When / Why ?
  • Painting
  • What can be done
  • Java 2D
  • Sounds
  • How to play them
  • Full Screen API

3
Graphics A BIG topic
  • Could run a whole lecture course on how to use
    the various graphics capabilities that Swings
    APIs provide.
  • Use the Tutorials and API docs at
  • http//java.sun.com/docs/books/tutorial/uiswing/14
    painting/index.html
  • http//java.sun.com/docs/books/tutorial/2d/TOC.htm
    ldisplay

4
When do I need to use graphics? (1)
  • Avoid using graphics if possible
  • You may well only need
  • icons
  • labels and buttons
  • borders
  • styled text areas
  • JEditorPane
  • JTextPane

5
When do I need to use graphics? (2)
  • May need graphics if
  • Wish to drastically change apperance of an
    existing component
  • Draw shapes to screen
  • Draw text to screen
  • Animation
  • Invent your own component

6
Painting (1)
  • Component painting
  • repaint()
  • automatically called when necessary. Eg Window
    unhidden
  • revalidate()
  • automatically called before repaint when needed.
    Eg when component sizes or position have changed
  • Threads and painting
  • painting on the event-dispatching thread
  • repaint() and revalidate() are thread safe

7
Painting (2)
  • Double buffering
  • Painting performed to off screen buffer, then
    flushed to screen when finished.
  • Opaque components
  • Improves performance. Time not spent painting
    behind components.
  • Shape of painting areas
  • always rectangular
  • non-opaque (transparent) components can appear
    any shape, although need to use glass panes to
    mask hit detection area

8
Painting order
  • Visibility based on containment hierarchy
  • Background (if opaque)
  • Custom painting
  • Border
  • Child components
  • repaints
  • if transparent component repaints, all components
    under it must also repaint. This is costly,
    therefore use opaque when possible

9
What graphics tools are available in Swing?
  • Java AWT Graphics
  • Basic but usually enough
  • Shapes
  • Text
  • Images
  • Java 2D graphics
  • Extends the AWT graphics to provide much more, eg
    gradients, textures etc

10
Custom painting (1)
  • Occurs between background and border
  • Extend component and customise
  • JPanel recommended
  • e.g. class myPaintingPanel extends JPanel
  • can also use atomic components
  • e.g. class myPaintingButton extends JButton
  • Code goes in overridden paintComponent() method
  • public void paintComponent(Graphics g)

11
Custom painting (2)
  • When writing custom painting code
  • should not reproduce painting functionality of
    any other Swing component
  • Before doing anything else in paintComponent()
  • invoke super.paintComponent() or
  • setOpaque(false)

12
Painting coordinates
  • Component sizes in pixels
  • (0,0) at top left corner
  • (width-1,height-1) at bottom right corner
  • Border sizes (e.g. a 1-pixel border)
  • (1,1) at top left
  • (width-2, height-2) at bottom right
  • General painting area for b pixel border
  • b,b w-(b1), h-(b1)

13
Getting painting coordinates
  • Use component getWidth and getHeight
  • Get border sizes with getInsets
  • e.g. to get width and height of custom painting
    area use something like
  • Insets insets getInsets()
  • int currentWidth getWidth() -
    insets.left - insets.right
  • int currentHeight getHeight() - insets.top
    - insets.bottom

14
Repainting custom paints
  • Calling repaint() method requests a repaint to
    occur
  • repaint()
  • paints whole component any others that need it
    if transparent
  • repaint(int leftx,int lefty,int width,int
    height)
  • only repaints a specified area
  • Component painted after all pending events
    dispatched.
  • Painting should be kept short - Done in event
    thread - slows GUI

15
Graphics - shapes and text (1)
  • Properties stored in Graphics object.
  • Represents state
  • eg Color, Font, etc
  • Methods of Graphics can draw...
  • Shapes
  • lines, rectangles, 3D rectangles, round-edged
    rectangles, ovals, arcs, polygons
  • Text
  • Draw string to screen as a graphic

16
Graphics - shapes and text (2)
  • Drawing a shape on screen
  • Use paintComponent Graphics
  • void paintComponent(Graphics g)
  • super(g)
  • g.setColor(Color.RED)
  • g.drawRect(x, y, width, height)
  • shapes x and y specify upper left

17
Graphics - shapes and text (3)
  • Drawing a string on screen
  • Use paintComponent Graphics again
  • void paintComponent(Graphics g)
  • super(g)
  • g.setFont(new Font(Serif, Font.PLAIN,
    12))
  • g.drawSring(Java Swing is _at_!, x, y)
  • x and y specify baseline left of text

18
Graphics - Images
  • Swing Icons by far and away the easiest method of
    displaying graphics.
  • If more features needed then use the AWT/Graphics
    functionality
  • Supports GIF, PNG and JPEG
  • Loading and displaying images
  • Image myImage Toolkit.getDefaultToolkit().g
    etImage(filename)g.drawImage(myImage, x, y,
    this)g.drawImage(myImage, x, y, width,
    height,this)

19
Tracking Images
  • MediaTracker class
  • utility class used to track the status of a
    number of images for a particular component.
  • Can get information about any errors and
    associated images
  • ImageObserver interface
  • implemented by all components
  • can get use to get notifications about loading
    progress of image as its constructed.

20
Java2D API (Graphics2D)
  • Provides enhanced image, graphics and text
    capabilities
  • Done through extensions to AWT Graphics API
  • Amongst others provides
  • Hit detection on Shapes text and images
  • Complex Printing Support
  • Texture rendering
  • Complex Shapes/lines

21
Java2D Shapes
  • Rectangle Shapes
  • QuadCurve2D and CubicCurve2D
  • GeneralPath
  • Arbitary shapes made by connecting lines

22
Java 2D - Text
  • Often easier to use JLabels since much of the
    work is done for you
  • Use the drawString method
  • Eg g.drawString(Hello, x, y)
  • To set font use setFont method
  • Create an instance of Font

23
Java 2D - Images
  • Much more powerful than basic image display
  • Images are best rendered in a Buffer off screen
    as a BufferedImage
  • Then drawn to screen with call to
    Graphics2D.drawImage
  • Graphics2D provides variety of image filtering
    options
  • sharpen/blur, rotate, transform, scale etc

24
Java 2D (VERY) Basic use
  • Simply cast the Graphics object to Graphics2D
  • void paintComponent(Graphics g)
  • super(g)
  • Graphics g2 (Graphics2D)g.create()
  • g2.setColor(Color.BLACK)
  • g2.drawRect(x, y, width, height)
  • g2.dispose()

25
Sounds in Java (1)
  • Java sound API under development
  • In meantime must use poor alternative
  • Need methods from java.applet.Applet
  • Can play multiple sound files at same time
  • result is mixed to form a composite output
  • Plays AIFF, WAV, MIDI, AU and RMF file types
  • Code is overly complex for what it does
  • inherited from applets hence URL etc...

26
Sounds in Java (2)
  • Following example plays a sound file sound.wav
  • URL soundURL new URL(file//path-to- file/
    sound.wav)
  • AudioClip audioClip Applet.newAudioCli
    p(soundURL)
  • //then some combination of...
  • audioClip.start() //play once
  • audioClip.loop() //loop clip till stop
  • audioClip.stop() //stop clip playing

27
The Full Screen API
  • Full Name
  • Full Screen Exclusive API
  • New to JDK 1.4
  • Allows programmer to suspend windowing system and
    draw directly to the screen
  • Good for games, slide shows and dedicated
    terminal applications
  • Not covered here, but excellent tutorial found
    here
  • http//java.sun.com/docs/books/tutorial/extra/full
    screen/index.html

28
Summary
  • Swing ves
  • Swings good because its Platform Independent
  • Swing makes it easy to knock up simple GUIs
  • Swing ves
  • Swing is not always as Platform Independent as it
    claims
  • Can get confusing when GUIs get BIG
  • Java - its not the fastest language ever...
  • When youve finished the cwk youll hate it and
    use JBuilder or some other (better) IDE instead.
Write a Comment
User Comments (0)
About PowerShow.com