Source code for LayerDemoFrame.java

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

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

import 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 LayerDemoFrame extends JFrame {

    LayeredPanel panel;
    CircleLayer cl;
    SquareLayer sl;
    JLabel status_bar;

    public LayerDemoFrame() {
        setBackground(Color.WHITE);

        panel = new LayeredPanel();
        panel.setBackground(Color.WHITE);

        // Add layers:
        cl = new CircleLayer(panel);
        panel.addTopLayer(cl);
        sl = new SquareLayer(panel);
        panel.addBottomLayer(sl);

        add(panel); // We'll display only this. 

        // setup the 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);

        // Action MENU

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

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

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

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

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

        menu = new JMenu("Layers");
        menu.setMnemonic('l');
        menuBar.add(menu);

        item = new JMenuItem("Raise Disks To Top");
        item.addActionListener(new ActionListener() {

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

        item = new JMenuItem("Raise Squares To Top");
        item.addActionListener(new ActionListener() {

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

        return menuBar;
    }

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

        f.setTitle("Layer 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]