Saturday 25 March 2017

Creating a Frame Window in an Applet - Java Tutorials

While it is possible to simply create a window by creating an instance of Frame, you will seldom do so, because you will not be able to do much with it. For example, you will not be able to receive or process events that occur within it or easily output information to it. Most of the time, you will create a subclass of Frame. Doing so lets you override Frame’s methods and event handling.

Creating a new frame window from within an applet is actually quite easy. First, create a subclass of Frame. Next, override any of the standard window methods, such as init( ), start( ), stop( ), and paint( ). Finally, implement the windowClosing( ) method of the WindowListener interface, calling setVisible(false) when the window is closed.

Once you have defined a Frame subclass, you can create an object of that class. This causes a frame window to come into existence, but it will not be initially visible. You make it visible by calling setVisible( ). When created, the window is given a default height and width. You can set the size of the window explicitly by calling the setSize( ) method.

The following applet creates a subclass of Frame called SampleFrame. A window of this subclass is instantiated within the init( ) method of AppletFrame. Notice that SampleFrame calls Frame’s constructor. This causes a standard frame window to be created with the title passed in title. This example overrides the applet window’s start( ) and stop( ) methods so that they show and hide the child window, respectively. This causes the window to be removed automatically when you terminate the applet, when you close the window, or, if using a browser, when you move to another page. It also causes the child window to be shown when the browser returns to the applet.

  // Create a child frame window from within an applet.
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  /*
    <applet code="AppletFrame" width=300 height=50>
    </applet>
  */

  // Create a subclass of Frame.
  class SampleFrame extends Frame {
    SampleFrame(String title) {
      super(title);
      // create an object to handle window events
      MyWindowAdapter adapter = new MyWindowAdapter(this);
        // register it to receive those events
        addWindowListener(adapter);
    }

    public void paint(Graphics g) {
      g.drawString("This is in frame window", 10, 40);
    }
  }

  class MyWindowAdapter extends WindowAdapter {
    SampleFrame sampleFrame;
    public MyWindowAdapter(SampleFrame sampleFrame) {
      this.sampleFrame = sampleFrame;
    }
    public void windowClosing(WindowEvent we) {
      sampleFrame.setVisible(false);
    }
  }

  // Create frame window.
  public class AppletFrame extends Applet {
    Frame f;
    public void init() {
      f = new SampleFrame("A Frame Window");

      f.setSize(250, 250);
      f.setVisible(true);
    }
    public void start() {
      f.setVisible(true);
    }
    public void stop() {
      f.setVisible(false);
    }
    public void paint(Graphics g) {
      g.drawString("This is in applet window", 10, 20);
    }
  }


Handling Events in a Frame Window

