/* CSC 15 Lab 3. Due one week from date assigned NOTE: There's a separate sheet attached to this lab! For this assignment you are to draw a stick figure like the one shown in class. It should have a round head, nose, mouth, torso, arms, and legs. You may add other features as you see fit. First download this file from the class homepage and make sure you can compile and run it. It draws a diamond. Find the two methods "drawfigure" and "draw" towards the bottom of this program. There's already code in the methods, but you are to erase (or comment them out) and replace it with your own code. First, write the drawfigure method so that instead of a diamond it draws a stick figure. Note that variables that control the attributes of the figure are declared not in the body of the method but as its parameters. This will allow you to draw different figures with each call of the method, without having to change how the method is implemented. After you've written the drawfigure method, running the program will draw one stick figure. You are now to change the draw method to make it call drawfigure multiple times. Draw several figures and vary the attributes of the figures (position, size, arm and mouth positions, and color). The object 'brush' manages the drawing of shapes onto a display area. It contains of many functions that controls how drawing is to be done. You can use the following basic drawing statements in your code: brush.drawLine(x1,y2,x2,y2); : this statement draws a line between the two point coordinates x1,y1 and x2,y2 brush.drawRect(x,y,w,h); : This statement draws a rectangle with upper left corner at coordinate x,y, and has width w and height h. Note that the last two argument does not represent a coordinate; they are relative to x and y. brush.drawOval(x,y,w,h); : x,y,w, and h are used the same way as they are in drawRect, expect that a ellipse will be drawn in the area. To draw a circle, make sure that w and h are the same. To draw a circle with its center at x0,y0 and a radius of r, use brush.drawOval(x0-r,y0-r,2*r,2*r); You can also draw a circle by specifying the center coordinates and the radius via (without brush. this time!) drawCircle(x,y,r); where x,y is the center of the circle and r is the radius. Note that there is no "brush." in front of drawCircle. That's because it's a function I defined for you, brush.drawArc(x,y,w,h,sa,al); : Draws an arc - part of an ellipse. The first four parameters define an ellipse as before. sa is the start angle of the arc (0 will be horizontal, 90 vertical). al is the "arc length" expressed in units of 1/16 of degrees. Confusing? Try it and see what happens!!! brush.setColor(Color.red); : Changes drawing color. The most popular colors can be chosen this way, by name. For example, Color.yellow, Color.pink are OK. To have more than just the common colors, use the following: brush.setColor(new Color(r,g,b)); where r, g and b are values representing the intensity of red, green and blue respectively. These three values can each range from 0 to 255. For example, new Color(0,0,255) gives you blue, and new Color(255,255,255) is the same as white. There are also commands brush.fillRect, and brush.fillOval that will draw a solid rectangle or oval instead of just the outline - use them as you would use brush.drawRect and brush.drawOval. Remember that the size of the window is 800X600 pixels - this means that the range of the x coordinate is 0-799, and for the y coordinate 0-599. Coordinate 0,0 represents the top left hand corner of the window. coordinate 0,599 represents the lower left hand corner, etc... For commenting, make clear as to what part of the anatomy you're drawing with each command. If you have additional code, you need to comment on what they're for too. Also make sure that all variables are clearly commented. ********** USE OF VARIABLES ************ You must use the parameter variables to represent the values of the coordinates and dimensions of your figure. By using the variables, you can change various attributes of the figure by just changing the value of one or two varibles, without touching the rest of the program. Refer to the diagram and demonstration given in class. You can print your stick figures by clicking on "print". Enter hp5si-ad-204 as the name of the printer (in Adams 204). Turn in: clear sketch of figure with calculated coordinates on paper print out of picture produced by program source code of your program, with these instructions deleted. BE CAREFUL!!!!!! Remember that integer division throws away the reminder. It's not appropriate to use doubles in this program since screen coordinates are in integers. But let's say you want to compute three-fourth times the variable scale, if you write the expression as (3/4)*scale, the value will be ZERO! That's because 3/4 throws away the remainder. To get the value you want, use (3*scale)/4 instead. */ import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; public class lab3 extends JFrame implements ActionListener { private Graphics brush; private Graphics display; private Image canvas; // off-screen graphics buffer private JMenuBar menubar; private JMenu printmenu; private JMenuItem pitem; public void paint(Graphics g) { super.paint(g); display.drawImage(canvas,0,50,null); } // overrides auto-update public lab3() { menubar = new JMenuBar(); printmenu = new JMenu("print"); pitem = new JMenuItem("print"); pitem.addActionListener(this); printmenu.add(pitem); menubar.add(pitem); setJMenuBar(menubar); setSize(800,650); setVisible(true); canvas = createImage(800,600); // double buffer brush = canvas.getGraphics(); // display = this.getGraphics(); brush.setColor(Color.white); // clear brush.fillRect(0,0,800,600); // with white background brush.setColor(Color.blue); // this.draw(); // display.drawImage(canvas,0,50,null); // draws to screen } public void init() { display = this.getGraphics(); display.drawImage(canvas,0,50,null); // draws to screen } public static void main(String[] args) // needed for application { lab3 session = new lab3(); session.init(); session.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); } private void drawCircle(int cx, int cy, int radius) { brush.drawOval(cx-radius,cy-radius,2*radius,2*radius); } public void actionPerformed(ActionEvent e) // printing { PrintJob laser = Toolkit.getDefaultToolkit().getPrintJob(this,"stickfig",null); Graphics pg = laser.getGraphics(); pg.drawImage(canvas,0,0,null); pg.dispose(); laser.end(); display.drawImage(canvas,0,50,null); // draws to screen again } // actionPerformed // your function should draw a stick figure centered at // coordinate x,y, be of size scale, and color c: public void drawfigure(int x, int y, int scale, Color c) { // ******************** Your code goes here *************** /* The following sample code draws a diamond, you should replace it with code to draw a stick figure. */ brush.setColor(c); brush.drawLine(x,y-scale/2,x+scale/2,y); brush.drawLine(x+scale/2,y,x,y+scale/2); brush.drawLine(x,y+scale/2,x-scale/2,y); brush.drawLine(x-scale/2,y,x,y-scale/2); } // end drawfigure method public void draw() // student defined method { // ******************** Your code goes here *************** this.drawfigure(300,300,40,Color.red); // call drawfigure multiple times varying the possible // position, color, and other attributes of the figure. } // end draw method } // class lab3