Part 1 to building our own Java Swing Components - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Part 1 to building our own Java Swing Components

Description:

Heavy weight vs. light weight is a well-known difference between AWT & Swing. A lot more ... Marks & text decorating line of speed in different directions ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 30
Provided by: georgep3
Category:

less

Transcript and Presenter's Notes

Title: Part 1 to building our own Java Swing Components


1
Drawing with Swing
  • Part 1 to building our own Java Swing Components

2
Agenda
  • Questions
  • Build a SWING component today!
  • Drawing basics
  • Damage management

3
What are common components
  • Very similar to those you have in Java AWT
  • Window ? JWindow
  • Frame ? JFrame
  • Component ? JComponent
  • Panel ? JPanel
  • Button ? JButton
  • Label ? JLabel

4
But different right?
  • Heavy weight vs. light weight is a well-known
    difference between AWT Swing
  • A lot more components available in Swing
  • Very subtle differences in how you would build
    your own components
  • (good if familiar with AWT)
  • http//java.sun.com/products/jfc/tsc/articles/pain
    ting/index.html

5
Swing
  • Lots of very nice features in toolkit
    architecture that we like for this class, such
    as
  • MVC
  • z-ordering

6
Building a Soft Jog Dial
7
How to build your own Swing Component
  • Define a class that extends JComponent
  • Define the size
  • Define its look (paint method)
  • http//java.sun.com/docs/books/tutorial/uiswing/pa
    inting/index.html

8
Define class
  • import java.awt.
  • import javax.swing.
  • class JogDial
  • extends JComponent
  • // variables methods

9
Define dimensions
  • Parent will often determine how much space each
    sub component gets
  • But components should tell its parent how much
    space it needs in a layout.

10
Define dimensions
  • public Dimension getPreferredSize()
  • return(getMinimumSize())
  • public Dimension getMinimumSize()
  • return(new Dimension(400,150))

11
Local coordinate system
  • Each component has its own coordinate system
    (0,0) - (width-1, height-1)

12
Define look
  • Methods involved
  • public void paintBorder(Graphics g)
  • public void paintChildren(Graphics g)
  • public void paintComponent(Graphics g)

13
What happened to the paint method?
  • Its still there
  • Will call the other three paint methods
  • Not recommended that you override it

14
The paintBorder method
  • Paints the components border
  • Not recommended that you override it
  • If this is usedit consumes some of the size of
    the component

15
The paintChildren method
  • Paints the components children
  • Not recommended that you override it

16
The paintComponent method
  • Paints the component
  • This is the method you should override

17
Painting shapes
  • Methods supported by Graphics object for painting
    shapes
  • Lines (drawLine)
  • Rectangles (drawRect and fillRect)
  • Raised or lowered rectangles (draw3DRect and
    fill3DRect)
  • Round-edged rectangles (drawRoundRect and
    fillRoundRect)
  • Ovals (drawOval and fillOval)
  • Arcs (drawArc and fillArc)
  • Polygons (drawPolygon, drawPolyline, and
    fillPolygon)
  • http//java.sun.com/docs/books/tutorial/uiswing/pa
    inting/drawingShapes.html

18
Painting text
  • Methods supported by Graphics object for painting
    text
  • String (drawString)
  • http//java.sun.com/docs/books/tutorial/uiswing/pa
    inting/drawingText.html

19
Painting text
  • Methods supported by Graphics object for painting
    text
  • String (drawString)
  • http//java.sun.com/docs/books/tutorial/uiswing/pa
    inting/drawingText.html

20
Java 2D
  • Additional methods for drawing.
  • Draw lines of varying thickness
  • Fill shapes with gradients textures
  • Move, rotate, scale, shear text graphics
  • Composite overlapping text graphics
  • Anti-aliasing
  • http//java.sun.com/docs/books/tutorial/2d/index.h
    tml

21
Java 2D
  • import java.awt.geom.
  • public void paintComponent(Graphics g)
  • Graphics2D g2 (Graphics2D) g
  • //// paints component

22
Fonts FontMetrics
  • Font font new Font(Helvetica,Font.BOLD,14)
  • FontMetrics metrics g2.getFontMetrics(font)
  • Or
  • FontMetrics metrics component.getFontMetrics(fon
    t)

23
FontMetrics methods
  • int getAscent(), int getMaxAscent()
  • int getDescent(), int getMaxDescent()
  • int getHeight()
  • int getLeading()
  • int getMaxAdvance()
  • int bytesWidth(byte, int, int)
  • int charWidth(int), int charWidth(char)
  • int charsWidth(char, int, int)
  • int stringWidth(String)
  • int getWidths()

24
Lets draw the JogDial
  • Line
  • Bead
  • Marks text decorating line of speed in
    different directions
  • (supports configurable max speed up down)

25
Damage management
  • Now that the JogDial is drawnlets make it more
    lively
  • Adding a thread that moves the bead a pixel at a
    time to the right until it hits the far right and
    then snaps back to the center

26
Damage management
  • Just painting is a problem
  • Parts of the drawn output is damagedmust be
    redrawn

27
Damage management
  • What happens when we try to repaint the entire
    component each time? Why not do this?
  • Itd be nice if we could do something else other
    than just painting

28
Damage management
  • Repaint only the damaged area

29
Next time
  • Handling input
Write a Comment
User Comments (0)
About PowerShow.com