tailieunhanh - Lecture Introduction to Java programming - Chapter 15: Graphics
If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it? This chapter describes how to use the methods in the Graphics class to draw strings, lines, rectangles, ovals, arcs, polygons, and images, and how to develop reusable GUI components. | Chapter 15 Graphics Motivations If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it? Objectives To describe Java coordinate systems in a GUI component (§). To draw things using the methods in the Graphics class (§). To override the paintComponent method to draw things on a GUI component (§). To use a panel as a canvas to draw things (§). To draw strings, lines, rectangles, ovals, arcs, and polygons (§§, ). To obtain font properties using FontMetrics and know how to center a message (§). To display an image in a GUI component (§). To develop reusable GUI components FigurePanel, MessagePanel, StillClock, and ImageViewer (§§, , , ). Java Coordinate System Each GUI Component Has its Own Coordinate System The Graphics Class You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class. paintComponent Example In order to draw things on a component, you need to define a class that extends JPanel and overrides its paintComponent method to specify what to draw. The first program in this chapter can be rewritten using paintComponent. TestPaintComponent Run Drawing Geometric Figures Drawing Strings Drawing Lines Drawing Rectangles Drawing Ovals Drawing Arcs Drawing Polygons Drawing Strings drawLine(int x1, int y1, int x2, int y2); drawString(String s, int x, int y); Drawing Rectangles drawRect(int x, int y, int w, int h); fillRect(int x, int y, int w, int h); 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); Drawing Ovals drawOval(int x, int y, int w, int h); fillOval(int x, int y, int w, int h); 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, | Chapter 15 Graphics Motivations If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it? Objectives To describe Java coordinate systems in a GUI component (§). To draw things using the methods in the Graphics class (§). To override the paintComponent method to draw things on a GUI component (§). To use a panel as a canvas to draw things (§). To draw strings, lines, rectangles, ovals, arcs, and polygons (§§, ). To obtain font properties using FontMetrics and know how to center a message (§). To display an image in a GUI component (§). To develop reusable GUI components FigurePanel, MessagePanel, StillClock, and ImageViewer (§§, , , ). Java Coordinate System Each GUI Component Has its Own Coordinate System The Graphics Class You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class. paintComponent Example In order
đang nạp các trang xem trước