Chris Johnson, Index
Drawing


/*
Author:                 Chris Johnson
                        johnson@dcs.gla.ac.uk
 
Last modified:          06/01/2000
 
Description:    Provides users with a choice of shapes and a fill option.
                Updates the screen in response to these selections.

Notice that the structure of the applet is very similar to that of the font choice
test applet derived from Geary's Graphic Java p. 204 - Prentice Hall/Sunsoft, 1997.
*/
 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
 
public class GraphChoice extends Applet {
        private GraphicsChoicePanel graphicschoicepanel = new
        GraphicsChoicePanel(this);
        Canvas rectangle; // graphic area to illustrate drawing
        Checkbox filled;
        static boolean DEBUG = true;
 
        public void init()
        {
                rectangle = new Canvas();
                filled = new Checkbox("Filled?");
 
                setLayout(new BorderLayout(2,2));
        
                //Add components to the Applet.
                add(graphicschoicepanel, "North");
                add(rectangle, "Center");
                add(filled, "South");
 
        }
 
        public void updateGraphics(String item)
        {
 
        Graphics g = rectangle.getGraphics(); // get graphics handle of canvas
        Dimension the_size = size();
        Point the_position = location();
        int width = the_size.width/2;
        int height = the_size.height/2;
        int new_x = the_position.x+(the_size.width/4);
        int new_y = the_position.x+(the_size.width/4);
        g.setColor(Color.blue);
        g.clearRect(the_position.x, the_position.y, the_size.width,the_size.height);
 
        if (filled.getState()== false) {
                if (item.equals("draw3DRect(int, int, int, int, boolean)"))
                {
                        g.draw3DRect(new_x, new_y, width-1, height-1, true); 
                        g.draw3DRect(new_x+3, new_y+3, width-7, height-7, false);
                }
                else if (item.equals("drawRoundRect(int, int, int, int, int,int)"))
                        g.drawRoundRect(new_x, new_y,width, height,30, 30);
                        // last two parameters are the diameter of the arcs at the corners
                else if (item.equals("drawRect(int, int, int, int)"))
                        g.drawRect(new_x, new_y, width, height);
                else if (item.equals("drawOval(int, int, int, int)"))
                        g.drawOval(new_x+(new_x/2), new_y, width/2, height);
        }
        else {
                if (item.equals("draw3DRect(int, int, int, int, boolean)"))
                {
                        g.draw3DRect(new_x, new_y, width-1, height-1, true); 
                        g.fill3DRect(new_x+3, new_y+3, width-7, height-7, false);
                }
                else if (item.equals("drawRoundRect(int, int, int, int, int, int)"))
                        g.fillRoundRect(new_x, new_y, width, height, 30, 30);
                else if (item.equals("drawRect(int, int, int, int)"))
                        g.fillRect(new_x, new_y, width, height);
                else if (item.equals("drawOval(int, int, int, int)"))
                        g.fillOval(new_x+(new_x/2), new_y, width/2, height);
        }
 }
 
}// end of GraphChoice
 
class GraphicsChoicePanel extends Panel{
 
        private GraphChoice graphchoice;
        Choice choice; //pop-up list of choices
 
        public GraphicsChoicePanel(GraphChoice applet) {
                Listener listener = new Listener();
                graphchoice = applet;
 
                choice = new Choice();
                add(choice);
 
                choice.addItem("Select a shape to be drawn...");
                choice.addItem("drawRect(int, int, int, int)");
                choice.addItem("draw3DRect(int, int, int, int, boolean)");
                choice.addItem("drawRoundRect(int, int, int, int, int, int)");
                choice.addItem("drawOval(int, int, int, int)");
 
                choice.addItemListener(listener);
        }
 
        public class Listener implements ItemListener{
                public void itemStateChanged(ItemEvent event){
                        graphchoice.updateGraphics(choice.getSelectedItem());}
        }
}



forward