Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L01 (Chapter 13) Graphics.

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L01 (Chapter 13) Graphics."— Presentation transcript:

1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L01 (Chapter 13) Graphics 1

2 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 Objectives F To understand Java coordinate systems (§13.2). F To draw things using the methods in the Graphics class (§13.3). F To obtain a graphics context using the getGraphics() method (§13.3). F To override the paintComponent method to draw things on a graphical context (§13.4). F To use a panel as a canvas to draw things (§13.5). F To draw strings, lines, rectangles, ovals, arcs, and polygons (§§13.6, 13.8-13.9). F To obtain font properties using FontMetrics and know how to center a message (§13.10). F To display image in a GUI component (§13.13). F To develop reusable GUI components FigurePanel, MessagePanel, StillClock, and ImageViewer (§§13.7, 13.11, 13.12, 13.14).

3 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 Java Coordinate System

4 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 4 Each GUI Component Has its Own Coordinate System

5 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 5 The Graphics Class You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class.

6 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 6 Obtaining Graphics Object The Graphics class is an abstract class that provides a device-independent graphics interface 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 on the native platform. This object can be obtained using the getGraphics() method. For example, the graphics context for a label object jlblBanner can be obtained using Graphics graphics = jlblBanner.getGraphics(); You can then apply the methods in the Graphics class to draw things on the label’s graphics context.

7 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 7 A Drawing Example Draw a line and a text (0, 0) (50, 50) TestGetGraphicsRun

8 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 8 Two Problems With the Preceding Example F If you resize the frame, the line is gone. F It is awkward to program because you have to make sure that the component to be displayed before obtaining its graphics context using the getGraphics() method. For this reason, Lines 20 and 21 are placed after the frame is displayed in Line 17. To fix the first problem, you need to know its cause. When you resize the frame, the JVM invokes the paintComponent method of a Swing component (e.g., a label) to redisplay the graphics on the component. Since you did not draw a line in the paintComponent method, the line is gone when the frame is resized. To permanently display the line, you need to draw the line in the paintComponent method.

9 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 9 The paintComponent Method The paintComponent method is defined in JComponent, and its header is as follows: protected void paintComponent(Graphics g) This method, defined in the JComponent class, 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.

10 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 10 paintComponent Example In order to draw things on a component (e.g., a JLabel), you need to declare a class that extends a Swing GUI component class and overrides its paintComponent method to specify what to draw. The first program in this chapter can be rewritten using paintComponent. TestPaintComponentRun

11 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 11 Drawing on Panels F 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. You can then display strings, draw geometric shapes, and view images on the panel. TestPanelDrawingRun

12 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 12 Drawing Geometric Figures F Drawing Strings F Drawing Lines F Drawing Rectangles F Drawing Ovals F Drawing Arcs F Drawing Polygons

13 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 13 Drawing Strings drawLine(int x1, int y1, int x2, int y2);drawString(String s, int x, int y);

14 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 14 Drawing Rectangles drawRect(int x, int y, int w, int h); fillRect(int x, int y, int w, int h);

15 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 15 Drawing Rounded Rectangles 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);

16 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 16 Drawing Ovals drawOval(int x, int y, int w, int h); fillOval(int x, int y, int w, int h);

17 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 17 Case Study: The FigurePanel Class This example develops a useful class for displaying various figures. The class enables the user to set the figure type and specify whether the figure is filled, and displays the figure on a panel. FigurePanel

18 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 18 Test FigurePanel This example develops a useful class for displaying various figures. The class enables the user to set the figure type and specify whether the figure is filled, and displays the figure on a panel. TestFigurePanelRun FigurePanel.java


Download ppt "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L01 (Chapter 13) Graphics."

Similar presentations


Ads by Google