CIS 121 Wednesday March 1, 2000


Recall the examples we were looking at on Monday.

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

public class ButtonController extends Frame implements ActionListener,WindowListener {
   Button myButton1;
   Button myButton2;
   TextField myTextField;

   public ButtonController(String s) {
      super(s);

      setLayout(null);

      myButton1 = new Button("Print Message");
      myButton2 = new Button("Erase Message");
      myTextField = new TextField();

      myButton1.setLocation(140,150);
      myButton1.setSize(100,20);

      myButton2.setLocation(260,150);
      myButton2.setSize(100,20);

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myButton1);
      add(myButton2);
      add(myTextField);

      myButton1.addActionListener(this);
      myButton2.addActionListener(this);

      myTextField.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getSource() == myButton2) {
         myTextField.setText("");
      } 
   }

}
ButtonController.java

MyGui.java
public class MyGui {

   public static void main(String[] args) {
      ButtonController myController = new ButtonController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}
MyGui.java

A program to demonstrate capturing a list selection and based on that selection writing text into a textfield

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

public class ListController extends Frame implements ActionListener,ItemListener,WindowListener {
   List myList;
   TextField myTextField;

   public ListController(String s) {
      super(s);

      setLayout(null);

      myList = new List(1,false);
      myTextField = new TextField();

      myList.add("Print Message");
      myList.add("Erase Message");

      myList.setLocation(200,150);
      myList.setSize(100,40);

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myList);
      add(myTextField);

      myList.addActionListener(this);
      myList.addItemListener(this);
      myTextField.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void itemStateChanged(ItemEvent e) {
      int position = ((Integer)e.getItem()).intValue();

      if (position == 0) {
         myTextField.setText("Example");
      }
      if (position == 1) {
         myTextField.setText("");
      }
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getActionCommand().equals("")) {
         myTextField.setText("");
      }   
   }

}
ListController.java

MyGui1.java
public class MyGui1 {

   public static void main(String[] args) {
      ListController myController = new ListController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}
MyGui1.java

A program to demonstrate capturing a radio button selection and based on that selection writing text into a textfield

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

public class CheckboxController extends Frame implements ItemListener,WindowListener {
   Checkbox myCheckbox1;
   Checkbox myCheckbox2;
   TextField myTextField;

   public CheckboxController(String s) {
      super(s);

      setLayout(null);

      CheckboxGroup choices = new CheckboxGroup();

      myCheckbox1 = new Checkbox("Print Message",choices,true);
      myCheckbox2 = new Checkbox("Erase Message",choices,false);

      myTextField = new TextField();

      myCheckbox1.setLocation(120,150);
      myCheckbox1.setSize(150,40);

      myCheckbox2.setLocation(300,150);
      myCheckbox2.setSize(150,40);

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myCheckbox1);
      add(myCheckbox2);

      add(myTextField);

      myCheckbox1.addItemListener(this);
      myCheckbox2.addItemListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void itemStateChanged(ItemEvent e) {
      if (e.getStateChange() == ItemEvent.SELECTED) {   
         if (e.getItem() == "Print Message") {
            myTextField.setText("Example");
         }
         if (e.getItem() == "Erase Message") {
            myTextField.setText("");
         }
      }
   }

}
CheckboxController.java

MyGui2.java
public class MyGui2 {

   public static void main(String[] args) {
      CheckboxController myController = new CheckboxController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}
MyGui2.java

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

public class MenuController extends Frame implements ActionListener,WindowListener {
   MenuBar myMenuBar;
   Menu options;
   TextField myTextField;

   public MenuController(String s) {
      super(s);

      setLayout(null);

      CheckboxGroup choices = new CheckboxGroup();

      myMenuBar = new MenuBar();

      options = new Menu("Options");

      options.add("Print Message");
      options.add("Erase Message");

      myMenuBar.add(options);

      this.setMenuBar(myMenuBar);

      myTextField = new TextField();

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myTextField);

      options.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getActionCommand().equals("Erase Message")) {
         myTextField.setText("");
      }
   }

}
MenuController.java

MyGui3.java
public class MyGui3 {

   public static void main(String[] args) {
      MenuController myController = new MenuController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}
MyGui3.java

A program using panels to accomplish the same thing that was done with the ButtonController

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

public class Viewer extends Frame implements ActionListener,WindowListener {
   Button myButton1;
   Button myButton2;
   TextField myTextField;

   public Viewer(String s) {
      super(s);

      setLayout(null);

      Panel panel1 = new Panel(new FlowLayout());
      Panel panel2 = new Panel(new FlowLayout());

      myButton1 = new Button("Print Message");
      myButton2 = new Button("Erase Message");

      myTextField = new TextField(20);

      panel1.setSize(500,100);
      panel2.setSize(500,100);

      panel1.add(myTextField);

      panel2.add(myButton1);
      panel2.add(myButton2);

      panel1.setLocation(0,20);
      panel2.setLocation(0,120);

      panel2.setBackground(Color.yellow);

      add(panel1);
      add(panel2);

      myButton1.addActionListener(this);
      myButton2.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getSource() == myButton2) {
         myTextField.setText("");
      } 
   }

}
Viewer.java

