Java Programming for absolute beginner- P19 ppt

20 181 0
Java Programming for absolute beginner- P19 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

and then fill a rectangle of the same size over it in a different color, you would still see the right and bottom edges of the drawn rectangle because it is one pixel wider and taller. For example: g.setColor(Color.black); g.drawRect(10, 10, 100, 100); g.setColor(Color.green); g.fillRect(10, 10, 100, 100); Using this code in the paint(Graphics) method, you would see a green rectan- gle with a black right and bottom edge. This same idea holds true for all draw and fill methods of the Graphics class. Try it out and see for yourself. 318 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r FIGURE 9.3 Drawing rectangles is the coolest. (10, 10) (250, 10) (300, 250) 150 150 150 200 150 100 50 75 250 100 100 (10, 200) Here is the source code for RectTest.java, an application that illustrates these methods: /* * RectTest * Demonstrates drawing Rectangles */ import java.awt.*; public class RectTest extends Canvas { public RectTest() { super(); setSize(300, 200); setBackground(Color.white); } public static void main(String args[]) { RectTest rt = new RectTest(); GUIFrame frame = new GUIFrame("Rectangle Test"); frame.add(rt); JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 318 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { g.drawRect(10, 10, 150, 150); g.setColor(Color.blue); g.fillRect(250, 10, 200, 150); g.setColor(Color.red); g.drawRoundRect(10, 200, 250, 150, 100, 100); g.setColor(Color.green); g.fillRoundRect(300, 250, 100, 100, 50, 75); } } This application renders four rectangles, as shown in Figure 9.3. The first one (upper-left) is drawn at point (10, 10) and is 150 wide by 150 high. The second rectangle (upper-right) is filled with the color blue at position (250, 10) with dimensions 200 by 150. The next rectangle (lower-left) is rounded, drawn at posi- tion (10, 200), with a width of 250 and a height of 150. An arc of a circular diam- eter of 100 rounds the corners. The fourth and final rectangle (lower-right) is a round rectangle filled at position (300, 250), with a dimension of 100 by 100 and rounded at the corners by an arc with a horizontal diameter of 50 and a verti- cal diameter of 75. Drawing 3D Rectangles 3D rectangles have the appearance of either being raised above or sunk below the surface by using highlighting colors. The colors that are used for highlight- ing are dependent upon the current color. The method for drawing a 3D rectangle is draw3DRect(int x, int y, int width, int height, boolean raised) for drawing a rectangle at the given (x, y) position with the given width by height dimensions and a boolean value that is true if the 3D rectangle should be raised, or false if it should be sunken. The method for filling a 3D rec- tangle is fill3DRect(int x, int y, int width, int height, boolean raised). The Rect3DTest application illustrates these methods: /* * Rect3DTest * Draws 3D Rectangles */ import java.awt.*; public class Rect3DTest extends Canvas { public Rect3DTest() { super(); 319 C h a p t e r 9 T h e G r a p h i c s C l a s s : D r a w i n g S h a p e s ,I m a g e s ,a n d T e x t JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 319 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. setSize(300, 200); setBackground(SystemColor.control); } public static void main(String args[]) { Rect3DTest rt3d = new Rect3DTest(); GUIFrame frame = new GUIFrame("3D Rectangle Test"); frame.add(rt3d); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { g.setColor(Color.gray); g.draw3DRect(5, 5, 140, 90, true); g.draw3DRect(150, 5, 140, 90, false); g.fill3DRect(5, 100, 140, 90, true); g.fill3DRect(150, 100, 140, 90, false); } } Let the rendering begin! First the color is set to gray. The first rectangle is drawn at location (5, 5) and the width and height are 140 by 90, which is the same width and height for all four of these 3D rectangles. The fifth argument is true, so the first rectangle should appear raised. In Figure 9.4, the first 3D rectangle appears in the upper-left portion of the frame. The second 3D rectangle is on the upper- right and the fifth argument is false, so it should appear sunken. The next two 3D rectangles are filled underneath the drawn rectangles. The one on the left is raised, whereas the one on the right is not. 320 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r Notice that the drawn 3D rectangle appears raised when the lighter color is on the top and left side and the darker color is on the bottom and the right side. The opposite is true to make the 3D rectangle appear sunken. In fact if you look at Figure 9.4 upside down, the raised rectangle will appear sunk and the sunken rectangle will appear raised. HINT FIGURE 9.4 3D Rectangles can be raised or not. Raised Not raised JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 320 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Drawing Ovals Drawing ovals is similar to drawing rectangles. In fact the arguments to the methods that render the ovals specify the rectangular bounds of the oval. There are two methods for rendering ovals. The drawOval(int x, int y, int width, int height) method draws the outline of an oval that fits in the rectangle spec- ified by the arguments. The position is at ( x, y) and the width and height are given as the third and fourth arguments. The fillOval(int x, int y, int width, int height) method fills the oval with the current color. The OvalTest application uses these methods to render two ovals. Here is the source code for OvalTest.java: /* * OvalTest * Demonstrates drawing Ovals */ import java.awt.*; public class OvalTest extends Canvas { public OvalTest() { super(); setSize(300, 200); setBackground(Color.white); } public static void main(String args[]) { OvalTest ot = new OvalTest(); GUIFrame frame = new GUIFrame("Oval Test"); frame.add(ot); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { g.fillOval(10, 10, 100, 100); g.setColor(Color.blue); g.drawOval(110, 110, 150, 50); } } The first oval is filled at position (10, 10) and has a width and a height of 100. The second oval is drawn at (110, 110). Unlike the first oval, this second oval is not an exact circle. Its width is 150 and its height is 50. You can see these ovals rendered in Figure 9.5. 321 C h a p t e r 9 T h e G r a p h i c s C l a s s : D r a w i n g S h a p e s ,I m a g e s ,a n d T e x t JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 321 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Drawing Arcs Rendering arcs is similar to rendering ovals except there are two more argu- ments in its methods. Arcs are also known as semicircles. The drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) method draws an arc at the given position and having the given dimensions. The fifth argument specifies the starting angle, which is where on the oval specified by the first four arguments to start drawing the arc. If you think of the oval as the face of a clock, 0 degrees is at three o’clock, 90 degrees is at twelve o’clock, 180 degrees is at nine o’clock, 270 degrees is at six o’clock, and so it goes for all the angles in between these right angles. The sixth argument is the arc angle; it is the arc length along the oval. 360 degrees makes a whole circle, 180 degrees makes half a circle, and so on, from the starting point. The direction in which the arc is drawn is depen- dent on the sign of the sixth argument. A positive value indicates a counter- clockwise direction, whereas a negative value indicates a clockwise direction. The fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) method takes the same arguments but it fills the arc instead of draw- ing the outline. The shape of a filled arc is like that of a piece of pie. “mmmm- mmm pie,” as Homer would say. If that doesn’t draw a clear enough picture for you think of it as the minute hand of a clock (that can go both ways). The tip of the minute hand moves along the arc outline and the whole thing leaves a trail as it moves. The ArcTest application uses these methods to render a couple of arcs. /* * ArcTest * Demonstrates drawing Arcs */ import java.awt.*; public class ArcTest extends Canvas { public ArcTest() { super(); setSize(300, 200); 322 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r FIGURE 9.5 Two ovals, one filled, and one drawn. (10, 10) 100 100 JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 322 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. setBackground(Color.white); } public static void main(String args[]) { ArcTest at = new ArcTest(); GUIFrame frame = new GUIFrame("Arc Test"); frame.add(at); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { g.drawArc(10, 10, 100, 100, 0, 270); g.setColor(Color.green); g.fillArc(150, 150, 50, 50, 90, -270); } } The first arc, shown in the upper-left corner of Figure 9.6, is drawn along at posi- tion (10, 10). The width and height are both 100. These bounds define the oval that the arc is drawn along. The starting angle is 0, so the arc will start at 3:00. The sixth argument is 270, so the arc will be drawn 270 degrees along the oval in a counter-clockwise direction. The second arc (in the lower-right corner of the fig- ure) is drawn at (150, 150). Its width and height are both 50. The starting angle is 90, so the arc will start at 12:00. The arcAngle is –270, so it will be filled 270 degrees along the oval in a clockwise direction. The third arc, the Arc of the Covenant, has yet to be found. 323 C h a p t e r 9 T h e G r a p h i c s C l a s s : D r a w i n g S h a p e s ,I m a g e s ,a n d T e x t Drawing Polygons Polygons are multisided, enclosed shapes that are rendered by connecting a series of points together with lines. There are four methods for rendering poly- gons, described here: • The drawPolygon(int[] xPoints, int[] yPoints, int nPoints) method draws the outline of a polygon defined by an array of x-coordinate points, xPoints, and an array of y-coordinate points, yPoints that correspond to FIGURE 9.6 Drawing arcs is like drawing semi- circles. (10, 10) 100 100 0° 270° JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 323 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. the x-coordinate points by the same array index. The third argument speci- fies the number of points of the polygon. • The drawPolygon(Polygon) method draws the outline of a polygon based on the Polygon object passed to it. • The other two methods are the draw methods’ corresponding fill methods and fill the polygonal shapes with the current color. The PolyTest application renders a few polygons. Here is the source code: /* * PolyTest * Demonstrates drawing Polygons * and also the translate() method in the Graphics class */ import java.awt.*; public class PolyTest extends Canvas { public PolyTest() { super(); setSize(300, 200); setBackground(Color.white); } public static void main(String args[]) { PolyTest pt = new PolyTest(); GUIFrame frame = new GUIFrame("Polygon Test"); frame.add(pt); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Polygon p = new Polygon(); p.addPoint(150, 10); p.addPoint(150, 75); p.addPoint(290, 75); g.drawPolygon(p); int[] xs = {50, 75, 10, 90, 25}; int[] ys = {10, 90, 40, 40, 90}; g.fillPolygon(xs, ys, 5); g.translate(150, 100); g.drawPolygon(xs, ys, 5); } } The first polygon is rendered by passing a Polygon object to the drawPolygon(Polygon) method. The Polygon class itself is not that important in 324 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 324 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. this particular book, so I’ll just go over how I used it here. I created a new Poly- gon object, p, by using the no-argument constructor. Then I added points by call- ing the addPoint(int x, int y) method. The points I added are the ones that were used to draw the triangle shown in Figure 9.7. 325 C h a p t e r 9 T h e G r a p h i c s C l a s s : D r a w i n g S h a p e s ,I m a g e s ,a n d T e x t The second polygon is star shaped and the lines cross each other. It is filled using the even-odd, or alternating fill, rule. That’s why the center of the star remains unfilled. Think of it sort of like a checkerboard. When lines of a polygon cross and the polygon is filled, only every other enclosed area is filled. The third polygon is drawn using the same arrays as the second polygon, but the translate(int x, int y) method is called beforehand. This method changes the origin, which is (0, 0) by default to the specified position. The third polygon is drawn relative to the new origin, (150, 100) instead of (0, 0), so all the points of the second polygon shift over by 150 to the right, and 100 down to draw the third polygon. Be careful when rendering polygons. The third argument, which specifies the number of points of the polygon, can cause an ArrayIndexOutOfBounds excep- tion if the arrays passed as the first and second arguments don’t have the capac- ity to hold that number of integers. Did you notice that when the polygons were rendered in the PolyTest applica- tion, the first point does not have to be repeated to close the polygon? This hap- pens automatically. The last point is automatically connected to the first point to close the polygon. There is a way to draw a polygon that remains unclosed. This is referred to as a poly line. The one method that allows you to do this is the drawPolyline(int[] xPoints, int[] yPoints, int nPoints) method. As you can see it takes the same arguments as one of the drawPolygon() methods, but when you use drawPolyline() method, the polygon is not closed. Note that a poly line cannot be filled because it is not closed. That’s why there is no such thing as a fillPolyline() method. TRICK TRAP (50, 10) (10, 40) (25, 90) (90, 40) (75, 90) FIGURE 9.7 Three polygons are drawn here. Notice that the one that is filled is not filled in its center. JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 325 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Drawing Strings The drawString(String str, int x, int y) method draws the given String object at the given (x, y) location. The given location indicates the position of the baseline of the left-most character in the string. The string is drawn using the Graphics object’s current font and color. The StringTest application draws some strings onto a Canvas. Here is the source code for StringTest.java: /* * StringTest * Demonstrates drawing Strings */ import java.awt.*; public class StringTest extends Canvas { public StringTest() { super(); setSize(300, 200); setBackground(Color.white); } public static void main(String args[]) { StringTest st = new StringTest(); GUIFrame frame = new GUIFrame("String Test"); frame.add(st); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { g.drawString("Metallica ", 10, 10); g.setColor(Color.lightGray); g.setFont(new Font("Timesroman", Font.ITALIC, 48)); g.drawString("New Bassist?", 25, 150); g.setColor(Color.black); g.setFont(new Font("Courier", Font.BOLD, 20)); g.drawString("James", 20, 40); g.drawString("Lars", 30, 60); g.drawString("Dave", 40, 80); g.drawString("Kirk", 60, 100); g.drawString("Ron", 80, 120); g.drawString("Cliff", 100, 140); g.drawString("Jason", 120, 160); } } The StringTest application sets two different fonts by calling the Graphics class’ setFont(Font) method. You’ll learn more about fonts in the next section. This application draws a bunch of strings onto the Canvas. You can see the output in Figure 9.8. 326 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 326 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 327 C h a p t e r 9 T h e G r a p h i c s C l a s s : D r a w i n g S h a p e s ,I m a g e s ,a n d T e x t FIGURE 9.8 Drawing strings within a canvas. Fonts and FontMetrics A font, as you probably know, is a collection of graphical representations of char- acters. You are probably very familiar with fonts such as Times New Roman or Arial. A graphics object has a font associated to it, implemented as a Font object. The Font class of the java.awt package represents fonts as you know them. The FontMetrics class has information about fonts that is useful to Graphics objects, such as the width and height of a string rendered with the current font. The Font Class The Font class encapsulates fields and methods that define fonts. The Font con- structor method used in this book is the Font(String name, int style, int size) constructor. The first argument is the name of the font. The second argu- ment is the style. Styles are represented by static integers in the font classes Font.PLAIN, Font.BOLD, and Font.ITALIC. The style can also be the combination (sum) Font.BOLD + Font.ITALIC. The third argument is the font point size. Here are a couple of examples: Font f1 = new Font("Timesroman", Font.PLAIN, 24); Font f2 = new Font("Helvetica", Font.BOLD + Font.ITALIC, 12); Table 9.1 lists some important fields and methods of the Font class. It is possible to get all of a system’s available fonts. Here’s how. First, you need to get the GraphicsEnvironment object that represents the system’s local graphics environment. The GraphicsEnvironment class is part of the java.awt package. The local graphics environment describes graphics devices and fonts that are available to the Java VM. The way to get the local graphics environment is to call the static method GraphicsEnvironment.getLocalGraphicsEnvironment(). Once you have the local GraphicsEnvironment object, you can call its getAllFonts() method, which returns an array of Font objects of point size 1. You can also call getAvailableFontFamilyNames() to return a String array containing the font family names of all the available fonts. Here’s a brief example: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontList = ge.getAvailableFontFamilyNames(); (20, 40) JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 327 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... 0); //An InterruptedException might occur try { mt.waitForID(0); TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The Graphics Class: Drawing Shapes, Images, and Text Description Chapter 9 Method JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 336 336 Java Programming for the Absolute Beginner } catch (InterruptedException ie)... vertical line drawn to the left of the string whose height is equal to the string’s height JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 334 Java Programming for the Absolute Beginner 334 FIGURE 9.10 Displaying two images within a canvas using the Graphics class /* * ImageTest * Demonstrates drawing Images */ import java. awt.*; public class ImageTest extends Canvas { public ImageTest() { super(); setSize(300,... base - ascent - leading, getSize().width, base - ascent - leading); //draw the height line g.drawLine(start - 10, base + descent, start - 10, base + descent - height); JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 332 Java Programming for the Absolute Beginner 332 g.setColor(Color.lightGray); g.fillRect(start, base + descent - height, width, height); The location of this rectangle must be relative to the...JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 328 328 Java Programming for the Absolute Beginner TA B L E 9 1 F ONT F I E L D S AND METHODS Field or Method Description static int BOLD Represents a bold style static int ITALIC Represents an italic... the frame to be the correct size based on the image’s size, there needs to be a way to wait for the images to load before setting the size of the frame (which packs the canvas in) This application does that Here is a listing of the source code for ImageTest2 .java: /* * ImageTest2 * Demonstrates how to wait for an image to load, how to * size the Frame initially based on the image size, and * how to... Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The Graphics Class: Drawing Shapes, Images, and Text Description Chapter 9 Method JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 330 Java Programming for the Absolute Beginner 330 if (args.length == 1) { fmt = new FontMetricsTest(args[0], "Timesroman"); } else if (args.length == 2) { fmt = new FontMetricsTest(args[0], args[1]);... width, descent + base + 10); //draw a line for the ascent g.drawLine(0, base - ascent, getSize().width, base - ascent); //draw a line for the descent g.drawLine(0, base + descent, getSize().width, base + descent); //draw the leading line TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM... FontMetrics Class */ import java. awt.*; import java. awt.event.*; public class FontMetricsTest extends Canvas implements ItemListener { protected String string, font; public FontMetricsTest(String s, String f) { super(); string = s; font = f; setSize(500, 250); setBackground(Color.white); } public static void main(String args[]) { FontMetricsTest fmt; TEAM LinG - Live, Informative, Non-cost and Genuine!... allows Component objects to receive image information Any component can be passed as an image observer Here, this is passed in because ImageTest extends Canvas, which extends Component and therefore is a Component and an ImageObserver The Image class represents graphical images Table 9.3 shows some of the more common methods of the Image class TEAM LinG - Live, Informative, Non-cost and Genuine! Please... characters will exceed these values A font’s leading is the space reserved for interline spacing (the space in between lines of text) This value is part of the calculation of a font’s height The TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-09.qxd 2/25/03 8:55 AM Page 329 329 TA B L E 9 2 F ONT M ETRICS . way to wait for the images to load before setting the size of the frame (which packs the canvas in). This application does that. Here is a listing of the source code for ImageTest2 .java: /* *. that are used for highlight- ing are dependent upon the current color. The method for drawing a 3D rectangle is draw3DRect(int x, int y, int width, int height, boolean raised) for drawing a. these methods to render two ovals. Here is the source code for OvalTest .java: /* * OvalTest * Demonstrates drawing Ovals */ import java. awt.*; public class OvalTest extends Canvas { public OvalTest()

Ngày đăng: 03/07/2014, 05:20

Tài liệu cùng người dùng

Tài liệu liên quan