Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 1 COS240 O-O Languages AUBG,

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 1 COS240 O-O Languages AUBG,"— Presentation transcript:

1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 1 COS240 O-O Languages AUBG, COS dept Lecture 19 Title: Graphics with Java Reference: COS240 Syllabus

2 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 2 Motivations If you want to draw shapes such as a bar chart, a clock, or a stop sign, as shown below, how do you do it?

3 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 3 Lecture contents F To understand Java coordinate systems. F To draw things using the methods in the Graphics class. F To understand how and when a Graphics object is created. F To override the paintComponent method to draw things on a GUI component. F To use a panel as a canvas to draw things. F To draw strings, lines, rectangles, ovals, arcs, and polygons. F To obtain font properties using FontMetrics and know how to center a message. F To display image in a GUI component. F To develop reusable GUI components FigurePanel, MessagePanel, StillClock, and ImageViewer.

4 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 4 Java Coordinate System To draw or to paint, you need to specify where to draw or to paint.

5 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 5 Each GUI Component Has its Own Coordinate System

6 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 6 The Graphics class The Graphics class provides the methods for drawing –Strings –Lines –Rectangles –Ovals –Arcs –Polygons –Polylines  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 (e.g., a button, a label, a panel)

7 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 7 The Graphics Class You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class. You can apply the methods in the Graphics class to draw things on a GUI component (e.g., a button, a label, a panel)

8 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 8 The Graphics class The Graphics class 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, the JVM automatically creates a Graphics object for the component on the native platform and passes this object to invoke the paintComponent() method to display the drawings. The method is defined in JComponent class with signature as follows: protected void paintComponent(Graphics g) This method is invoked whenever a component is first displayed or redisplayed

9 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 9 The Graphics class: practical hints F In order to draw things on a component, you need to define a class that extends JPanel and overrides its paintComponent() method (inherited from JComponent) to specify what to draw.

10 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 10 The Graphics class: practical hints F One recommended application skeleton includes: –Import directives –User specified class to extend JFrame u class myFrame extends JFrame { … } –User specified test class to include main() method u public class TestGraphics { … } –User specified class to extend JPanel u class CanvasToDraw extends JPanel { … } F See next slide for details – file GraphicsTest.java

11 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 11 The Graphics class: practical hints import javax.swing.*; import java.awt.*; class myFrame extends JFrame { public myFrame() { setTitle("SB GraphicsTest1"); add(new CanvasToDraw()); // getContentPane().add(new CanvasToDraw()); } public class GraphicsTest { public static void main(String[] args) { myFrame frame = new myFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setLocationRelativeTo(null); // Center the frame frame.setVisible(true); } class CanvasToDraw extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.drawLine(0, 0, 200, 200); g.setColor(Color.RED); g.drawLine(200, 0, 0, 200); }

12 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 12 A Drawing Example, using paintComponent method Draw two lines File GraphicsTest.java

13 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 13 A Drawing Example, using paint method import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JDemoLine extends JFrame { public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLUE); g.drawLine(0, 0, 200, 200); g.setColor(Color.RED); g.drawLine(200, 0, 0, 200); } public static void main(String[] args) { JDemoLine frame = new JDemoLine(); frame.setTitle("SB Demo Line"); frame.setSize(200, 200); frame.setVisible(true); }

14 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 14 A Drawing Example, using paint method Draw two lines File JDemoLine.java

15 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 15 Problems With the paint() method F If you resize the frame, the line is gone. 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.

16 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 16 The paintComponent Method 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. The paintComponent method is automatically invoked to paint graphics whenever the component is first displayed or redisplayed. Invoking super.paintComponent(g) invokes the paintComponent method defined in the superclass. This is necessary to ensure that the viewing area is cleared before a new drawing is displayed.

17 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 17 paintComponent Example 1. The JVM invokes paintComponent() to draw things on a component. 2. The user should never invoke paintComponent() directly. 3. For this reason, the protected visibility is sufficient for paintComponent().

18 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 18 Drawing on Panels F Panels are invisible and are used as containers to group components to achieve a desired layout. F Another important use of JPanel is for drawing. F You can draw on any Swing GUI component, but normally you should use a JPanel as a canvas upon which to draw things  See next slide.

19 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 19 Drawing on Panels F What happens if you replace JPanel with JLabel or JButton in following line –class CanvasToDraw extends JPanel { F With –class CanvasToDraw extends JLabel { F Or with –class CanvasToDraw extends JButton { F The program works, but it is not preferred. JLabel is designed for creating a label, not for drawing. Same for JButton is designed for creating a button, not for drawing.

20 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 20 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.  Compile and run GraphicsTest.java  Compile and run GraphicsTest1.java

21 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 21 Drawing Geometric Figures F Drawing Strings F Drawing Lines F Drawing Rectangles F Drawing Ovals F Drawing Arcs F Drawing Polygons

22 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 22 Drawing Strings drawLine(int x1, int y1, int x2, int y2);drawString(String s, int x, int y);

23 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 23 Drawing Rectangles drawRect(int x, int y, int w, int h); fillRect(int x, int y, int w, int h);

24 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 24 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);

25 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 25 Drawing Ovals drawOval(int x, int y, int w, int h); fillOval(int x, int y, int w, int h);

26 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 26 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

27 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 27 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

28 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 28 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

29 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 29 Drawing Arcs Example DrawArcsRun

30 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 30 Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g.drawPolygon(x, y, x.length); g.drawPolyline(x, y, x.length);

31 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 31 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);

32 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 32 Drawing Polygons Example DrawPolygon Run

33 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 33 Demo Programs Program “Running Fan” Open, Compile and Run Folder Exercise16_12_RunningFan

34 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 34 Demo Programs Program “4 Running Fans” Open, Compile and Run Folder Exercise18_12_FourRunningFans

35 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 35 Demo Programs Program “MouseInsideTriangle” Open, Compile and Run Folder Exercise16_21_MouseInsideTriangle

36 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 36 Demo Programs Program “Message Panel” Open, Compile and Run Folder Exercise17_1_MessagePanelA

37 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 37 Demo Programs Program “Message Panel” Open, Compile and Run Folder Exercise17_10_MessagePanelB

38 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 38 Demo Programs Program “Random Circles” Open, Compile and Run Folder Exercise18_28_RandomCircles

39 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 39 Demo Programs Program “Random Rectangles” Open, Compile and Run Folder Exercise18_29_RandomRectangles

40 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 40 Demo Programs Program “Random Triangles” Open, Compile and Run Folder Exercise18_30_RandomTriangles

41 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 013601267141 Thank You for Your attention!


Download ppt "Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 1 COS240 O-O Languages AUBG,"

Similar presentations


Ads by Google