Source code for DemoFrame.java

What follows is the highlighted source code with comments. You can also download this file directly: DemoFrame.java.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coordinatechanges;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 *
 * @author pat
 */
public class DemoFrame extends JFrame {

    DemoPanel panel;
    JLabel status_bar;

    public DemoFrame() {
        setBackground(Color.BLACK);

        panel = new DemoPanel();
        this.setLayout(new BorderLayout());
        add(panel, BorderLayout.CENTER); // We'll display only this. 

        // setup the status bar
        status_bar = new JLabel("Displaying an unfolding");
        add(status_bar, BorderLayout.SOUTH);
        panel.setStatusBar(status_bar);
        setJMenuBar(buildMenuBar());
    }

    /**
     * This function creates and returns the menu bar.
     */
    private JMenuBar buildMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        // FILE MENU

        JMenu menu = new JMenu("File");
        menu.setMnemonic('F');
        menuBar.add(menu);

        JMenuItem item;

        item = new JMenuItem("Close");
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(item);

        // VIEW MENU

        menu = new JMenu("View");
        menu.setMnemonic('v');
        menuBar.add(menu);

        item = new JMenuItem("Zoom In");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
                ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.setInteractionMode(DemoPanel.InteractionMode.ZOOM_BOX);
            }
        });
        menu.add(item);

        item = new JMenuItem("Zoom Out");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
                ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.zoomOut();
            }
        });
        menu.add(item);

        item = new JMenuItem("Zoom Square");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.zoomSquare();
            }
        });
        menu.add(item);

        // Action MENU

        menu = new JMenu("Action");
        menu.setMnemonic('A');
        menuBar.add(menu);

        item = new JMenuItem("Plot Point");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
                ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.setInteractionMode(DemoPanel.InteractionMode.PLOT_A_POINT);
            }
        });
        menu.add(item);

        item = new JMenuItem("Plot Square");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
                ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.setInteractionMode(DemoPanel.InteractionMode.PLOT_A_UNIT_SQUARE);
            }
        });
        menu.add(item);

        // ADD MORE MENU ITEMS!
        
        return menuBar;
    }

    public static void main(String[] args) {
        DemoFrame f = new DemoFrame();

        f.setTitle("Demo"); // Set the window's title.
        f.setSize(640, 480); // Set the dimensions of the window
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setVisible(true);
    }
}
HOOPER >>>>> JAVA TUTORIAL
Last modified on August 17, 2018.
[check html] [check css]