graphics basic 1. 2 objectives understand java coordinate systems. draw things using the methods in...
TRANSCRIPT
![Page 1: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/1.jpg)
Graphics basic
1
![Page 2: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/2.jpg)
2
Objectives
• Understand Java coordinate systems.• Draw things using the methods in the Graphics class.• Override the paintComponent 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.
![Page 3: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/3.jpg)
3
Java Coordinate System• To paint, you need to know where to paint. • the origin (0, 0) at the upper-left corner of the
component
![Page 4: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/4.jpg)
4
Java Coordinate System• Each GUI Component Has its Own
Coordinate System
![Page 5: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/5.jpg)
5
The Graphics Class
You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class.
![Page 6: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/6.jpg)
6
The Graphics Class
![Page 7: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/7.jpg)
7
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.
![Page 8: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/8.jpg)
8
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.
![Page 9: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/9.jpg)
9
The paintComponent Method
• To draw you will override paintComponent.– protected void paintComponent(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 paintComponent.
![Page 10: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/10.jpg)
10
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 paintComponent method to tell the panel how to draw things.
• Invoking super.paintComponent(g) is necessary to ensure that the viewing area is cleared before a new drawing is displayed.
![Page 11: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/11.jpg)
11
Drawing Geometric Figures
• Drawing Strings• Drawing Lines• Drawing Rectangles• Drawing Ovals• Drawing Arcs• Drawing Polygons
![Page 12: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/12.jpg)
12
Drawing Strings, Lines
drawLine(int x1, int y1, int x2, int y2);drawString(String s, int x, int y);
![Page 13: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/13.jpg)
13
Drawing RectanglesdrawRect(int x, int y, int w, int h);
fillRect(int x, int y, int w, int h);
![Page 14: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/14.jpg)
14
Drawing Ovals
drawOval(int x, int y, int w, int h);
fillOval(int x, int y, int w, int h);
![Page 15: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/15.jpg)
15
Drawing Rounded Rectangle
drawRoundRect(int x, int y, int w, int h, int aw, int ah);
fillRoundRect(int x, int y, int w, int h, int aw, int ah);
![Page 16: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/16.jpg)
16
TestFigurePanel
This example develops a useful class for displaying various figures.
![Page 17: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/17.jpg)
17
Drawing Arcs
drawArc(int x, int y, int w, int h, int angle1, int angle2);
fillArc(int x, int y, int w, int h, int angle1, int angle2);
Angles are in degree
![Page 18: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/18.jpg)
18
Drawing Arcs Example
![Page 19: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/19.jpg)
19
Drawing Polygons and Polylines
g.drawPolygon(x, y, x.length);
int[] x = {40, 70, 60, 45, 20};int[] y = {20, 40, 80, 45, 60};
g.drawPolyline(x, y, x.length);
![Page 20: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/20.jpg)
20
Drawing Polygons Using the Polygon Class
Polygon polygon = new Polygon(); polygon.addPoint(40, 59); polygon.addPoint(40, 100); polygon.addPoint(10, 100); g.drawPolygon(polygon);
![Page 21: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/21.jpg)
21
Drawing Polygons Example
![Page 22: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/22.jpg)
22
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:ImageIcon icon = new ImageIcon("image/us.gif");JLabel jlblImage = new JLabel(imageIcon);
• An image icon displays a fixed-size image.
![Page 23: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/23.jpg)
23
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 getImage() method as follows:Image image = imageIcon.getImage();
![Page 24: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/24.jpg)
24
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 drawImage method of the Graphics class on a panel. Four versions of the drawImage method are shown here.
![Page 25: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/25.jpg)
25
Displaying Images Example
![Page 26: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/26.jpg)
26
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.
![Page 27: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/27.jpg)
27
Swing Timers
• When you create the timer, you specify an action listener to be notified when the timer "goes off".
• The actionPerformed 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.
![Page 28: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/28.jpg)
28
Swing Timers
• If you want the timer to go off only once, you can invoke setRepeats(false) on the timer.
• To start the timer, call its start() method. To suspend it, call stop().
• EXTimer timer = new Timer(speed, Listener object); timer.setInitialDelay(pause); //time before invoke the first listener methodtimer.start();
![Page 29: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/29.jpg)
29
Test Timer Example
![Page 30: Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method](https://reader031.vdocuments.mx/reader031/viewer/2022032723/56649f575503460f94c7b34e/html5/thumbnails/30.jpg)
Any Question
30