Source code for FractalPathDisplay.java

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

/*
 * Copyright (C) 2012 W. Patrick Hooper <wphooper@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package graphics;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import org.freehep.util.export.ExportDialog;

/**
 * This program creates a window which displays the Koch Snowflake.
 *
 * Note that with small modifications, the same program could be used to display
 * any path in a window.
 *
 * @author Pat Hooper
 */
public class FractalPathDisplay extends JFrame {

    FractalPathPanel panel;
    JLabel status_bar;

    public FractalPathDisplay() {
        panel = new FractalPathPanel();
        this.setLayout(new BorderLayout());
        add(panel, BorderLayout.CENTER); // We'll display only this. 
        
        // setup the status bar
        status_bar=new JLabel();
        add(status_bar, BorderLayout.SOUTH);
        panel.setStatusBar(status_bar);
        
        setJMenuBar(buildMenuBar());
    }

    public FractalPathPanel getPanel() {
        return panel;
    }
    
    /**
     * 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;

        try {
            // The following throws an error if the class in quotes does not exist.
            Class.forName("org.freehep.util.export.ExportDialog", false, this.getClass().getClassLoader());
            // If an error was not thrown, then we add the menu item.
            item = new JMenuItem("Export...");
            item.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    ExportDialog export = new ExportDialog();
                    export.showExportDialog(panel, "Export view as ...", panel, "export");
                }
            });
            menu.add(item);
        } catch (ClassNotFoundException e) {
            System.err.println("Failed to find the class org.freehep.util.export.ExportDialog.");
            System.err.println("Check your classpath.");
            System.err.println("You will not be able to export images.");
        }

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

            @Override
            public void actionPerformed(ActionEvent e) {
                FractalPathDisplay.this.dispose();
            }
        });
        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(FractalPathPanel.InteractionMode.ZOOM_IN);
            }
        });
        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.setInteractionMode(FractalPathPanel.InteractionMode.ZOOM_OUT);
            }
        });
        menu.add(item);
        
        item = new JMenuItem("Zoom Fit");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
            ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.fit();
            }
        });
        menu.add(item);        
        
        // PATH MENU
        
        menu = new JMenu("Path");
        menu.setMnemonic('P');
        menuBar.add(menu);        
        
        item = new JMenuItem("Move Point");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,
            ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.setInteractionMode(FractalPathPanel.InteractionMode.MOVE_POINT);
            }
        });
        menu.add(item);
        
        item = new JMenuItem("Delete Point");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
            ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.setInteractionMode(FractalPathPanel.InteractionMode.DELETE_POINT);
            }
        });
        menu.add(item);
        
        item = new JMenuItem("Add Point");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
            ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.setInteractionMode(FractalPathPanel.InteractionMode.ADD_POINT);
            }
        });
        menu.add(item);
        
        menu.addSeparator();
        
        item = new JMenuItem("Increase Path Accuracy");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
            ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.increaseDepth();
            }
        });
        menu.add(item);
        
        item = new JMenuItem("Decrease Path Accuracy");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
            ActionEvent.ALT_MASK));
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.decreaseDepth();
            }
        });
        menu.add(item);
        
        return menuBar;
    }

    /** Create a FractalPathDisplay window and display it. */
    public static void main(String[] args) {
        FractalPathDisplay display = new FractalPathDisplay();

        // Configure the window:
        display.setTitle("Fractal Path Manipulator"); // Set the window's title.
        display.setSize(640, 480); // Set the dimensions of the window
        display.setDefaultCloseOperation(EXIT_ON_CLOSE);

        display.setVisible(true);
    }
}
HOOPER >>>>> JAVA TUTORIAL
Last modified on August 25, 2021.
[check html] [check css]