FileDocCategorySizeDatePackage
event.javaAPI DocExample5174Thu Sep 07 10:51:46 BST 2000None

event.java

/*  A program to use i/o (for the first time) via a text field.  The program necessarily
    makes use of events.
    The program adds a method 'actionPerformed' which is called each time new text
    is entered in the text field (it actually responds to the 'Enter' key).
    The 'listener' method 'actionPerformed' is set up (initialised) by the method 'addActionListener'.
    Without this call to addActionListener, the 'event' would not happen and there would be no way 
    of getting text from the text field...
*/

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

public class event extends Applet implements ActionListener
{  
   private TextField input;
   private int choice;
   
   // Routine 'init' is always called first (if present).  Put
   // any initialisation code here...
   public void init()
   {
      Label menu1, menu2, menu3;
      // Create a menu using 'Labels'
      menu1 = new Label("Choice 1 = draw colour graph");
      menu2 = new Label("Choice 2 = draw colour pie chart");
      menu3 = new Label("Choice 3 = print colour table");
      add(menu1);
      add(menu2);
      add(menu3);
      input = new TextField(10);
      add (input);
      // addActionListener will cause the routine 'actionPerformed' to be invoked
      // when new data is entered in the textfield...
      input.addActionListener(this);
   }
      
   public void actionPerformed(ActionEvent event) {
      // The line below converts the 'string' in the textfield to
      // an integer...  
      choice = Integer.parseInt (input.getText());
      //  Now the choice has been entered, repaint the applet...
      // This will cause the routine 'paint' to be called.  You can insert all your code into
      // paint confident that it will  be run as a result of the repaint command.
      repaint();
   }

	public void paint(Graphics g)
	{
	   // Here you can do something with the variable 'choice' confident that it has
	   // been set to some meaningful value in the routine 'actionPerformed'...
	   // Also check for errors in the input.  E.g. if the choice lies outside the
	   // acceptable range then tell the user using some appropriate message.
	   
	   // Parameters  for drawing pie chart
  	   double startAngle = 0.0;
      double arcAngle;
      int x = 10; // top left hand screen position (for the pie chart)
      int y = 100;
      int width = 200;  // The total size of the pie chart
      int height = 200;  
      arcAngle = 360.0 / 13.0;   //(360 degrees divided equally by the 13 available colours)
      
      // Parameters for drawing rectangle
      int rectangle_height = 15;
      
      // Parameters for drawing table
      String colourString = " ";
      int string_height = 15;
      
      for (int colour = 1; colour <= 13; colour++) 
      {
        // Select the colour
        switch (colour) {
          case 1:
            g.setColor(Color.black);
            colourString = "Black = 1";
            break;
          case 2:
            g.setColor(Color.blue);
            colourString = "Blue = 2";
            break;
          case 3:
            g.setColor(Color.cyan);
            colourString = "cyan = 3";
            break;
          case 4:
            g.setColor(Color.darkGray);
            colourString = "dark grey = 4";
            break;
          case 5:  
            g.setColor(Color.gray);
            colourString = "grey = 5";
            break;
          case 6:
            g.setColor(Color.green);
            colourString = "green = 6";
            break;
          case 7:
            g.setColor(Color.lightGray);
            colourString = "lightgrey = 7";
            break;
          case 8:
            g.setColor(Color.magenta);
            colourString = "magenta = 8";
            break;
          case 9:
            g.setColor(Color.orange);
            colourString = "orange = 9";
            break;
          case 10:
            g.setColor(Color.pink);
            colourString = "pink = 10";
            break;
          case 11:
            g.setColor(Color.red);
            colourString = "red = 11";
            break;
          case 12:
            g.setColor(Color.white);
            colourString = "White = 12";
            break;
          case 13:
            g.setColor(Color.yellow);
            colourString = "Yellow = 13";
            break;
        }// end of switch (colour)
                      
	     switch(choice) {
	       case 1:
	         // Case 1 - draw a colour graph
	         g.fillArc( x, y, width, height, (int)startAngle, (int)arcAngle );
	         startAngle = startAngle + arcAngle;
	         break;
	       case 2:
	         g.fillRect( x, y, width, rectangle_height);
	         y = y + rectangle_height;
	         break;
	       case 3:
	         g.drawString(colourString, x, y);
	         y = y + string_height;
	         break;
	       default:
	         break;
	     }// End of 'switch'
	   }  // end of 'for' loop
	   //g.drawString("Choice 3 = Leave the program",20,60);
	}
}