Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting Started with GUI Programming Chapter 10 CSCI 1302.

Similar presentations


Presentation on theme: "Getting Started with GUI Programming Chapter 10 CSCI 1302."— Presentation transcript:

1 Getting Started with GUI Programming Chapter 10 CSCI 1302

2 CSCI 1302 – Getting Started with GUI Programming2 Drawing Graphics on Panels JPanel can be used for both containing components and for direct drawing To draw in a JPanel, you create a new class that extends JPanel and override the paintComponent method Doing this prevents you from interfering with other components Override this method protected void paintComponent(Graphics g) g is provided automatically by JVM

3 CSCI 1302 – Getting Started with GUI Programming3 Drawing Graphics on Panels Create a new type of panel Override the paintComponent method to modify drawing behavior Add your new panel to a frame See DrawMessage.java

4 CSCI 1302 – Getting Started with GUI Programming4 Drawing Graphics on Panels Java doesn’t use the traditional coordinate system

5 CSCI 1302 – Getting Started with GUI Programming5 Drawing Lines drawLine(int x1, int y1, int x2, int y2)

6 CSCI 1302 – Getting Started with GUI Programming6 Drawing Rectangles drawRect(int x, int y, int w, int h) fillRect(int x, int y, int w, int h) draw3DRect(int x, int y, int w, int h, int raised) x, y are upper left corner w, h are width and height

7 CSCI 1302 – Getting Started with GUI Programming7 Drawing 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) See DrawRectangles.java

8 CSCI 1302 – Getting Started with GUI Programming8 Drawing Ovals Based on bounding box of rectangle drawOval(int x, int y, int w, int h) fillOval(int x, int y, int w, int h) See DrawOvals.java

9 CSCI 1302 – Getting Started with GUI Programming9 Drawing Arcs Based on bounding rectangle drawArc(int x, int y, int w, int h, int startAngle, int arcAngle) fillArc(int x, int y, int w, int h, int startAngle, int arcAngle)

10 CSCI 1302 – Getting Started with GUI Programming10 The Polygon Class Description of a closed 2D region Arbitrary number of line segments Internally represented as a list of coordinate pairs Each pair represents a vertex, each successive pair represent the endpoints of a line First and last pairs are joined by a line to close the polygon

11 CSCI 1302 – Getting Started with GUI Programming11 The Polygon Class Two constructors public Polygon() public Polygon(int[] xpoints, int[] ypoints, int npoints) Draw polygons with one of 4 methods drawPolygon(Polygon polygon) drawPolygon(int[] xs, int[] ys, int n) fillPolygon(Polygon polygon) fillPolygon(int[] xs, int[] ys, int n)

12 CSCI 1302 – Getting Started with GUI Programming12 The Polygon Class Can add points manually, or through arrays Use the respective drawPolygon or fillPolygon methods to actually draw the shape on the canvas Also a drawPolyline method that doesn’t close the shape Polygon p = new Polygon(); p.addPoint(40,20); p.addPoint(70,40); p.addPoint(60,80); g.drawPolygon(p); OR int x[] = {40, 70, 60}; int y[] = {20, 40, 80}; g.drawPolygon(x,y, x.length) See DrawPolygon.java

13 CSCI 1302 – Getting Started with GUI Programming13 The FontMetrics Class FontMetrics can measure these attributes Leading, Ascent, Descent, Height Use getters, getFontMetrics and stringWidth method See CenterMessage.java

14 CSCI 1302 – Getting Started with GUI Programming14 Case Study: MessagePanel Develop a class that displays a message in a panel User can set the location of the message, center the message, or move the location UML on next slide provides the class contract; users can use the class without knowing how the class is implemented

15 CSCI 1302 – Getting Started with GUI Programming15 Case Study: MessagePanel

16 CSCI 1302 – Getting Started with GUI Programming16 Case Study: MessagePanel Do not invoke the paintComponent method directly Use the repaint method to force the JVM to update the viewing area, the JVM will call the paintComponent method Notice the reusability of MessagePanel, it can now be used in any other class to display a message on a panel


Download ppt "Getting Started with GUI Programming Chapter 10 CSCI 1302."

Similar presentations


Ads by Google