// Standard imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.net.URL; import java.net.MalformedURLException; import javax.media.j3d.GraphicsConfigTemplate3D; // Application Specific imports // none /** * A basic frame that does all the setup needed for these demo programs. *
* * The window contains a textfield at the top and a Go button to tell it to * load the URL described. In the bottom of the frame is a text label for * information messages. * * @author Justin Couch, Alan Hudson * @version $Revision: 1.1.1.1 $ */ public abstract class DemoFrame extends JFrame implements ActionListener { /** The graphics config template that is best to use */ protected GraphicsConfiguration gfxConfig; /** The textfield to read the values from */ protected JTextField urlTextField; /** The open button on the URl panel */ private JButton openButton; /** The open button on the URl panel */ private JButton reloadButton; /** The label for status messages */ protected JLabel statusLabel; /** The label for FPS messages */ protected JLabel FPSLabel; public DemoFrame(String title) { super(title); GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D(); template.setDoubleBuffer(template.REQUIRED); GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice dev = env.getDefaultScreenDevice(); gfxConfig = dev.getBestConfiguration(template); urlTextField = new JTextField(); urlTextField.setText(System.getProperty("user.dir")); urlTextField.addActionListener(this); urlTextField.requestFocus(); openButton = new JButton(" Open "); openButton.addActionListener(this); reloadButton = new JButton(" Reload "); reloadButton.addActionListener(this); JLabel l1 = new JLabel(" Location: "); JPanel p1 = new JPanel(new BorderLayout()); JPanel p2 = new JPanel(new BorderLayout()); JPanel p4 = new JPanel(new BorderLayout()); p1.add(l1, BorderLayout.WEST); p1.add(urlTextField, BorderLayout.CENTER); p4.add(openButton, BorderLayout.WEST); p4.add(reloadButton, BorderLayout.CENTER); p2.add(p4, BorderLayout.WEST); p2.add(p1, BorderLayout.CENTER); Container content_pane = getContentPane(); content_pane.add(p2, BorderLayout.NORTH); statusLabel = new JLabel("Enter a world to load"); FPSLabel = new JLabel("0"); JPanel p3 = new JPanel(new BorderLayout()); p3.add(statusLabel, BorderLayout.WEST); p3.add(FPSLabel, BorderLayout.EAST); content_pane.add(p3, BorderLayout.SOUTH); setSize(1024, 768); setLocation(40, 40); setDefaultCloseOperation(EXIT_ON_CLOSE); } /** * Go to the named URL location. No checking is done other than to make * sure it is a valid URL. * * @param url The URL to open */ public abstract void gotoLocation(URL url); /** * Load the named file. The file is checked to make sure that it exists * before calling this method. * * @param file The file to load */ public abstract void gotoLocation(File file); protected abstract void setWarning(String msg); protected abstract void setError(String msg); /** * An action has been performed. This is the Go button being pressed. * Grab the URL and check with the file to see if it exists first as * a local file, and then try to make a URL of it. Finally, if this all * works, call the abstract gotoLocation method. * * @param evt The event that caused this method to be called. */ public void actionPerformed(ActionEvent evt) { Object src = evt.getSource(); File fil=null; if (src == openButton) { FileDialog fd = new FileDialog(this, "Load File"); fd.setMode(FileDialog.LOAD); fd.show(); fil = new File(fd.getDirectory() + fd.getFile()); if (fil.exists()) gotoLocation(fil); } else if((src == urlTextField) || (src == reloadButton)) { String location = urlTextField.getText(); fil = new File(location); // try a file first if(fil.exists()) { if(fil.isDirectory()) setError("File is a directory"); else { gotoLocation(fil); statusLabel.setText("World Loaded Successfully"); } } else { // Try a URL try { URL url = new URL(location); gotoLocation(url); statusLabel.setText("World Loaded Successfully"); } catch(MalformedURLException mue) { setError("Invalid URL: " + location); } } } } }