MyGui4.java
public class MyGui4 {

   public static void main(String[] args) {
      Viewer myController = new Viewer("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}

MyGui4.java


A program which will listen for a mouse click, get the x and y coordinates of where the mouse was clicked, and display the coordinates in a TextField

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

class MouseTester extends Frame implements MouseListener,WindowListener {
   TextField myTextField;

   public MouseTester(String s) {
      super(s);
      setBackground(Color.yellow);

      setLayout(new FlowLayout());

      myTextField = new TextField(20);

      add(myTextField);

      addMouseListener(this);
      addWindowListener(this);
   }

   public void mouseClicked(MouseEvent e) { }
   public void mouseReleased(MouseEvent e) { }
   public void mouseEntered(MouseEvent e) { }
   public void mouseExited(MouseEvent e) { }

   public void mousePressed(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();

      myTextField.setText("[" + String.valueOf(x) + "," + String.valueOf(y) + "]");
   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

}
MouseTester.java

MyGui5.java
public class MyGui5 {

   public static void main(String[] args) {
      MouseTester myController = new MouseTester("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}
MyGui5.java

Another important class is the Applet class.

An Applet is a special type of class that is meant to be run in a browser. An Applet has no main() method. In runs in the context of a browser, and borrows some of the event handling such as windows closing and opening. So we don't have to implement WindowListener.

In order to run an applet, it must be placed in a document which can be interpreted by a browser. The document must be in HTML (HyperText Markup Language) format. We will only use very simple HTML in the code we generate.

Below is a listing of the Applet class.
public class Applet extends Panel {

   public Applet();

   public void destroy();
   public AppletContext getAppletContext();
   public String getAppletInfo();
   public AudioClip getAudioClip(URL url);
   public AudioClip getAudioClip(URL url,String name);
   public URL getCodeBase();
   public URL getDocumentBase();
   public Image getImage(URL url);
   public Image getImage(URL url,String name);
   public Locale getLocale();
   public String getParameter(String name);
   public String[][] getParameterInfo();
   public void init();
   public boolean isActive();
   public void play(URL url);
   public void play(URL url,String name);
   public void resize(Dimension d);
   public void resize(int width,int height);
   public final void setStub(AppletStub stub);
   public void showStatus(String mag);
   public void start();
   public void stop();
}
In order to create an applet, we create a subclass of Applet and override some or all of the following methods.

  1. init() - is called after the constructor of the subclass is run, i.e. when the applet first starts
  2. start() - is called after the browser opens the applet's window
  3. stop() - is called when the browser changes to another HTML page
  4. destroy() - is called when the applet exits
The applet also overrides the paint() method from Component.

Examples:

The first example we have is an applet which will graph functions of the form f(x)=ax2+bx+c. Let us look at how the applet works and then look at the source code.



Source Code:

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

public class QuadraticGrapher extends Applet implements ActionListener {
   TextField acoefficient;
   TextField bcoefficient;
   TextField ccoefficient;
   Button graph;
   Panel graphRegion = new Panel(null);

   public QuadraticGrapher() {
      setLayout(null);

      Panel controlPanel = new Panel(new FlowLayout());

      graphRegion.setSize(500,300);
      graphRegion.setLocation(0,0);

      Label a = new Label("a ");
      Label b = new Label("b ");
      Label c = new Label("c ");

      acoefficient = new TextField(5);
      bcoefficient = new TextField(5);
      ccoefficient = new TextField(5);

      acoefficient.setText("1");
      bcoefficient.setText("1");
      ccoefficient.setText("1");

      graph = new Button("Draw Graph");

      graph.addActionListener(this);

      controlPanel.add(a);
      controlPanel.add(acoefficient);
      controlPanel.add(b);
      controlPanel.add(bcoefficient);
      controlPanel.add(c);
      controlPanel.add(ccoefficient);
      controlPanel.add(graph);

      controlPanel.setSize(500,100);
      controlPanel.setLocation(0,300);

      add(controlPanel);
      add(graphRegion);

   }

   public void actionPerformed(ActionEvent e) {

      if (e.getSource() == graph) {
         Graphics g = graphRegion.getGraphics();
         paint(g);
      }

   }

   public void init() {
      setBackground(Color.yellow);
   }

   public void paint(Graphics g) {
      int i;

      float a = Float.valueOf(acoefficient.getText()).floatValue();
      float b = Float.valueOf(bcoefficient.getText()).floatValue();
      float c = Float.valueOf(ccoefficient.getText()).floatValue();

      g.clearRect(0,0,500,300);

      g.setColor(Color.blue);

      for (i=0;i<499;i++) {
         g.fillRect(i,150,2,2);
      }

      for (i=0;i<299;i++) {
         g.fillRect(250,i,2,2);
      }
         
      g.setColor(Color.red);

      for (i=-500;i<=500;i+=1) {
         float x=i/(100f);
         int y = (int)((a*x*x+b*x+c)*100);
         g.fillRect(i+250,150-y,2,2);
         System.out.println(i + " " + y);
      }
   }

}
The HTML code used to display the applet is the following.

<table border=3>
<tr><td><applet code="QuadraticGrapher" width=500 height=400>
</applet></td></tr>
</table>


The following example is a slight modification of the Quadratic Grapher. It allows us to select through radio buttons what range of points we use to graph the function.



Source Code:

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

public class QuadraticGrapher1 extends Applet implements ActionListener,ItemListener {
   TextField acoefficient;
   TextField bcoefficient;
   TextField ccoefficient;
   Button graph;
   Checkbox range1;
   Checkbox range2;
   Checkbox range3;
   Checkbox range4;
   Checkbox range5;
   int low=-100;
   int high=100;

   Panel graphRegion = new Panel(null);

   public QuadraticGrapher1() {
      setLayout(null);

      Panel controlPanel = new Panel(new FlowLayout());
      Panel controlPanel2 = new Panel(new FlowLayout());

      graphRegion.setSize(500,300);
      graphRegion.setLocation(0,0);

      Label a = new Label("a ");
      Label b = new Label("b ");
      Label c = new Label("c ");

      acoefficient = new TextField(5);
      bcoefficient = new TextField(5);
      ccoefficient = new TextField(5);

      acoefficient.setText("1");
      bcoefficient.setText("1");
      ccoefficient.setText("1");

      graph = new Button("Draw Graph");

      graph.addActionListener(this);

      CheckboxGroup ranges = new CheckboxGroup();

      range1 = new Checkbox("-100 to 100",ranges,true);
      range2 = new Checkbox("-200 to 200",ranges,false);
      range3 = new Checkbox("-300 to 300",ranges,false);
      range4 = new Checkbox("-400 to 400",ranges,false);
      range5 = new Checkbox("-500 to 500",ranges,false);

      controlPanel.add(a);
      controlPanel.add(acoefficient);
      controlPanel.add(b);
      controlPanel.add(bcoefficient);
      controlPanel.add(c);
      controlPanel.add(ccoefficient);
      controlPanel.add(graph);

      controlPanel2.add(range1);
      controlPanel2.add(range2);
      controlPanel2.add(range3);
      controlPanel2.add(range4);
      controlPanel2.add(range5);

      range1.addItemListener(this);
      range2.addItemListener(this);
      range3.addItemListener(this);
      range4.addItemListener(this);
      range5.addItemListener(this);

      controlPanel.setSize(500,50);
      controlPanel.setLocation(0,300);

      controlPanel2.setSize(500,50);
      controlPanel2.setLocation(0,350);

      add(controlPanel);
      add(controlPanel2);
      add(graphRegion);

   }

   public void actionPerformed(ActionEvent e) {

      if (e.getSource() == graph) {
         Graphics g = graphRegion.getGraphics();
         paint(g);
      }

   }

   public void itemStateChanged(ItemEvent e) {
   
      if (e.getStateChange() == ItemEvent.SELECTED) {
         if (e.getItem() == "-100 to 100") {
            low=-100;
            high=100;
         }
         if (e.getItem() == "-200 to 200") {
            low=-200;
            high=200;
         }
         if (e.getItem() == "-300 to 300") {
            low=-300;
            high=300;
         }
         if (e.getItem() == "-400 to 400") {
            low=-400;
            high=400;
         }
         if (e.getItem() == "-500 to 500") {
            low=-500;
            high=500;
         }
      }

   }

   public void init() {
      setBackground(Color.yellow);
   }

   public void paint(Graphics g) {
      int i;

      float a = Float.valueOf(acoefficient.getText()).floatValue();
      float b = Float.valueOf(bcoefficient.getText()).floatValue();
      float c = Float.valueOf(ccoefficient.getText()).floatValue();

      g.clearRect(0,0,500,300);

      g.setColor(Color.blue);

      for (i=0;i<499;i++) {
         g.fillRect(i,150,2,2);
      }

      for (i=0;i<299;i++) {
         g.fillRect(250,i,2,2);
      }
         
      g.setColor(Color.red);

      for (i=low;i<=high;i+=1) {
         float x=i/(100f);
         int y = (int)((a*x*x+b*x+c)*100);
         g.fillRect(i+250,150-y,2,2);
         System.out.println(i + " " + y);
      }
   }

}
For our third example, let us consider a simple applet which will allow us to input red, green, and blue components of a color and then display that color also outputting the RGB code for the color.



For an exercise, write the code which will produce the ColorTester applet.