Title: AGore.java
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Submitted from a Former Student


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

public class ballet extends Applet implements MouseMotionListener, ActionListener{

       private Button b1, b2;
       int buttonWidth;
    int buttonHeight;
    int MAX_X = 70;// Maximum button width
    int MAX_Y = 35;// Maximum button height
    int X_LOC = 90;
    int Y_LOC = 170;
    Point center;
    int x,y,z;
    char flag = 'F';
    Image offscreenImg;
   Graphics offscreen;
    String message1,message2,message3,message4,message5,message6,message7,message8;

    public void init() {
       System.out.println ("J L McLaughlin  29 November 2000");

       message1 = new String("My Dear Americans,");
           message2 = new String("Please allow me the priviledge to ask your opinion"+
                                 " of the following matter.");
           message3 = new String("I am fighting for every person's basic right" +
                                 " (for me to be president) but I will not do so ");
           message4 = new String("at the expense of our democracy. " +
                                 "Please take a moment and vote below. ");
           message5 = new String("Regards,");
           message6 = new String("Al Gore");
           message7 = new String("Given the situation in Florida, Should I continue to"+
                                 " battle G.W. Bush (A.K.A. \"Dirty Rascal\")"     );
           message8 = new String("or should I concede for the sake of the nation?");

       // Set up offscreen image for double buffering
       offscreenImg = createImage(getBounds().width,getBounds().height);
       offscreen =  offscreenImg.getGraphics();
       offscreen.setFont(new Font("SansSerif", Font.PLAIN, 12));

        b1 = new Button("Concede");
        add(b1);
        b2 = new Button("Fight ON!");
        add(b2);

        // Set button width and calculate the center point
        buttonWidth = MAX_X;
        buttonHeight = MAX_Y;
        center = new Point();
        center.x = (int)(buttonWidth + (.5 * buttonWidth) + X_LOC);
        center.y = (int)(buttonHeight + (.5 * buttonHeight) + Y_LOC);

        addMouseMotionListener(this);
        b2.addActionListener(this);



    }

    public void paint(Graphics g) {

           // Set up background
           // Drawn to offscreen image for double buffering
           offscreen.setColor(Color.black);
               offscreen.fillRect(0,0,getBounds().width, getBounds().height);
               offscreen.setColor(Color.white);
               offscreen.fillRect(2,2,getBounds().width -4, getBounds().height -4);
               offscreen.setColor(Color.blue);

               if ('F' == flag){
                   // Display the text of the applet
                   offscreen.drawString(message1,5,40);
                   offscreen.drawString(message2,25,55);
                   offscreen.drawString(message3,5,70);
                   offscreen.drawString(message4,5,85);
                   offscreen.drawString(message5,5,110);
                   offscreen.drawString(message6,25,125);
                   offscreen.drawString(message7,5,160);
                   offscreen.drawString(message8,5,175);

                   // Draw the resized button
                   b1.setBounds((int)(center.x - (buttonWidth)*.5) ,
                           (int)(center.y - (buttonHeight)*.5) ,
                           buttonWidth, buttonHeight);

               b2.setBounds(100 + (int)(center.x - (.5 * MAX_X)),
                           (int)(center.y -(.5 * (MAX_Y))),
                           MAX_X, MAX_Y);
                   }
               else
                   {
                   // Display "Thanks for your support" screen
                   offscreen.setFont(new Font("SansSerif", Font.ITALIC, 14));
                   offscreen.drawString("Thanks for your support!", 150,100);
                   b1.setBounds(0,0,0,0);
                   b2.setBounds(0,0,0,0);
                   }

               g.drawImage(offscreenImg, 0,0, this); // Draw buffered image
      }


   public void update(Graphics g){
       paint(g);
       }



   public void mouseMoved(MouseEvent evt){
       // Set the button size and then repaint the display
       setButtonSize(evt.getPoint());
       repaint();
       }

   public void mouseDragged(MouseEvent evt){
       // Set the button size and then repaint the display
       setButtonSize(evt.getPoint());
       repaint();
       }

   public void actionPerformed(ActionEvent evt){

       // Set flag for paint and paint new message if "Fight On!" (b2) button is pressed
       if (evt.getSource() == b2)
           flag = 'T';
           repaint();
           }


   void setButtonSize(Point location){
       double xDis,yDis,distance;

       // Determine the distance from the mouse pointer to the center of the button
       xDis = location.x - center.x;
       yDis = location.y - center.y;
       distance = Math.sqrt((xDis*xDis) + (yDis * yDis));


       // Set the dimension of the button based on the distance
       if ((100.0 >= distance) & (90.0 < distance)){
           buttonWidth = (int)(MAX_X * .9);
           buttonHeight = (int)(MAX_Y * .9);
           }
       else{
           if ((90.0 >= distance) & (80.0 < distance)){
               buttonWidth = (int)(MAX_X * .8);
               buttonHeight = (int)(MAX_Y * .8);
               }
           else{
               if ((80.0 >= distance) & (70.0 < distance)){
                   buttonWidth = (int)(MAX_X * .7);
                   buttonHeight = (int)(MAX_Y * .7);
                   }
               else{
                   if ((70.0 >= distance) & (60.0 < distance)){
                       buttonWidth = (int)(MAX_X * .6);
                       buttonHeight = (int)(MAX_Y * .6);
                       }
                   else{
                       if ((60.0 >= distance) & (50.0 < distance)){
                           buttonWidth = (int)(MAX_X * .5);
                           buttonHeight = (int)(MAX_Y * .5);
                           }
                       else{
                           if ((50.0 >= distance) & (40.0 < distance)){
                               buttonWidth = (int)(MAX_X * .4);
                               buttonHeight = (int)(MAX_Y * .4);
                               }
                           else{
                               if ((40.0 >= distance) & (30.0 < distance)){
                                   buttonWidth = (int)(MAX_X * .3);
                                   buttonHeight = (int)(MAX_Y * .3);
                                   }
                               else{
                                   if ((30.0 >= distance) & (20.0 < distance)){
                                       buttonWidth = (int)(MAX_X * .2);
                                       buttonHeight = (int)(MAX_Y * .2);
                                       }
                                   else{
                                       if ((20.0 >= distance) ){
                                           buttonWidth = (int)(MAX_X * 0);
                                           buttonHeight = (int)(MAX_Y * 0);
                                           }
                                       else{
                                           buttonWidth = MAX_X ;
                                           buttonHeight = MAX_Y ;
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }

       }




}