Archive for December, 2007

Make web site - 624 Graphics and Java2D Chapter 11 13 //

Monday, December 31st, 2007

624 Graphics and Java2D Chapter 11 13 // set window’s title bar String and dimensions 14 public DrawArcs() 15 { 16 super( “Drawing Arcs” ); 17 18 setSize( 300, 170 ); 19 setVisible( true ); 20 } 21 22 // draw rectangles and arcs 23 public void paint( Graphics g ) 24 { 25 // call superclass’s paint method 26 super.paint( g ); 27 28 // start at 0 and sweep 360 degrees 29 g.setColor( Color.yellow ); 30 g.drawRect( 15, 35, 80, 80 ); 31 g.setColor( Color.black ); 32 g.drawArc( 15, 35, 80, 80, 0, 360 ); 33 34 // start at 0 and sweep 110 degrees 35 g.setColor( Color.yellow ); 36 g.drawRect( 100, 35, 80, 80 ); 37 g.setColor( Color.black ); 38 g.drawArc( 100, 35, 80, 80, 0, 110 ); 39 40 // start at 0 and sweep -270 degrees 41 g.setColor( Color.yellow ); 42 g.drawRect( 185, 35, 80, 80 ); 43 g.setColor( Color.black ); 44 g.drawArc( 185, 35, 80, 80, 0, -270 ); 45 46 // start at 0 and sweep 360 degrees 47 g.fillArc( 15, 120, 80, 40, 0, 360 ); 48 49 // start at 270 and sweep -90 degrees 50 g.fillArc( 100, 120, 80, 40, 270, -90 ); 51 52 // start at 0 and sweep -270 degrees 53 g.fillArc( 185, 120, 80, 40, 0, -270 ); 54 } 55 56 // execute application 57 public static void main( String args[] ) 58 { 59 DrawArcs application = new DrawArcs(); 60 61 application.setDefaultCloseOperation( 62 JFrame.EXIT_ON_CLOSE ); 63 } 64 65 } // end class DrawArcs Fig. 11.19 Demonstrating drawArcand fillArc(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 11 Graphics and Java2D 623 Positive angles (Make web site)

Sunday, December 30th, 2007

Chapter 11 Graphics and Java2D 623 Positive angles Negative angles 90 90 180 0 180 0 270 270 Fig. 11.17Positive and negative arc angles. 11.17 Method Description public void drawArc( int x, int y, int width, int height, int startAngle, int arcAngle ) Draws an arc relative to the bounding rectangle s top-left coordinates (x, y) with the specified widthand height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees. public void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle ) Draws a solid arc (i.e., a sector) relative to the bounding rectangle s top-left coordinates (x, y) with the specified widthand height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees. Fig. 11.18 Graphicsmethods for drawing arcs. The program of Fig. 11.19 demonstrates the arc methods of Fig. 11.18. The program draws six arcs (three unfilled and three filled). To illustrate the bounding rectangle that helps determine where the arc appears, the first three arcs are displayed inside a yellow rectangle that has the same x, y, widthand heightarguments as the arcs. 1 // Fig. 11.19: DrawArcs.java 2 // Drawing arcs 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class DrawArcs extends JFrame { 12 Fig. 11.19 Demonstrating drawArcand fillArc(part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Affordable web design - 622 Graphics and Java2D Chapter 11 arc width

Saturday, December 29th, 2007

622 Graphics and Java2D Chapter 11 arc width height (x, y) arc height width Fig. 11.15The arc width and arc height for rounded rectangles. 11.15 The drawOvaland fillOvalmethods take the same four arguments. The first two arguments specify the top-left coordinate of the bounding rectangle that contains the oval. The last two arguments specify the widthand heightof the bounding rectangle, respectively. Figure 11.16 shows an oval bounded by a rectangle. Note that the oval touches the center of all four sides of the bounding rectangle (the bounding rectangle is not displayed on the screen). 11.6 Drawing Arcs An arc is a portion of a oval. Arc angles are measured in degrees. Arcs sweep from a starting angle the number of degrees specified by their arc angle. The starting angle indicates in degrees where the arc begins. The arc angle specifies the total number of degrees through which the arc sweeps. Figure 11.17 illustrates two arcs. The left set of axes shows an arc sweeping from zero degrees to approximately 110 degrees. Arcs that sweep in a counterclockwise direction are measured in positive degrees. The right set of axes shows an arc sweeping from zero degrees to approximately 110 degrees. Arcs that sweep in a clockwise direction are measured in negative degrees. Notice the dashed boxes around the arcs in Fig. 11.17. When drawing an arc, we specify a bounding rectangle for an oval. The arc will sweep along part of the oval. The Graphicsmethods drawArcand fillArcfor drawing arcs are summarized in Fig. 11.18. (x, y) height width Fig. 11.16An oval bounded by a rectangle. 11.16 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting support - Chapter 11 Graphics and Java2D 621 48 //

