Java Lecture 5

[Previous Lecture] [Lecture Index] [Next Lecture]

Java Graphical User Interfaces

Java's GUI packages: Two types of programs use java.awt: Both types use event driven programming model.

HelloApplet.java

import java.applet.Applet;
import java.awt.Graphics;

public class HelloApplet extends Applet {

  public void paint(Graphics g) {
    g.drawString("Hello, World!", 50, 25);
  }

}


HelloApplet.html


<html>
  <head>
    <title>HelloApplet Applet tester</title>
  </head>
  <body>
    A test applet:
    <applet code=HelloApplet width=200 height=200>
      If you see this, you do not have a
      browser that knows about java...
    </applet>
  </body>
</html>


Introduction to Applets


LineDraw1.java

/*
 * <applet code=LineDraw1 width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class LineDraw1
        extends java.applet.Applet
        implements MouseListener
{
  private int nPoints = 0;
  private int x1, y1;
  private int x2, y2;

  public void init() {
    addMouseListener(this);
  }

  public void paint(Graphics g) {
    if (nPoints == 1)
        g.fillRect(x1 - 1, y1 - 1, 3, 3);
    else if (nPoints == 2)
        g.drawLine(x1, y1, x2, y2);
  }

  public void mouseClicked(MouseEvent me) {
    if (nPoints != 1) {
        nPoints = 1;
        x1 = me.getX();
        y1 = me.getY();
    } else {
        nPoints++;
        x2 = me.getX();
        y2 = me.getY();
    }
    repaint();
  }
  public void mouseEntered(MouseEvent me) { }
  public void mouseExited(MouseEvent me) { }
  public void mousePressed(MouseEvent me) { }
  public void mouseReleased(MouseEvent me) { }
}

LineDraw2.java

/*
 * <applet code=LineDraw2 width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class LineDraw2
        extends java.applet.Applet
{
  private int nPoints = 0;
  private int x1, y1;
  private int x2, y2;

  public void paint(Graphics g) {
    if (nPoints == 1)
        g.fillRect(x1 - 1, y1 - 1, 3, 3);
    else if (nPoints == 2)
        g.drawLine(x1, y1, x2, y2);
  }

  public void mouseClicked(MouseEvent me) {
    if (nPoints != 1) {
        nPoints = 1;
        x1 = me.getX();
        y1 = me.getY();
    } else {
        nPoints++;
        x2 = me.getX();
        y2 = me.getY();
    }
    repaint();
  }
}

class HelperClass extends MouseAdapter
{
  HelperClass(LineDraw2 parent) {
      this.parent = parent;
      parent.addMouseListener(this);
  }

  public void mouseClicked(MouseEvent me) {
    parent.mouseClicked(me);
  }

  private LineDraw2 parent;
}

LineDraw3.java

/*
 * <applet code=LineDraw3 width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class LineDraw3
        extends java.applet.Applet
{
  private int nPoints = 0;
  private int x1, y1;
  private int x2, y2;

  public void init() {
    addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent me) {
            if (nPoints != 1) {
                nPoints = 1;
                x1 = me.getX();
                y1 = me.getY();
            } else {
                nPoints++;
                x2 = me.getX();
                y2 = me.getY();
            }
            repaint();
          }
        }
      );
  }

  public void paint(Graphics g) {
    if (nPoints == 1)
        g.fillRect(x1 - 1, y1 - 1, 3, 3);
    else if (nPoints == 2)
        g.drawLine(x1, y1, x2, y2);
  }
}

Intro to AWT Events

General Event Design Pattern Commonly Used Listeners/Adapters:

AWT Elements


ButtonTest.java

/*
 * <applet code=ButtonTest width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class ButtonTest
        extends java.applet.Applet
        implements ActionListener
{
  Button aButton;
  Button bButton;

  public void init() {
    aButton = new Button("First button");
    bButton = new Button("B");

    add(aButton);
    add(bButton);

    aButton.addActionListener(this);
    bButton.addActionListener(this);
  }

  public void actionPerformed(ActionEvent ae) {
      if (ae.getSource() == aButton)
          System.out.println("First pressed");
      else if (ae.getSource() == bButton)
          System.out.println("B pressed");
  }
}

[Previous Lecture] [Lecture Index] [Next Lecture]