1. Write a subclass of the graphicsprogram class and draw a pyramid, as shown in.
The number of bricks decreases by 1 as the tower height increases.
This pyramid should be placed at the bottom of the window, centered and aligned, and set the following constants:
Brick_width brick width (30 pixels)
Brick_height brick height (12 pixels)
Bricks_in_base bottom-layer bricks (14)
The number corresponding to the number in the brackets. This value should be modifiable during programming.
Code:
Import ACM. graphics. *; <br/> Import ACM. program. *; <br/> Import Java. AWT. *; <br/> public class pyramid extends graphicsprogram {<br/>/** width of each brick in pixels */<br/> Private Static final int brick_width = 30; <br/>/** width of each brick in pixels */<br/> Private Static final int brick_height = 12; <br/>/** Number of bricks in the base of the pyramid */<br/> Private Static final int bricks_in_base = 14; <br/> Public void run () {<br/> // obtain the window size <br/> int W = getwidth (); <br/> int H = getheight (); <br/> // obtain the X coordinate of the midpoint of the window <br/> int MX = W/2; </P> <p> // draw from top to bottom <br/> for (INT I = 1; I <= bricks_in_base; I ++) {<br/> int Yb = H-(bricks_in_base-I + 1) * brick_height; <br/> for (Int J = 1; j <= I; j ++) {<br/> int firstbrick = Mx-I * (brick_width/2); <br/> int XB = firstbrick + (I-j) * brick_width; <br/> grect B = new grect (XB, Yb, brick_width, brick_height); <br/> Add (B ); <br/>}</P> <p >}< br/>
2. Assume that you are employed to write a program and the content is to draw an image of an arrow target-if you want it to have a real business
You can also think of it as the logo of a national department store, as shown in
This image is just three simple applications of the goval class (the goval class is used to show an ellipse in the graphic window), that is
Draw two red, one white, and three circles in the correct order. The radius of the outermost circle should be 1 inch (72 pixels), and the radius of the white circle should be 0.65
The radius of the red circle on the innermost side is 0.3 inch. Center the image in the graphics program window.
Code:
Import ACM. graphics. *; <br/> Import ACM. program. *; <br/> Import Java. AWT. *; <br/> public class target extends graphicsprogram {</P> <p> Public static final int r = 72; </P> <p> Public void run () {<br/> // obtain the midpoint coordinates of the window <br/> int x = getwidth ()/2; <br/> int y = getheight ()/2; <br/> // circle one by one (and color) from the outside to the inside <br/> goval O1 = new goval (X-r/2, Y-r/2, R, r); <br/> o1.setfilled (true); <br/> o1.setcolor (color. red); <br/> Add (O1); <br/> goval O2 = new goval (X-R * 0.65/2, Y-R * 0.65/2, r x 0.65, r x 0.65); <br/> o2.setfilled (true); <br/> o2.setcolor (color. white); <br/> Add (O2); <br/> goval O3 = new goval (X-R * 0.3/2, Y-R * 0.3/2, r x 0.3, r x 0.3); <br/> o3.setfilled (true); <br/> o3.setcolor (color. red); <br/> Add (O3); </P> <p >}< br/>
The third is a bit confusing.
The computer was poisoned and the chicken was infected ..