import java.applet.* ; import java.awt.* ; import java.awt.event.* ; public class sample05 extends Applet implements ActionListener, ItemListener, MouseListener { int shapeID = 0 ; // 0:Circle 1:Square 2:Cross int colorID = 0 ; Color[] colorTable = {Color.black,Color.red,Color.green,Color.blue} ; Button clearButton = new Button("Clear") ; Choice shapeChoice = new Choice() ; Choice colorChoice = new Choice() ; String[] itemShape = {"Circle","Square","Cross"} ; String[] itemColor = {"Black", "Red", "Green", "Blue"} ; int lastX=-1, lastY=-1 ; boolean clearFlag = true ; public void init() { int i ; for( i=0 ; i<3 ; i++ ) { shapeChoice.addItem(itemShape[i]) ; } for( i=0 ; i<4 ; i++ ) { colorChoice.addItem(itemColor[i]) ; } add(clearButton) ; add(shapeChoice) ; add(colorChoice) ; clearButton.addActionListener(this) ; shapeChoice.addItemListener(this) ; colorChoice.addItemListener(this) ; addMouseListener(this) ; setBackground(Color.white) ; } public void update(Graphics g) { paint(g) ; } public void paint(Graphics g) { if( clearFlag ) { g.setColor(Color.white) ; g.fillRect(0,0,getSize().width,getSize().height) ; g.setColor(Color.black) ; g.drawRect(0,0,getSize().width-1,getSize().height-1) ; clearFlag = false ; } else { g.setColor(colorTable[colorID]) ; switch( shapeID ) { case 0: g.fillOval(lastX-3,lastY-3,7,7) ; break ; case 1: g.fillRect(lastX-3,lastY-3,7,7) ; break ; case 2: g.drawLine(lastX-3,lastY,lastX+3,lastY) ; g.drawLine(lastX,lastY-3,lastX,lastY+3) ; break ; } } } public void actionPerformed(ActionEvent e) { if( e.getSource() == clearButton ) { clearFlag = true ; repaint() ; } } public void itemStateChanged(ItemEvent e) { if( e.getSource() == shapeChoice ) { shapeID = shapeChoice.getSelectedIndex() ; } else if( e.getSource() == colorChoice ) { colorID = colorChoice.getSelectedIndex() ; } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) { lastX = e.getX() ; lastY = e.getY() ; repaint() ; } }