Javascript required
Skip to content Skip to sidebar Skip to footer

Drawing a Circle With Drawpolygon Java

13.ii Drawing Simple Shapes

Although Java has a large number of very powerful methods for creating and manipulating images, nigh of these are well beyond the scope of this volume. We will be examining only a tiny fraction of the methods - those that can exist used to create simple shapes in solid colours. To create our drawings, we will exist using methods from the grade coffee.awt.Graphics, simply equally we did in the last section, where we were cartoon text. As was the case in drawing text, the coordinate system has its origin in the upper left corner of the drawing area and the coordinates of a betoken are the distances in pixels to the correct of and below the origin. One of the simplest geometric objects that nosotros can draw is a line segment. Although it is possible to depict line segments of diverse thicknesses and characteristics (wide, thin, dotted, dashed, rounded ends, and so on), we will only be cartoon segments that are one pixel wide. To do so, we use the method drawLine in which the four int parameters correspond the coordinates of the ends of the segment.

Example 1

The argument
g.drawLine(20,30,400,300);
will depict a line segment from the betoken with coordinates (xx,30) to the signal with coordinates (400,300). Rectangles and squares tin be drawn in outline using drawRect or every bit solid regions using fillRect. Both methods take four int parameters: the first 2 requite the coordinates of the upper left corner of the rectangle and the last ii give the width and height respectively.

Case 2

The post-obit fragment draws a solid 40 ten twoscore square in the upper left mitt corner of the cartoon region and the outline of a rectangle that is 100 pixels wide and 20 pixels high located only below and to the right of the square.
thou.fillRect(O,O,40,xl); m.drawRect(40,40,100,20);              
The analogy shows a window containing these figures. The window is 400 pixels wide and 100 pixels high.

We can as well describe ellipses and circles either in outline using drawOval or filled using fillOval. These methods also have four int parameters: the starting time two specify the coordinates of the bespeak at the upper left corner of a rectangle that would just contain the figure while the last two give the width and summit respectively.

Example 3

The following fragment starting time draws the outline of a rectangle and then draws the outline of an ellipse independent by the rectangle. Finally, it draws a filled circle inside the ellipse. The results are again displayed in a 400 x 100 pixel window.
g.drawRect(100,x,200,50); yard.drawOval(100,x,200,50); g.fillOval(175,10,50,l);              
Notice the horizontal coordinate of the left end of the circle. Since the left stop of the ellipse is at 100 and the ellipse is 200 pixels wide, its centre is located 100+ 200 -;-2 = 200 pixels from the left. For the circumvolve to exist centred on the ellipse, its eye must also be 200 pixels from the left. The circle's diameter is 50 pixels so the left side of the circumvolve is located 25 pixels (the circle'south radius) to the left of the centre of the ellipse. Thus the circumvolve starts at a point 200 - 25 = 175 pixels from the left.

To draw polygons (in outline or filled) nosotros tin can utilise drawPolygon or fillPolygon. Both methods accept iii parameters of the form (int x [] , int y [], int due north) The arrays 10 and y specify the coordinates of the vertices of the polygon while n specifies the number of vertices. The polygon is formed using line segments joining (in lodge) the points specified by the arrays. The polygon is automatically closed by joining the final bespeak to the first (if they are unlike). The line segments tin can cross. If they practise, fillPolygon fills the inside of the resulting figure in a reasonable fashion.

Example four

The following fragment draws an open up triangle and a filled quadrilateral.
                                  int                [] xTri = {50,150, 50};                                  int                [] yTri = {20, 60, lx}; g.drawPolygon(xTri,yTri,3);                                  int                [] xQuad = {200,320,300,220};                                  int                [] yQuad = { xx, twoscore, 10, 50}; g.fillPolygon(xQuad,yQuad,four);              
Hither are the resulting drawings.

Unless yous have specified otherwise, cartoon is done in black. To get other colours, you tin can utilize objects from the class j ava. awt .Color. A colour in Java can be divers by an RGB colour specification scheme in which a colour is represented by iii integers with a range of 0 to 255 indicating the amount of each of the three chief colours (red, greenish, and bluish) in the given colour. The Colour class contains a number of predefined objects whose identifiers are shown in the following table.

If none of these colours is suitable, you can construct your own colours in a multifariousness of ways. One such mode is to use a constructor of the Color form in which the RGB values are the arguments of the constructor.

Case 5

The following statement creates a Color object representing lime dark-green.

java.awt.Colour lime = new java.awt.Color(128,255,O);

Of course, by using a suitable import statement, this tin can be abbreviated to

Color lime = new Color(128,255,O);

In that location is also a four parameter version of the Color constructor. It allows you lot to specify the Color's blastoff value. This is a number between 0-255 that specifies how transparent a Color is. The value 0 represents completely transparent (invisible) aand 255 is completely opaque (the same every bit the 3 parameter Colour constructor).

Instance 5B

Color seeThroughRed =                                      new                                    Color(255,0,0,100);                

If y'all describe a rectangle, say, in that color on acme of another shape, y'all will still be able to see the other shape through the rectangle.

To draw something in a specified color, we can use the instance method setColor from the Color class . This method tin can exist used with either a predefined Color object or one that we have defined.

Example 6

The following method could be used to draw a solid magenta ellipse in the top left corner of a window with a lime green label on top of the ellipse.
                                      public                                                        void                                    paint (Graphics g) {    chiliad.setColor(Color.magenta);    yard.fillOval(O,O,100,50);    Color lime =                                      new                                    Color(128,255,O);    g.setColor(lime);    1000.drawString("An Ellipse",25,xxx); }                

Practice thirteen.two

  1. If a region is 300 pixels wide and 200 pixels high, determine the coordinates of each betoken. (a) the upper left corner (b) the midpoint of the bottom (c) the centre of the left half (d) the heart of the lower right quadrant
  2. Write a paint method that could be used to depict a square each of whose sides are 100 pixels long. The square should exist positioned so that it would exist at the middle of a window that is 400 pixels broad and 200 pixels high. The top and bottom lines defining the foursquare should be dark-green while the sides should be red.
  3. Write a paint method that could be used to draw a standard, octagonal stop sign that is 200 pixels wide.
  4. Write a paint method that could be used to describe the post-obit pattern of squares. The middle of the smallest square has coordinates(200,100) and the length of each of its sides is 20 pixels.
  5. Write a complete program containing a paint method that draws a balderdash's eye with a red eye circle, an orange ring around that, a yellow ring effectually that, and a greenish band around that. The radius of the centre circumvolve and the width of each of the surrounding rings should be 10 pixels. The bull's center should be located in the eye of a square region that is 100 pixels wide and 100 pixels loftier.

antonconferverd57.blogspot.com

Source: http://ntci.on.ca/compsci/java/ch13/13_2.html