Koordinatengrafik mit Java
HomeAufgabenDruckenJava-Online

Menu-Events

Menüs sind typische GUI-Elemente, die Events auslösen. Diese werden mit Callbackmethoden abgefangen. Unter einem Popup-Menü versteht man ein Fenster mit einer Liste von Menüoptionen. Ein Popup-Menü ist eine Instanz der Klasse JMenu und enthält Menüoptionen, die Instanzen der Klasse JMenuItem sind. (Da eine Menüoption wieder ein Menü sein kann, wird JMenu aus JMenuItem abgeleitet.)

 

// MenuEvent1.java

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import ch.aplu.util.*;

public class MenuEvent1 implements ActionListener
{
  private interface State
  {
    static int IDLE = 0;
    static int PLAYING = 1;
    static int EXITING = 2;
  }

  private GPanel p = new GPanel(setMenu());
  private JMenuItem playItem, stopItem,
                    exitItem;
  private int state = State.IDLE;

  public MenuEvent1()
  {
    while (state != State.EXITING)
    {
      while (state == State.PLAYING)
      {
        zeichne();
      }
    }
    System.exit(0);
  }

  private JMenuBar setMenu()
  {
    JMenu fileMenu = new JMenu("File");
    playItem = new JMenuItem("Play");
    fileMenu.add(playItem);
    playItem.addActionListener(this);
    stopItem = new JMenuItem("Stop");
    fileMenu.add(stopItem);
    stopItem.addActionListener(this);
    exitItem = new JMenuItem("Exit");
    fileMenu.add(exitItem);
    exitItem.addActionListener(this);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);

    return menuBar;
  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == playItem)
      state = State.PLAYING;
    if (e.getSource() == stopItem)
      state = State.IDLE;
    if (e.getSource() == exitItem)
      state = State.EXITING;
  }
  
  private void zeichne()
  {
    p.pos(Math.random()Math.random());
    p.color(Color.red);
    p.fillCircle(0.01);

    Console.delay(200);
  }

  public static void main(String[] args)
  {
    new MenuEvent1();
  }
}
 

Um die verschiedenen Zustände zu unterscheiden, verwenden wir einen Aufzählungstyp. Dazu deklarieren wir eine Klasse State als enum. Bei der Deklaration geben wir in einer geschweiften Klammer alle möglichen Bezeichner an. Die Variable state besitzt den Typ State und wird zu Beginn auf den Wert State.IDLE gesetzt.

// MenuEvent2.java

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import ch.aplu.util.*;

public class MenuEvent2 implements ActionListener
{
  private interface State
  {
    static int IDLE = 0;
    static int PLAYING = 1;
    static int EXITING = 2;
  }

  private GPanel p =
    new GPanel(setMenu()-0.57.5-0.57.5);
  private JMenuItem playItem, stopItem,
                    exitItem, helpItem, aboutItem;
  private int state = State.IDLE;

  public MenuEvent2()
  {
    int xold = -1, yold = -1;
    int x, y;
    drawBoard();
    while (state != State.EXITING)
    {
      Console.delay(1);  // Let's sleep a while in stopped state
      while (state == State.PLAYING)
      {
        x = (int)(8 * Math.random());
        y = (int)(8 * Math.random());
        drawField(xold, yold);  // Erase image
        p.move(x, y);
        p.color(Color.red);
        p.fillCircle(0.3);
        xold = x; yold = y;
        Console.delay(500);
      }
    }
    System.exit(0);
  }

  private JMenuBar setMenu()
  {
      JMenu fileMenu = new JMenu("File");
      playItem = new JMenuItem("Play");
      fileMenu.add(playItem);
      playItem.addActionListener(this);
      stopItem = new JMenuItem("Stop");
      fileMenu.add(stopItem);
      stopItem.addActionListener(this);
      exitItem = new JMenuItem("Exit");
      fileMenu.add(exitItem);
      exitItem.addActionListener(this);

      JMenu helpMenu = new JMenu("Help");
      helpItem = new JMenuItem("Help Topics");
      helpMenu.add(helpItem);
      helpItem.addActionListener(this);
      aboutItem = new JMenuItem("About...");
      helpMenu.add(aboutItem);
      aboutItem.addActionListener(this);

      JMenuBar menuBar = new JMenuBar();
      menuBar.add(fileMenu);
      menuBar.add(helpMenu);

      return menuBar;
  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == playItem)
      state = State.PLAYING;
    if (e.getSource() == stopItem)
      state = State.IDLE;
    if (e.getSource() == exitItem)
      state = State.EXITING;
    if (e.getSource() == helpItem)
      JOptionPane.showMessageDialog(null,
             "Select File | Play to (re)start the game\n"
           + "Select File | Stop to stop the game");
    if (e.getSource() == aboutItem)
      JOptionPane.showMessageDialog(null,
             " Application written by\n © Aegidius Plüss\n"
           + " Berne, Switzerland\n"
           + " http://www.aplu.ch");
  }

  private void drawBoard()
  {
    for (int x = 0; x < 8; x++)
      for (int y = 0; y < 8; y++)
        drawField(x, y);
  }

  private void drawField(int x, int y)
  {
    if ((+ y) % 2 == 0)
    {
      p.color(Color.BLACK);
      p.move(x, y);
      p.fillRectangle(11);
    }
    else
    {
      p.color(Color.WHITE);
      p.move(x, y);
      p.fillRectangle(11);
    }
  }

  public static void main(String[] args)
  {
    new MenuEvent2();
  }
}