Draw a Circle Within a Square Java
thirteen.2 Cartoon Simple Shapes
Although Java has a large number of very powerful methods for creating and manipulating images, most of these are well beyond the scope of this volume. We will exist 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 volition be using methods from the class java.awt.Graphics, just as nosotros did in the final section, where we were cartoon text. As was the case in cartoon text, the coordinate organization has its origin in the upper left corner of the drawing area and the coordinates of a point are the distances in pixels to the correct of and below the origin. One of the simplest geometric objects that we tin draw is a line segment. Although it is possible to describe line segments of various thicknesses and characteristics (wide, thin, dotted, dashed, rounded ends, so on), nosotros will only be drawing segments that are one pixel wide. To practice so, we use the method drawLine in which the 4 int parameters represent the coordinates of the ends of the segment.
Instance 1
The statementm.drawLine(20,thirty,400,300);volition draw a line segment from the point with coordinates (xx,30) to the point with coordinates (400,300). Rectangles and squares can be drawn in outline using drawRect or every bit solid regions using fillRect. Both methods have four int parameters: the commencement two requite the coordinates of the upper left corner of the rectangle and the final ii requite the width and height respectively.
Example 2
The following fragment draws a solid 40 x forty square in the upper left mitt corner of the cartoon region and the outline of a rectangle that is 100 pixels broad and 20 pixels loftier located just beneath and to the right of the square.thousand.fillRect(O,O,xl,40); g.drawRect(40,forty,100,20);The illustration shows a window containing these figures. The window is 400 pixels wide and 100 pixels high.
We can also draw ellipses and circles either in outline using drawOval or filled using fillOval. These methods likewise have four int parameters: the first two specify the coordinates of the betoken at the upper left corner of a rectangle that would but contain the figure while the last two requite the width and height respectively.
Instance 3
The following fragment first draws the outline of a rectangle and then draws the outline of an ellipse contained by the rectangle. Finally, information technology draws a filled circle inside the ellipse. The results are again displayed in a 400 ten 100 pixel window.thousand.drawRect(100,10,200,l); one thousand.drawOval(100,10,200,fifty); g.fillOval(175,10,50,50);Observe the horizontal coordinate of the left end of the circle. Since the left terminate of the ellipse is at 100 and the ellipse is 200 pixels broad, its center is located 100+ 200 -;-2 = 200 pixels from the left. For the circumvolve to exist centred on the ellipse, its center must also be 200 pixels from the left. The circle's diameter is l pixels so the left side of the circle is located 25 pixels (the circle's radius) to the left of the center of the ellipse. Thus the circle starts at a point 200 - 25 = 175 pixels from the left.
To depict polygons (in outline or filled) we tin can use drawPolygon or fillPolygon. Both methods have three parameters of the class (int x [] , int y [], int n) 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 gild) the points specified by the arrays. The polygon is automatically airtight by joining the last point to the first (if they are different). The line segments can cross. If they do, fillPolygon fills the inside of the resulting figure in a reasonable style.
Example four
The following fragment draws an open triangle and a filled quadrilateral.int [] xTri = {50,150, l}; int [] yTri = {20, threescore, 60}; thousand.drawPolygon(xTri,yTri,3); int [] xQuad = {200,320,300,220}; int [] yQuad = { 20, 40, 10, 50}; thou.fillPolygon(xQuad,yQuad,four);Hither are the resulting drawings.
Unless you have specified otherwise, drawing is done in black. To get other colours, y'all tin use objects from the class j ava. awt .Color. A colour in Coffee tin can be defined by an RGB colour specification scheme in which a colour is represented past three integers with a range of 0 to 255 indicating the corporeality of each of the three main colours (red, green, and blue) in the given colour. The Color class contains a number of predefined objects whose identifiers are shown in the post-obit tabular array.
If none of these colours is suitable, you can construct your own colours in a variety of means. One such way is to use a constructor of the Color class in which the RGB values are the arguments of the constructor.
Example 5
The following statement creates a Colour object representing lime dark-green.coffee.awt.Color lime = new coffee.awt.Color(128,255,O);
Of grade, by using a suitable import statement, this tin exist abbreviated to
Color lime = new Color(128,255,O);
At that place is also a four parameter version of the Color constructor. It allows yous to specify the Color'southward alpha 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 as the three parameter Color constructor).
Example 5B
Colour seeThroughRed = new Color(255,0,0,100);
If you draw a rectangle, say, in that color on summit of another shape, you volition still be able to encounter the other shape through the rectangle.
To draw something in a specified color, we tin utilise the case method setColor from the Color form . This method can exist used with either a predefined Colour object or one that we have defined.
Example half-dozen
The following method could exist used to draw a solid magenta ellipse in the top left corner of a window with a lime dark-green label on top of the ellipse.public void paint (Graphics g) { g.setColor(Color.magenta); m.fillOval(O,O,100,l); Colour lime = new Color(128,255,O); yard.setColor(lime); g.drawString("An Ellipse",25,30); }
Exercise xiii.ii
- If a region is 300 pixels wide and 200 pixels high, make up one's mind the coordinates of each point. (a) the upper left corner (b) the midpoint of the lesser (c) the centre of the left one-half (d) the heart of the lower right quadrant
- Write a pigment method that could be used to describe a foursquare each of whose sides are 100 pixels long. The foursquare should exist positioned so that it would be at the center of a window that is 400 pixels broad and 200 pixels high. The top and bottom lines defining the square should be green while the sides should exist red.
- Write a pigment method that could be used to draw a standard, octagonal stop sign that is 200 pixels wide.
- Write a paint method that could be used to draw the following blueprint of squares. The heart of the smallest square has coordinates(200,100) and the length of each of its sides is twenty pixels.
- Write a complete program containing a pigment method that draws a bull's center with a ruby-red centre circle, an orange ring effectually that, a yellow ring around that, and a green ring around that. The radius of the centre circle and the width of each of the surrounding rings should be 10 pixels. The bull's eye should exist located in the centre of a square region that is 100 pixels wide and 100 pixels loftier.
Source: http://ntci.on.ca/compsci/java/ch13/13_2.html
0 Response to "Draw a Circle Within a Square Java"
Postar um comentário