Graphics basic 1 Objectives Understand Java coordinate systems

Graphics basic 1

Objectives • • • Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paint. Component method to draw things. Use a panel as a canvas to draw things. Draw strings, lines, rectangles, ovals, arcs, and polygons. • Display image in a GUI component. • Using Swing Timers. 2

Java Coordinate System • To paint, you need to know where to paint. • the origin (0, 0) at the upper-left corner of the component 3

Java Coordinate System • Each GUI Component Has its Own Coordinate System 4

The Graphics Class You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class. 5

The Graphics Class 6

Obtaining Graphics Object • The Graphics class is an abstract class that used for displaying figures and images on the screen on different platforms. • Whenever a component (e. g. , a button, a label, a panel) is displayed, a Graphics object is created for the component. 7

Obtaining Graphics Object • You can then apply the methods in the Graphics class to draw things on the label’s graphics context. • Think of a GUI component as a piece of paper and the Graphics object as a pencil or paintbrush. • You can apply the methods in the Graphics class to draw things on a GUI component. 8

The paint. Component Method • To draw you will override paint. Component. – protected void paint. Component(Graphics g). • This method, is invoked whenever the component is first displayed or redisplayed. • The Graphics object g is created automatically by the JVM for every visible GUI component. • The JVM obtains the Graphics object and passes it to invoke paint. Component. 9

Drawing on Panels • JPanel can be used to draw graphics (including text) and enable user interaction. • To draw in a panel, you create a new class that extends JPanel and override the paint. Component method to tell the panel how to draw things. • Invoking super. paint. Component(g) is necessary to ensure that the viewing area is cleared before a new drawing is displayed. 10

Drawing Geometric Figures • • • Drawing Strings Drawing Lines Drawing Rectangles Drawing Ovals Drawing Arcs Drawing Polygons 11

Drawing Strings, Lines draw. String(String s, int x, int y); 12 draw. Line(int x 1, int y 1, int x 2, int y 2);

Drawing Rectangles draw. Rect(int x, int y, int w, int h); fill. Rect(int x, int y, int w, int h); 13

Drawing Ovals draw. Oval(int x, int y, int w, int h); fill. Oval(int x, int y, int w, int h); 14

Drawing Rounded Rectangle draw. Round. Rect(int x, int y, int w, int h, int aw, int ah); fill. Round. Rect(int x, int y, int w, int h, int aw, int ah); 15

Test. Figure. Panel This example develops a useful class for displaying various figures. 16

Drawing Arcs draw. Arc(int x, int y, int w, int h, int angle 1, int angle 2); fill. Arc(int x, int y, int w, int h, int angle 1, int angle 2); Angles are in degree 17

Drawing Arcs Example 18
![Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y](http://slidetodoc.com/presentation_image_h2/d6b5783fd8f5ad904f578f8248ef7299/image-19.jpg)
Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g. draw. Polygon(x, y, x. length); 19 g. draw. Polyline(x, y, x. length);

Drawing Polygons Using the Polygon Class Polygon polygon = new Polygon(); polygon. add. Point(40, 59); polygon. add. Point(40, 100); polygon. add. Point(10, 100); g. draw. Polygon(polygon); 20

Drawing Polygons Example 21

Displaying Image Icons • You learned how to create image icons and display image icons in labels and buttons. For example, the following statements create an image icon and display it in a label: Image. Icon icon = new Image. Icon("image/us. gif"); JLabel jlbl. Image = new JLabel(image. Icon); • An image icon displays a fixed-size image. 22

Displaying Image Icons • To display an image in a flexible size, you need to use the java. awt. Image class. • An image can be created from an image icon using the get. Image() method as follows: Image image = image. Icon. get. Image(); 23

Displaying Images • Using a label as an area for displaying images is simple and convenient, but you don't have much control over how the image is displayed. • A more flexible way to display images is to use the draw. Image method of the Graphics class on a panel. Four versions of the draw. Image method are shown here. 24

Displaying Images Example 25

Swing Timers • A Swing timer fires one or more action events after a specified delay. • You can use Swing timers in two ways: Ø To perform a task once, after a delay. Ø To perform a task repeatedly. 26

Swing Timers • When you create the timer, you specify an action listener to be notified when the timer "goes off". • The action. Performed method in this listener should contain the code for whatever task you need to be performed. • When you create the timer, you also specify the number of milliseconds between timer firings. 27

Swing Timers • If you want the timer to go off only once, you can invoke set. Repeats(false) on the timer. • To start the timer, call its start() method. To suspend it, call stop(). • EX Timer timer = new Timer(speed, Listener object); timer. set. Initial. Delay(pause); //time before invoke the first listener method timer. start(); 28

Test Timer Example 29

Any Question 30
- Slides: 30