The second rectangle upper-right is filled with the color blue at position 250, 10 with dimensions 200 by 150.. The method for drawhighlight-ing a 3D rectangle is draw3DRectint x, int y,
Trang 1and 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
s o
l ut
n e
FIGURE 9.3
Drawing rectangles
is the coolest.
(300, 250)
150
150
150
200
150
100
50 75
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);
Trang 2frame.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 drawhighlight-ing a 3D rectangle is draw3DRect(int x, int y, int width, int height, boolean
rec-tangle is fill3DRect(int x, int y, int width, int height, boolean raised)
/*
* Rect3DTest
* Draws 3D Rectangles
*/
import java.awt.*;
public class Rect3DTest extends Canvas { public Rect3DTest() {
super();
319
i n
Trang 3setSize(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 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
s o
l ut
n e
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.
H I N T
FIGURE 9.4
3D Rectangles can
be raised or not.
Trang 4Drawing 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,
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
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
i n
Trang 5Drawing 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,
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 counter-clockwise direction The fillArc(int x, int y, int width, int height, int startAngle, int
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
arcs
/*
* ArcTest
* Demonstrates drawing Arcs
*/
import java.awt.*;
public class ArcTest extends Canvas { public ArcTest() {
super();
setSize(300, 200);
322
J a
s o
l ut
n e
FIGURE 9.5
Two ovals, one filled, and one drawn.
(10, 10)
100
100
Trang 6} 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
i n
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, yPointsthat correspond to
FIGURE 9.6
Drawing arcs is like drawing semi-circles.
(10, 10)
100
100
0 °
270 °
Trang 7the x-coordinate points by the same array index The third argument speci-fies the number of points of the polygon
• The other two methods are the draw methods’ corresponding fill methods and fill the polygonal shapes with the current color
/*
* 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);
} }
324
J a
s o
l ut
n e
Trang 8this particular book, so I’ll just go over how I used it here I created a new
were used to draw the triangle shown in Figure 9.7
325
i n
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.
T R I C K
T R A P
(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.
Trang 9Drawing Strings
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
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);
} }
application draws a bunch of strings onto the Canvas You can see the output in Figure 9.8
326
J a
s o
l ut
n e
Trang 10i n
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 Fontobject
The Fontclass of the java.awtpackage represents fonts as you know them The FontMetricsclass has information about fonts that is useful to Graphicsobjects, such as the width and height of a string rendered with the current font
The Font Class
The Fontclass 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 Fontclass
It is possible to get all of a system’s available fonts Here’s how First, you need to get the GraphicsEnvironmentobject 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 GraphicsEnvironmentobject, you can call its getAllFonts() method, which returns an array of Fontobjects 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)