Saturday, December 29th, 2007

Chapter 11 Graphics and Java2D 621 48 // execute application 49 public static void main( String args[] ) 50 { 51 LinesRectsOvals application = new LinesRectsOvals(); 52 53 application.setDefaultCloseOperation( 54 JFrame.EXIT_ON_CLOSE ); 55 } 56 57 } // end class LinesRectsOvals drawRect drawLine fillRect draw3DRect fill3DRect fillRoundRect drawRoundRect drawOval fillOval Fig. 11.14 Demonstrating Graphicsmethod drawLine(part 2 of 2). Methods fillRoundRect (line 36) and drawRoundRect (line 37) draw rectangles with rounded corners. Their first two arguments specify the coordinates of the upper- left corner of the bounding rectangle the area in which the rounded rectangle will be drawn. Note that the upper-left corner coordinates are not the edge of the rounded rectangle, but the coordinates where the edge would be if the rectangle had square corners. The third and fourth arguments specify the widthand heightof the rectangle. Their last two arguments arcWidthand arcHeight determine the horizontal and vertical diameters of the arcs used to represent the corners. Methods draw3DRect (line 40) and fill3DRect (line 41) take the same arguments. The first two arguments specify the top-left corner of the rectangle. The next two arguments specify the width and height of the rectangle, respectively. The last argument determines whether the rectangle is raised (true) or lowered (false). The three- dimensional effect of draw3DRectappears as two edges of the rectangle in the original color and two edges in a slightly darker color. The three-dimensional effect of fill3DRectappears as two edges of the rectangle in the original drawing color and the fill and other two edges in a slightly darker color. Raised rectangles have the original drawing color edges at the top and left of the rectangle. Lowered rectangles have the original drawing color edges at the bottom and right of the rectangle. The three-dimensional effect is difficult to see in some colors. Figure 11.15 labels the arc width, arc height, width and height of a rounded rectangle. Using the same value for arcWidth and arcHeightproduces a quarter circle at each corner. When width, height, arcWidthand arcHeighthave the same values, the result is a circle. If the values for widthand heightare the same and the values of arc- Widthand arcHeightare 0, the result is a square. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web file server - 620 Graphics and Java2D Chapter 11 The application

Friday, December 28th, 2007

620 Graphics and Java2D Chapter 11 The application of Fig. 11.14 demonstrates drawing a variety of lines, rectangles, 3D rectangles, rounded rectangles and ovals. 1 // Fig. 11.14: LinesRectsOvals.java 2 // Drawing lines, rectangles and ovals 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class LinesRectsOvals extends JFrame { 12 13 // set window’s title bar String and dimensions 14 public LinesRectsOvals() 15 { 16 super( “Drawing lines, rectangles and ovals” ); 17 18 setSize( 400, 165 ); 19 setVisible( true ); 20 } 21 22 // display various lines, rectangles and ovals 23 public void paint( Graphics g ) 24 { 25 // call superclass’s paint method 26 super.paint( g ); 27 28 g.setColor( Color.red ); 29 g.drawLine( 5, 30, 350, 30 ); 30 31 g.setColor( Color.blue ); 32 g.drawRect( 5, 40, 90, 55 ); 33 g.fillRect( 100, 40, 90, 55 ); 34 35 g.setColor( Color.cyan ); 36 g.fillRoundRect( 195, 40, 90, 55, 50, 50 ); 37 g.drawRoundRect( 290, 40, 90, 55, 20, 20 ); 38 39 g.setColor( Color.yellow ); 40 g.draw3DRect( 5, 100, 90, 55, true ); 41 g.fill3DRect( 100, 100, 90, 55, false ); 42 43 g.setColor( Color.magenta ); 44 g.drawOval( 195, 100, 90, 55 ); 45 g.fillOval( 290, 100, 90, 55 ); 46 } 47 Fig. 11.14 Demonstrating Graphicsmethod drawLine(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 11 Graphics and Java2D 619 Method Description (Web design rates)

Thursday, December 27th, 2007