Since Frame is a subclass of Component, it inherits all the capabilities defined by Component. This means that you can use and manage a frame window that you create just like you manage your applet’s main window. For example, you can override paint( ) to display output, call repaint( ) when you need to restore the window, and override all event handlers. Whenever an event occurs in a window, the event handlers defined by that window will be called. Each window handles its own events. For example, the following program creates a window that responds to mouse events. The main applet window also responds to mouse events. When you experiment with this program, you will see that mouse events are sent to the window in which the event occurs.

  // Handle mouse events in both child and applet windows.
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  /*
    <applet code="WindowEvents" width=300 height=50>
    </applet>
  */

  // Create a subclass of Frame.
  class SampleFrame extends Frame
    implements MouseListener, MouseMotionListener {

    String msg = "";
    int mouseX=10, mouseY=40;
    int movX=0, movY=0;

    SampleFrame(String title) {
      super(title);
      // register this object to receive its own mouse events
      addMouseListener(this);
      addMouseMotionListener(this);
      // create an object to handle window events
      MyWindowAdapter adapter = new MyWindowAdapter(this);
      // register it to receive those events
      addWindowListener(adapter);
    }

    // Handle mouse clicked.
    public void mouseClicked(MouseEvent me) {
    }

    // Handle mouse entered.
    public void mouseEntered(MouseEvent evtObj) {
      // save coordinates
      mouseX = 10;
      mouseY = 54;
      msg = "Mouse just entered child.";
      repaint();
    }

    // Handle mouse exited.
    public void mouseExited(MouseEvent evtObj) {
      // save coordinates
      mouseX = 10;
      mouseY = 54;
      msg = "Mouse just left child window.";
      repaint();
    }
   
    // Handle mouse pressed.
    public void mousePressed(MouseEvent me) {
      // save coordinates
      mouseX = me.getX();
      mouseY = me.getY();
      msg = "Down";
      repaint();
    }
    // Handle mouse released.
    public void mouseReleased(MouseEvent me) {
      // save coordinates
      mouseX = me.getX();
      mouseY = me.getY();
      msg = "Up";
      repaint();
    }

    // Handle mouse dragged.
    public void mouseDragged(MouseEvent me) {
      // save coordinates
      mouseX = me.getX();
      mouseY = me.getY();
      movX = me.getX();
      movY = me.getY();
      msg = "*";
      repaint();
    }

    // Handle mouse moved.
    public void mouseMoved(MouseEvent me) {
      // save coordinates
      movX = me.getX();
      movY = me.getY();
      repaint(0, 0, 100, 60);
    }

    public void paint(Graphics g) {
      g.drawString(msg, mouseX, mouseY);
      g.drawString("Mouse at " + movX + ", " + movY, 10, 40);
    }
  }

  class MyWindowAdapter extends WindowAdapter {
    SampleFrame sampleFrame;
    public MyWindowAdapter(SampleFrame sampleFrame) {
      this.sampleFrame = sampleFrame;
    }

    public void windowClosing(WindowEvent we) {
      sampleFrame.setVisible(false);
    }
  }

  // Applet window.
  public class WindowEvents extends Applet
    implements MouseListener, MouseMotionListener {

    SampleFrame f;
    String msg = "";
    int mouseX=0, mouseY=10;
    int movX=0, movY=0;

    // Create a frame window.
    public void init() {
      f = new SampleFrame("Handle Mouse Events");
      f.setSize(300, 200);
      f.setVisible(true);

      // register this object to receive its own mouse events
      addMouseListener(this);
      addMouseMotionListener(this);
    }

    // Remove frame window when stopping applet.
    public void stop() {
      f.setVisible(false);
    }

    // Show frame window when starting applet.
    public void start() {
      f.setVisible(true);
    }

    // Handle mouse clicked.
    public void mouseClicked(MouseEvent me) {
    }

    // Handle mouse entered.
    public void mouseEntered(MouseEvent me) {
      // save coordinates
      mouseX = 0;
      mouseY = 24;
      msg = "Mouse just entered applet window.";
      repaint();
    }

    // Handle mouse exited.
    public void mouseExited(MouseEvent me) {
      // save coordinates
      mouseX = 0;
      mouseY = 24;
      msg = "Mouse just left applet window.";
      repaint();
    }

    // Handle button pressed.
    public void mousePressed(MouseEvent me) {
      // save coordinates
      mouseX = me.getX();
      mouseY = me.getY();
      msg = "Down";
      repaint();
    }

    // Handle button released.
    public void mouseReleased(MouseEvent me) {
      // save coordinates
      mouseX = me.getX();
      mouseY = me.getY();
      msg = "Up";
      repaint();
    }

    // Handle mouse dragged.
    public void mouseDragged(MouseEvent me) {
      // save coordinates
      mouseX = me.getX();
      mouseY = me.getY();
      movX = me.getX();
      movY = me.getY();
      msg = "*";
      repaint();
    }

    // Handle mouse moved.
    public void mouseMoved(MouseEvent me) {
      // save coordinates
      movX = me.getX();
      movY = me.getY();
      repaint(0, 0, 100, 20);
    }

    // Display msg in applet window.
    public void paint(Graphics g) {
      g.drawString(msg, mouseX, mouseY);
      g.drawString("Mouse at " + movX + ", " + movY, 0, 10);
    }
  }

No comments:

Post a Comment