32
Graphics basic

Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Embed Size (px)

Citation preview

Page 1: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Graphics basic

Page 2: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Agenda

• Java Coordinate Systems.

• Graphics Class.

• Drawing on Panels.

• Drawing Shapes.

Page 3: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Java Coordinate Systems

Page 4: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Java Coordinate System

• To paint, you need to know where to paint. • The origin (0, 0) at the upper-left corner of the

component.

8 - 4

Page 5: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Java Coordinate System

• Each GUI Component Has its Own Coordinate System.

8 - 5

Page 6: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Graphics Class

Page 7: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

The Graphics Class

• Is a java class allow you to draw Strings. Lines. Rectangles. Ovals. Arcs. Polygons. Polylines.

8 - 7

Page 8: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

The Graphics Class

8 - 8

Page 9: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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 (button, label, panel etc…) is displayed, a Graphics object is created for the component.

8 - 9

Page 10: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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 - 10

Page 11: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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.

8 - 11

Page 12: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing on Panels

Page 13: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing on Panels

• Panel can be used to draw graphics 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.

8 - 13

Page 14: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Shapes

Page 15: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Shapes

• Drawing Strings.• Drawing Lines.• Drawing Rectangles.• Drawing Ovals.• Drawing Arcs.• Drawing Polygons.

8 - 15

Page 16: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Strings, Lines

8 - 16

drawLine(int x1, int y1, int x2, int y2);drawString(String s, int x, int y);

Page 17: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

TestDrawLine

• This example develops a useful class for displaying line and string.

8 - 17

Page 18: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Rectangles

drawRect(int x, int y, int w, int h);

fillRect(int x, int y, int w, int h);

8 - 18

Page 19: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

TestDrawRectangle

• This example develops a useful class for displaying rectangle.

8 - 19

Page 20: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Ovals

drawOval(int x, int y, int w, int h);

fillOval(int x, int y, int w, int h);

8 - 20

Page 21: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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

8 - 21

Page 22: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

TestFigurePanel

• This example develops a useful class for displaying various figures.

8 - 22

Page 23: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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

8 - 23

Angles are in degree

Page 24: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Arcs Example

8 - 24

Page 25: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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

8 - 25

Page 26: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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

8 - 26

Page 27: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Drawing Polygons Example

8 - 27

Page 28: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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.

8 - 28

Page 29: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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

8 - 29

Page 30: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

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.

8 - 30

Page 31: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Displaying Images Example

8 - 31

Page 32: Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes

Any Question