buttons.java Code

/*
 * buttons.java : Demonstrates applet with buttons. Clicking the Square
 * button  displays a square in the center of the applet, clicking the
 * Circle button displays a circle, etc. Use the right, left up and down
 * buttons to move the figures around. Uses the JDK 1.1 ActionListner model
 * to sense events. Created on August 29, 2000, 7:21 PM
 */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Font.*;

/**
 * @author  hunt,jan
 * @version 
 */
public class buttons extends java.applet.Applet implements ActionListener {
    // Declare variables for the buttons and to track figure.
    private Button btnMyApplet;
    private Button btnSquare;
    private Button btnCircle;
    private Button btnRndSquare;
    private Button btnLeft;
    private Button btnUp;
    private Button btnRight;
    private Button btnDown;
    private Button btnBackColor;
    private Button btnRefresh;
    private int figure = 100;
    private int x = 150;
    private int y = 100;
    private int w = 50;
    private int h = 50;
    private int flag = 0;
    
    //private Font DEFAULT_FONT = new Font("verdana",Font.BOLD,12);    
    /**
    * @param args the command line arguments
    */
    public void init () {
    //Create my Applet button, add it to the container, set up listening;    
    btnMyApplet = new Button("My Applet");
    add(btnMyApplet);
    btnMyApplet.addActionListener(this);
        
    //Create Square button, add it to the container, set up listening;
    btnSquare = new Button("Square");
    add(btnSquare);
    btnSquare.addActionListener(this);
    
    //Create Circle button, add it to the container, set up listening;
    btnCircle = new Button("Circle");
    add(btnCircle);
    btnCircle.addActionListener(this);
    
    //Create round square, add it to the container, set up listening;
    btnRndSquare = new Button("RndSquare");
    add(btnRndSquare);
    btnRndSquare.addActionListener(this);
    
    //Create Left button, add it to the container, set up listening;
    btnLeft = new Button("Left");
    add(btnLeft);
    btnLeft.addActionListener(this);
    
    //Create Up button, add it to the container, set up listening;
    btnUp = new Button("Up");
    add(btnUp);
    btnUp.addActionListener(this);
    
    //Create Right square, add it to the container, set up listening;
    btnRight = new Button("Right");
    add(btnRight);
    btnRight.addActionListener(this);
    
    //Create Down square, add it to the container, set up listening;
    btnDown = new Button("Down");
    add(btnDown);
    btnDown.addActionListener(this);
    
    //Create background color button, add it to the container, set up listening;
    btnBackColor = new Button("Background Color");
    add(btnBackColor);
    btnBackColor.addActionListener(this);
    
    //Create refresh button, add it to the container, set up listening;
    btnRefresh = new Button("Refresh");
    add(btnRefresh);
    btnRefresh.addActionListener(this);
    
    }//end init

        
    public void actionPerformed(ActionEvent event) {
      //set instance variable figure according to button clicked.
    if (event.getSource() == btnSquare)
        figure = 0; 
    else if (event.getSource() == btnCircle)
        figure = 1;
    else if (event.getSource() == btnRndSquare)
        figure = 2;
    else if (event.getSource() == btnLeft){
        if(x > 0){
            x = x-25; }//end if
        else if(x == 0){
        figure = 99; }//end else if
        }//end else if
    else if (event.getSource() == btnUp){
        if(y > -50){
            y = y-25;}//end if
        else if(y == -50){
            y = 225; }//end else if
        }//end else if
   else if (event.getSource() == btnRight){
        if(x < 300){
            x = x+25;}//end if
        else if(x == 300){
           figure = 99;
            }//end else if
        }//end else if
   else if (event.getSource() == btnDown){
        if(y < 250){
            y = y+25;}//end if
        else if(y == 250){
            y = -25; }//end else if
        }//end else if
   else if (event.getSource() == btnBackColor){
        if(flag == 0) {
        setBackground(Color.yellow);
        flag = 1; }//end if
        else if (flag == 1) { 
            setBackground(Color.green);
            flag = 2; }//end else if
            else if (flag ==2) {
                setBackground(Color.lightGray);
                flag = 3; } //end else if
                else if (flag ==3) {
                setBackground(Color.orange);
                flag = 0; } //end else if
        }//end main else if
    else if (event.getSource() == btnMyApplet)
        figure = 100;
    else if (event.getSource() == btnRefresh){
        x = 150;
        y = 100;
        w = 50;
        h = 50;
        figure = 100;  
    }//end refresh else if
    //call paint()
    repaint();
    }//end actionPerformed
    
    public void paint(Graphics g) {
        //draw figure depending on variable.
        if (figure == 100){
            g.setColor(Color.red);
            g.drawString("My First Applet", x, y);}
        else if (figure == 0)
            g.drawRect(x, y, w, h);
        else if (figure == 1)
            g.drawOval(x, y, w, h);
        else if (figure == 2)
            g.drawRoundRect(x, y, w, h, 15, 15);
        else if (figure == 99){
            g.setColor(Color.red);
            g.drawString("OUCH!!", x, y);
             }//end figure 99
       }//end paint
        
  }//class buttons