UID Swing and Graphics. Today. Graphics A big topic. Boriana Koleva Graphics. Full Screen API

UID – Swing and Graphics Boriana Koleva ([email protected]) Today • Graphics Š When / Why ? Š Painting Š What can be done Š Java 2D • Full Screen A...
Author: Milo Brown
2 downloads 0 Views 86KB Size
UID – Swing and Graphics

Boriana Koleva ([email protected])

Today • Graphics Š When / Why ? Š Painting Š What can be done Š Java 2D

• Full Screen API

Graphics – A big topic • Could run a whole lecture course on how to use the various graphics capabilities that Swing’s API’s provide. • Use the Tutorials and API docs at: • http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html • http://java.sun.com/docs/books/tutorial/2d/TOC.html#display

1

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

When do I need to use graphics? (2) • May need graphics if Š Wish to drastically change appearance of an existing component Š Draw shapes to screen Š Draw text to screen Š Animation Š “Invent” / customise your own component

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

2

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

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

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

3

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) {

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)

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-(b+1), h-(b+1)} ]

4

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;

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

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

5

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

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”, x, y); }

• x and y specify baseline left of text

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 NOT BMP • Loading and displaying images Image myImage = Toolkit.getDefaultToolkit().getImage(filename); g.drawImage(myImage, x, y, this); g.drawImage(myImage, x, y, width, height,this);

6

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 use to get notifications about loading progress of image as its constructed.

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

Java2D Shapes • Rectangle Shapes • QuadCurve2D and CubicCurve2D • GeneralPath Š Arbitrary shapes made by connecting lines

7

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

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

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(); }

8

The Full Screen API • Full Name: Š Full Screen Exclusive API

• Introduced in 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/fullscreen/index.html

Summary • Swing +ve’s Š Swings good because its Platform Independent Š Swing makes it easy to construct simple GUI’s quickly

• Swing –ve’s Š Swing is not always as Platform Independent as it claims Š Can get confusing when GUI’s get BIG, but aided by using an IDE Š Java - its not the fastest language ever...

The End.

9