Chapter 11 Graphics and Java2D 619 Method Description public void fillRect( int x, int y, int width, int height ) Draws a solid rectangle with the specified widthand height. The top-left corner of the rectangle has the coordinate (x, y). public void clearRect( int x, int y, int width, int height ) Draws a solid rectangle with the specified widthand height in the current background color. The top-left corner of the rectangle has the coordinate (x, y). public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) Draws a rectangle with rounded corners in the current color with the specified widthand height. The arcWidthand arcHeightdetermine the rounding of the corners (see Fig. 11.15). public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) Draws a solid rectangle with rounded corners in the current color with the specified widthand height. The arcWidthand arcHeightdetermine the rounding of the corners (see Fig. 11.15). public void draw3DRect( int x, int y, int width, int height, boolean b ) Draws a three-dimensional rectangle in the current color with the specified widthand height. The top-left corner of the rectangle has the coordinates (x, y). The rectangle appears raised when b is true and is lowered when b is false. public void fill3DRect( int x, int y, int width, int height, boolean b ) Draws a filled three-dimensional rectangle in the current color with the specified widthand height. The top-left corner of the rectangle has the coordinates (x, y). The rectangle appears raised when b is true and is lowered when b is false. public void drawOval( int x, int y, int width, int height ) Draws an oval in the current color with the specified widthand height. The bounding rectangle s top-left corner is at the coordinates (x, y). The oval touches all four sides of the bounding rectangle at the center of each side (see Fig. 11.16). public void fillOval( int x, int y, int width, int height ) Draws a filled oval in the current color with the specified width and height. The bounding rectangle s top-left corner is at the coordinates (x, y). The oval touches all four sides of the bounding rectangle at the center of each side (see Fig. 11.16). Fig. 11.13 Graphicsmethods that draw lines, rectangles and ovals (part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Free web space - 618 Graphics and Java2D Chapter 11 51 52535455

Thursday, December 27th, 2007

618 Graphics and Java2D Chapter 11 51 52535455 56 } application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end class Metrics Fig. 11.12 Fig. 11.12 Obtaining font metric information (part 2 of 2). Line 29 creates and sets the current drawing font to a SansSerif, bold, 12-point font. Line 30 uses Graphicsmethod getFontMetricsto obtain the FontMetrics object for the current font. Line 31 uses an implicit call to class Font s toString method to output the string representation of the font. Lines 32 35 use FontMetric methods to obtain the ascent, descent, height and leading for the font. Line 37 creates a new Serif, italic, 14-point font. Line 38 uses a second version of Graphicsmethod getFontMetrics, which receives a Fontargument and returns a corresponding FontMetricsobject. Lines 41 44 obtain the ascent, descent, height and leading for the font. Notice that the font metrics are slightly different for the two fonts. 11.5 Drawing Lines, Rectangles and Ovals This section presents a variety of Graphics methods for drawing lines, rectangles and ovals. The methods and their parameters are summarized in Fig. 11.13. For each drawing method that requires a width and height parameter, the width and height must be nonnegative values. Otherwise, the shape will not display.. Method Description public void drawLine( int x1, int y1, int x2, int y2 ) Draws a line between the point (x1, y1) and the point (x2, y2). public void drawRect( int x, int y, int width, int height ) Draws a rectangle of the specified widthand height. The top- left corner of the rectangle has the coordinates (x, y). Fig. 11.13 Graphicsmethods that draw lines, rectangles and ovals (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web host 4 life - Chapter 11 Graphics and Java2D 617 The program

Wednesday, December 26th, 2007

Chapter 11 Graphics and Java2D 617 The program of Fig. 11.12 uses the methods of Fig. 11.11 to obtain font metric information for two fonts. 1 // Fig. 11.12: Metrics.java 2 // Demonstrating methods of class FontMetrics and 3 // class Graphics useful for obtaining font metrics. 4 5 // Java core packages 6 import java.awt.*; 7 import java.awt.event.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class Metrics extends JFrame { 13 14 // set window’s title bar String and dimensions 15 public Metrics() 16 { 17 super( “Demonstrating FontMetrics” ); 18 19 setSize( 510, 210 ); 20 setVisible( true ); 21 } 22 23 // display font metrics 24 public void paint( Graphics g ) 25 { 26 // call superclass’s paint method 27 super.paint( g ); 28 29 g.setFont( new Font( “SansSerif”, Font.BOLD, 12 ) ); 30 FontMetrics metrics = g.getFontMetrics(); 31 g.drawString( “Current font: ” + g.getFont(), 10, 40 ); 32 g.drawString( “Ascent: ” + metrics.getAscent(), 10, 55 ); 33 g.drawString( “Descent: ” + metrics.getDescent(), 10, 70 ); 34 g.drawString( “Height: ” + metrics.getHeight(), 10, 85 ); 35 g.drawString( “Leading: ” + metrics.getLeading(), 10, 100); 36 37 Font font = new Font( “Serif”, Font.ITALIC, 14 ); 38 metrics = g.getFontMetrics( font ); 39 g.setFont( font ); 40 g.drawString( “Current font: ” + font, 10, 130 ); 41 g.drawString( “Ascent: ” + metrics.getAscent(), 10, 145 ); 42 g.drawString( “Descent: ” + metrics.getDescent(), 10, 160); 43 g.drawString( “Height: ” + metrics.getHeight(), 10, 175 ); 44 g.drawString( “Leading: ” + metrics.getLeading(), 10, 190); 45 } 46 47 // execute application 48 public static void main( String args[] ) 49 { 50 Metrics application = new Metrics(); Fig. 11.12 Fig. 11.12 Obtaining font metric information (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

616 Graphics and Java2D Chapter 11 Portability Tip (Space web hosting)

Tuesday, December 25th, 2007

616 Graphics and Java2D Chapter 11 Portability Tip 11.3 Java provides several standardized font names and maps these into system-specific font names for portability. This is transparent to the programmer. Font methods are also available to test the style of the current font and are summarized in Fig. 11.8. The isPlain method returns true if the current font style is plain. The isBold method returns true if the current font style is bold. The isItalic method returns trueif the current font style is italic. Sometimes precise information about a font s metrics must be known such as height, descent (the amount a character dips below the baseline), ascent (the amount a character rises above the baseline) and leading (the difference between the descent of one line of text and the ascent of the line of text below it i.e., the interline spacing). Figure 11.10 illustrates some of the common font metrics. Note that the coordinate passed to drawString corresponds to the lower-left corner of the baseline of the font. Class FontMetrics defines several methods for obtaining font metrics. These methods and Graphics method getFontMetricsare summarized in Fig. 11.11. leading height ascent Xy1 baseline descent Fig. 11.10 Font metrics. Method Description public int getAscent() // FontMetrics class Returns a value representing the ascent of a font in points. public int getDescent() // FontMetrics class Returns a value representing the descent of a font in points. public int getLeading() // FontMetrics class Returns a value representing the leading of a font in points. public int getHeight() // FontMetrics class Returns a value representing the height of a font in points. public FontMetrics getFontMetrics() // Graphics class Returns the FontMetricsobject for the current drawing Font. public FontMetrics getFontMetrics( Font f ) // Graphics class Returns the FontMetricsobject for the specified Fontargument. Fig. 11.11 FontMetricsand Graphicsmethods for obtaining font metrics. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 11 Graphics and Java2D 615 (X web hosting) 33 //

Monday, December 24th, 2007

Chapter 11 Graphics and Java2D 615 33 // set current font to Monospaced (Courier), 34 // italic, 24pt and draw a string 35 g.setFont( new Font( “Monospaced”, Font.ITALIC, 24 ) ); 36 g.drawString( “Monospaced 24 point italic.”, 20, 70 ); 37 38 // set current font to SansSerif (Helvetica), 39 // plain, 14pt and draw a string 40 g.setFont( new Font( “SansSerif”, Font.PLAIN, 14 ) ); 41 g.drawString( “SansSerif 14 point plain.”, 20, 90 ); 42 43 // set current font to Serif (times), bold/italic, 44 // 18pt and draw a string 45 g.setColor( Color.red ); 46 g.setFont( 47 new Font( “Serif”, Font.BOLD + Font.ITALIC, 18 ) ); 48 g.drawString( g.getFont().getName() + ” ” + 49 g.getFont().getSize() + 50 ” point bold italic.”, 20, 110 ); 51 } 52 53 // execute application 54 public static void main( String args[] ) 55 { 56 Fonts application = new Fonts(); 57 58 application.setDefaultCloseOperation( 59 JFrame.EXIT_ON_CLOSE ); 60 } 61 62 } // end class Fonts Fig. 11.9 Using Graphicsmethod setFontto change Fonts (part 2 of 2). Software Engineering Observation 11.3 To change the font, you must create a new Font object; there are no set methods in class Fontto change the characteristics of the current font. Often, it is necessary to get information about the current font, such as the font name, the font style and the font size. Several Fontmethods used to get font information are summarized in Fig. 11.8. Method getStylereturns an integer value representing the current style. The integer value returned is either Font.PLAIN, Font.ITALIC, Font.BOLD or any combination of Font.PLAIN, Font.ITALICand Font.BOLD. Method getSize returns the font size in points. Method getName returns the current font name as a String. Method getFamilyreturns the name of the font family to which the current font belongs. The name of the font family is platform specific. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01