Java Program to Draw Circle and Rectangle
This section presents Graphics methods for drawing lines, rectangles and ovals. The methods and their parameters are summarized in Fig. 12.17. For each drawing method that requires a width and meridian parameter, the width and meridian must exist nonnegative values. Otherwise, the shape will non brandish.
Method | Description |
---|---|
public void drawLine( int x1, int y1, int x2, int y2 ) | |
Draws a line between the point (x1, y1) and the bespeak (x2, y2). | |
public void drawRect( int 10, int y, int width, int height) | |
Draws a rectangle of the specified width and pinnacle. The top-left corner of the rectangle has the coordinates (x, y). Only the outline of the rectangle is drawn using the Graphics object'southward colorthe body of the rectangle is not filled with this color. | |
public void fillRect( int 10, int y, int width, int tiptop ) | |
Draws a filled rectangle with the specified width and summit. The top-left corner of the rectangle has the coordinate (x, y). The rectangle is filled with the Graphics object'southward color. | |
public void clearRect ( int 10, int y, int width, int height ) | |
Draws a filled rectangle with the specified width and acme in the electric current background color. The top-left corner of the rectangle has the coordinate (10, y). This method is useful if the developer wants to remove a portion of an image. | |
public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) | |
Draws a rectangle with rounded corners in the electric current color with the specified width and height. The arcWidth and arcHeight determine the rounding of the corners (come across Fig. 12.xx). Merely the outline of the shape is drawn. | |
public void fillRoundRect( int 10, int y, int width, int height, int arcWidth, int arcHeight ) | |
Draws a filled rectangle with rounded corners in the current color with the specified width and acme. The arcWidth and arcHeight determine the rounding of the corners (meet Fig. 12.xx). | |
public void draw3DRect( int x, int y, int width, int pinnacle, boolean b ) | |
Draws a 3-dimensional rectangle in the current color with the specified width and superlative. The top-left corner of the rectangle has the coordinates (ten, y). The rectangle appears raised when b is true and lowered when b is false. Simply the outline of the shape is drawn. | |
public void fill3DRect( int x, int y, int width, int height, boolean b ) | |
Draws a filled iii-dimensional rectangle in the current color with the specified width and height. The pinnacle-left corner of the rectangle has the coordinates (ten, y). The rectangle appears raised when b is true and 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 width and height. The bounding rectangle'south top-left corner is at the coordinates (v, y). The oval touches all iv sides of the bounding rectangle at the center of each side (run across Fig. 12.21). Merely the outline of the shape is drawn. | |
public void fillOval( int x, int y, int width, int height ) | |
Draws a filled oval in the current color with the specified width and acme. The bounding rectangle's pinnacle-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. 12.21). |
The awarding of Fig. 12.18Fig. 12.nineteen demonstrates drawing a variety of lines, rectangles, 3-dimensional rectangles, rounded rectangles and ovals.
Figure 12.18. Drawing lines, rectangles and ovals.
(This detail is displayed on folio 614 in the print version)
1 // Fig. 12.eighteen: LinesRectsOvalsJPanel.java 2 // Drawing lines, rectangles and ovals. 3 import coffee.awt.Color; 4 import coffee.awt.Graphics; 5 import javax.swing.JPanel; 6 7 public class LinesRectsOvalsJPanel extends JPanel 8 { nine // brandish various lines, rectangles and ovals 10 public void paintComponent( Graphics k ) 11 { 12 super.paintComponent( grand ); // call superclass's pigment method 13 14 this.setBackground( Color.WHITE ); 15 16 thousand.setColor( Color.RED ); 17 thou.drawLine( 5, 30, 380, 30 ); 18 nineteen g.setColor( Colour.BLUE ); 20 grand.drawRect( 5, xl, 90, 55 ); 21 g.fillRect( 100, forty, 90, 55 ); 22 23 k.setColor( Colour.CYAN ); 24 one thousand.fillRoundRect( 195, twoscore, 90, 55, fifty, 50 ); 25 g.drawRoundRect( 290, forty, ninety, 55, 20, twenty ); 26 27 g.setColor( Colour.YELLOW ); 28 g.draw3DRect( v, 100, 90, 55, true ); 29 g.fill3DRect( 100, 100, ninety, 55, false ); thirty 31 thou.setColor( Colour.MAGENTA ); 32 g.drawOval( 195, 100, ninety, 55 ); 33 1000.fillOval( 290, 100, ninety, 55 ); 34 } // end method paintComponent 35 } // finish grade LinesRectsOvalsJPanel
Figure 12.nineteen. Creating JFrame to display lines, rectangles and ovals.
(This item is displayed on folio 615 in the impress version)
1 // Fig. 12.19: LinesRectsOvals.java 2 // Drawing lines, rectangles and ovals. iii import java.awt.Color; iv import javax.swing.JFrame; 5 6 public class LinesRectsOvals 7 { viii // execute awarding 9 public static void main( Cord args[] ) ten { xi // create frame for LinesRectsOvalsJPanel 12 JFrame frame = 13 new JFrame( "Drawing lines, rectangles and ovals" ); fourteen frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 15 16 LinesRectsOvalsJPanel linesRectsOvalsJPanel = 17 new LinesRectsOvalsJPanel(); xviii linesRectsOvalsJPanel.setBackground( Color.WHITE ); 19 frame.add together( linesRectsOvalsJPanel ); // add panel to frame 20 frame.setSize( 400, 210 ); // set frame size 21 frame.setVisible( true ); // display frame 22 } // terminate chief 23 } // end class LinesRectsOvals
![]() |
In Fig. 12.18, line 17 draws a cherry line, line xx draws an empty blueish rectangle and line 21 draws a filled bluish rectangle. Methods fillRoundRect (line 24) and drawRoundRect (line 25) draw rectangles with rounded corners. Their starting time two arguments specify the coordinates of the upper-left corner of the bounding rectanglethe area in which the rounded rectangle will exist drawn. Annotation that the upper-left corner coordinates are non the edge of the rounded rectangle, just the coordinates where the border would be if the rectangle had foursquare corners. The 3rd and fourth arguments specify the width and pinnacle of the rectangle. The terminal two arguments determine the horizontal and vertical diameters of the arc (i.eastward., the arc width and arc acme) used to represent the corners.
Figure 12.20 labels the arc width, arc height, width and height of a rounded rectangle. Using the same value for the arc width and arc top produces a quarter circle at each corner. When the arc width, arc elevation, width and height accept the aforementioned values, the result is a circumvolve. If the values for width and elevation are the same and the values of arcWidth and arcHeight are 0, the effect is a square.
Figure 12.20. Arc width and arc height for rounded rectangles.
(This detail is displayed on folio 615 in the print version)
Methods draw3DRect (line 28) and fill3DRect (line 29) take the same arguments. The first two arguments specify the top-left corner of the rectangle. The next ii arguments specify the width and height of the rectangle, respectively. The last argument determines whether the rectangle is raised (true) or lowered (simulated). The three-dimensional effect of draw3DRect appears as 2 edges of the rectangle in the original color and two edges in a slightly darker color. The three-dimensional effect of fill3DRect appears equally ii edges of the rectangle in the original cartoon color and the fill and other two edges in a slightly darker color. Raised rectangles accept the original cartoon colour edges at the tiptop and left of the rectangle. Lowered rectangles accept the original drawing color edges at the bottom and right of the rectangle. The three-dimensional issue is difficult to run across in some colors.
Methods drawOval and fillOval (lines 3233) 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 width and pinnacle of the bounding rectangle, respectively. Figure 12.21 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 non displayed on the screen.)
Figure 12.21. Oval bounded by a rectangle.
williamssuccionoth.blogspot.com
Source: https://flylib.com/books/en/2.254.1/drawing_lines_rectangles_and_ovals.html
0 Response to "Java Program to Draw Circle and Rectangle"
إرسال تعليق