Draw Graphic Demo
 

Web Configuration:

<applet code="DrawGraphic.class" codebase="applet" width=300 height=300>
</applet>

Source Code:


import java.awt.*;
import java.awt.event.*;

public class DrawGraphic extends java.applet.Applet {

    String[] figureNames = {"circle", "oval", "sqare", "rectangle"};
    String[] colorNames = {"red", "green", "blue", "yellow"};
    Color[] colorValues = {Color.red, Color.green, Color.blue,
                           Color.yellow};
    Choice chFigure = new Choice();
    Choice chColor = new Choice();
    int curX, curY;

    public void init () {
        Panel p = new Panel(new GridLayout(1,0));

        for (int i = 0; i<figureNames.length; i++){
             chFigure.addItem(figureNames[i]);
        }

        for (int i = 0; i<colorNames.length;i++){
             chColor.addItem(colorNames[i]);
        }

        p.add(chColor);
        p.add(chFigure);

        setLayout(new BorderLayout());
        add(p, BorderLayout.NORTH);

        addMouseListener(new MouseEventListener());
    }

    public void update(Graphics g){
        g.setColor(colorValues[chColor.getSelectedIndex()]);

        switch (chFigure.getSelectedIndex()){
            case 0:
                g.fillOval(curX,curY,30,30);
                break;
            case 1:
                g.fillOval(curX, curY, 30,50);
                break;
            case 2:
                g.fillRect(curX,curY,30,30);
                break;
            case 3:
                g.fillRect(curX,curY,30,50);
                break;
        }
    }

    class MouseEventListener extends MouseAdapter{
        public void mousePressed(MouseEvent e){
            curX = e.getX();
            curY = e.getY();
            repaint();
        }
    }
}

Copyright©2002, All Right Reserved

Department of Geography, San Diego State University