UID Swing and Graphics Boriana Koleva bnkcs nott
UID – Swing and Graphics Boriana Koleva (bnk@cs. nott. ac. uk)
Today • Graphics w When / Why ? w Painting w What can be done w Java 2 D • 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/14 painting/index. html • http: //java. sun. com/docs/books/tutorial/2 d/TOC. html#display
When do I need to use graphics? (1) • Avoid using graphics if possible • You may well only need w icons w labels and buttons w borders w styled text areas § JEditor. Pane § JText. Pane
When do I need to use graphics? (2) • May need graphics if w Wish to drastically change appearance of an existing component w Draw shapes to screen w Draw text to screen w Animation w “Invent” / customise your own component
Painting (1) • Component painting w repaint() § automatically called when necessary. Eg Window unhidden w revalidate() § automatically called before repaint when needed. Eg when component sizes or position have changed • Threads and painting w painting on the event-dispatching thread w repaint() and revalidate() are thread safe
Painting (2) • Double buffering w 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 w always rectangular w 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 w w Background (if opaque) Custom painting Border Child components • repaints w 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 w Basic but usually enough w Shapes w Text w Images • Java 2 D graphics w Extends the AWT graphics to provide much more, eg gradients, textures etc
Custom painting (1) • Occurs between background and border • Extend component and customise w JPanel recommended § e. g. class my. Painting. Panel extends JPanel { w can also use atomic components § e. g. class my. Painting. Button extends JButton { • Code goes in overridden paint. Component() method w public void paint. Component(Graphics g) {
Custom painting (2) • When writing custom painting code w should not reproduce painting functionality of any other Swing component • Before doing anything else in paint. Component() w invoke super. paint. Component() or w set. Opaque(false)
Painting coordinates • Component sizes in pixels w (0, 0) at top left corner w (width-1, height-1) at bottom right corner • Border sizes (e. g. a 1 -pixel border) w (1, 1) at top left w (width-2, height-2) at bottom right • General painting area for b pixel border w [ {b, b} : {w-(b+1), h-(b+1)} ]
Getting painting coordinates • Use component get. Width() and get. Height() • Get border sizes with get. Insets() w e. g. to get width and height of custom painting area use something like: w Insets insets = get. Insets(); int current. Width = get. Width() insets. left - insets. right; int current. Height = get. Height() insets. top - insets. bottom;
Repainting custom paints • Calling repaint() method requests a repaint to occur w repaint() § paints whole component & any others that need it if transparent w 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. w Represents ‘state’ § eg Color, Font, etc • Methods of Graphics can draw. . . w Shapes § lines, rectangles, 3 D rectangles, round-edged rectangles, ovals, arcs, polygons w Text § Draw string to screen as a graphic
Graphics - shapes and text (2) • Drawing a shape on screen • Use paint. Component Graphics void paint. Component(Graphics g) { super(g) g. set. Color(Color. RED); g. draw. Rect(x, y, width, height); } • shapes: x and y specify upper left
Graphics - shapes and text (3) • Drawing a string on screen • Use paint. Component Graphics again void paint. Component(Graphics g) { super(g) g. set. Font(new Font(“Serif”, Font. PLAIN, 12)); g. draw. Sring(“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 my. Image = Toolkit. get. Default. Toolkit(). get. Image(filename); g. draw. Image(my. Image, x, y, this); g. draw. Image(my. Image, x, y, width, height, this);
Tracking Images • Media. Tracker class w utility class used to track the status of a number of images for a particular component. w Can get information about any errors and associated images • Image. Observer interface w implemented by all components w can use to get notifications about loading progress of image as its constructed.
Java 2 D API (Graphics 2 D) • Provides enhanced image, graphics and text capabilities • Done through extensions to AWT Graphics API • Amongst others provides w w Hit detection on Shapes text and images Complex Printing Support Texture rendering Complex Shapes/lines
Java 2 D Shapes • Rectangle Shapes • Quad. Curve 2 D and Cubic. Curve 2 D • General. Path w Arbitrary shapes made by connecting lines
Java 2 D - Text • Often easier to use JLabels since much of the work is done for you • Use the draw. String method w Eg g. draw. String(“Hello”, x, y); • To set font use set. Font method w Create an instance of Font
Java 2 D - Images • Much more powerful than basic image display • Images are best rendered in a Buffer off screen as a Buffered. Image • Then drawn to screen with call to Graphics 2 D. draw. Image • Graphics 2 D provides variety of image filtering options w sharpen/blur, rotate, transform, scale etc
Java 2 D – (VERY) Basic use • Simply cast the Graphics object to Graphics 2 D void paint. Component(Graphics g) { super(g); Graphics g 2 = (Graphics 2 D)g. create(); g 2. set. Color(Color. BLACK); g 2. draw. Rect(x, y, width, height); g 2. dispose(); }
The Full Screen API • Full Name: w 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: w http: //java. sun. com/docs/books/tutorial/extra/fullscreen/index. html
Summary • Swing +ve’s w Swings good because its Platform Independent w Swing makes it easy to construct simple GUI’s quickly • Swing –ve’s w Swing is not always as Platform Independent as it claims w Can get confusing when GUI’s get BIG, but aided by using an IDE w Java - its not the fastest language ever. . .
The End.
- Slides: 27