|
| 1 | +/* |
| 2 | + * Gui.java |
| 3 | + * This is part of the Antiksim (Antikythera Mechanism Simulation) project |
| 4 | + * |
| 5 | + * Copyright 2013 Fotis Xenakis |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +//Class to create and show the gui, handling events |
| 21 | +//TODO: Add menu item to display the user licence |
| 22 | +package antiksim; |
| 23 | + |
| 24 | +import javax.swing.*; |
| 25 | +import java.awt.*; |
| 26 | +import java.awt.event.*; |
| 27 | + |
| 28 | +class Gui |
| 29 | +{ |
| 30 | + public Gui() |
| 31 | + { |
| 32 | + //Initialize frame, set default close operation and set a white background |
| 33 | + final JFrame frame=new JFrame("Antikythera Mechanism Simulation"); |
| 34 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 35 | + |
| 36 | + //Create menubar and set it to the frame |
| 37 | + JMenuBar menubar=new JMenuBar(); |
| 38 | + frame.setJMenuBar(menubar); |
| 39 | + |
| 40 | + //Create menus and add them to the menubar |
| 41 | + JMenu actions_menu=new JMenu("Actions"); |
| 42 | + JMenu help_menu=new JMenu("Help"); |
| 43 | + menubar.add(actions_menu); |
| 44 | + menubar.add(help_menu); |
| 45 | + |
| 46 | + //Create menu items, set tooltips and add them to the menus |
| 47 | + final JMenuItem start_button=new JMenuItem("Start"); |
| 48 | + final JMenuItem pause_button=new JMenuItem("Pause"); |
| 49 | + final JMenuItem reset_button=new JMenuItem("Reset"); |
| 50 | + JMenuItem speed_menu=new JMenu("Change speed"); |
| 51 | + final JMenuItem exit_button=new JMenuItem("Exit"); |
| 52 | + JMenuItem controls_button=new JMenuItem("Controls"); |
| 53 | + JMenuItem description_button=new JMenuItem("Description"); |
| 54 | + JMenuItem about_button=new JMenuItem("About"); |
| 55 | + start_button.setToolTipText("Start animation"); |
| 56 | + pause_button.setToolTipText("Pause animation"); |
| 57 | + reset_button.setToolTipText("Reset animation"); |
| 58 | + speed_menu.setToolTipText("Select animation speed"); |
| 59 | + exit_button.setToolTipText("Exit application"); |
| 60 | + controls_button.setToolTipText("Simulation controls"); |
| 61 | + description_button.setToolTipText("About the Mechanism"); |
| 62 | + about_button.setToolTipText("About this application"); |
| 63 | + actions_menu.add(start_button); |
| 64 | + actions_menu.add(pause_button); |
| 65 | + actions_menu.add(reset_button); |
| 66 | + actions_menu.add(speed_menu); |
| 67 | + actions_menu.addSeparator(); |
| 68 | + actions_menu.add(exit_button); |
| 69 | + help_menu.add(controls_button); |
| 70 | + help_menu.add(description_button); |
| 71 | + help_menu.add(about_button); |
| 72 | + |
| 73 | + //Create radio buttons and buttongroup for speed selection and add them to the menu |
| 74 | + ButtonGroup rbgroup=new ButtonGroup(); |
| 75 | + final JRadioButtonMenuItem rbutton1=new JRadioButtonMenuItem("Slow"); |
| 76 | + final JRadioButtonMenuItem rbutton2=new JRadioButtonMenuItem("Normal"); |
| 77 | + final JRadioButtonMenuItem rbutton3=new JRadioButtonMenuItem("Fast"); |
| 78 | + rbutton2.setSelected(true); |
| 79 | + rbgroup.add(rbutton1); |
| 80 | + rbgroup.add(rbutton2); |
| 81 | + rbgroup.add(rbutton3); |
| 82 | + speed_menu.add(rbutton1); |
| 83 | + speed_menu.add(rbutton2); |
| 84 | + speed_menu.add(rbutton3); |
| 85 | + |
| 86 | + //Create simulation panel and add it to the frame |
| 87 | + final Simulation sim=new Simulation(); |
| 88 | + frame.add(sim,BorderLayout.CENTER); |
| 89 | + |
| 90 | + //Create action listeners for all menu items |
| 91 | + start_button.addActionListener(new ActionListener(){ |
| 92 | + public void actionPerformed(ActionEvent ae){ |
| 93 | + sim.startAnimation(); |
| 94 | + start_button.setEnabled(false); |
| 95 | + pause_button.setEnabled(true); |
| 96 | + }}); |
| 97 | + pause_button.addActionListener(new ActionListener(){ |
| 98 | + public void actionPerformed(ActionEvent ae){ |
| 99 | + sim.pauseAnimation(); |
| 100 | + pause_button.setEnabled(false); |
| 101 | + start_button.setEnabled(true); |
| 102 | + }}); |
| 103 | + reset_button.addActionListener(new ActionListener(){ |
| 104 | + public void actionPerformed(ActionEvent ae){ |
| 105 | + if(pause_button.isEnabled()) |
| 106 | + sim.pauseAnimation(); |
| 107 | + sim.resetAnimation(); |
| 108 | + if(start_button.isEnabled()) |
| 109 | + start_button.doClick(); |
| 110 | + else |
| 111 | + sim.startAnimation(); |
| 112 | + rbutton2.setSelected(true); |
| 113 | + }}); |
| 114 | + exit_button.addActionListener(new ActionListener(){ |
| 115 | + public void actionPerformed(ActionEvent ae){ |
| 116 | + System.exit(0); |
| 117 | + }}); |
| 118 | + controls_button.addActionListener(new ActionListener(){ |
| 119 | + public void actionPerformed(ActionEvent ae){ |
| 120 | + JOptionPane.showMessageDialog(frame,new JLabel("<html><center><b><font size=\"5\" color=\"red\">CONTROLS</font></b></center>"+ |
| 121 | + "<br><b>Left/right mouse click:</b> start/pause animation.<br><b>Middle mouse click:"+ |
| 122 | + "</b> reset animation.<br><b>Actions -> Change speed:</b> change animation's speed.</html>"),"Controls",JOptionPane.PLAIN_MESSAGE); |
| 123 | + }}); |
| 124 | + description_button.addActionListener(new ActionListener(){ |
| 125 | + public void actionPerformed(ActionEvent ae){ |
| 126 | + sim.changeIndicatorColors(); |
| 127 | + JOptionPane.showMessageDialog(frame,new JLabel("<html><div align=center><b><font size=\"5\" color=\"red\">About the Mechanism</font></b><br>"+ |
| 128 | + "<br>The Antikythera Mechanism is an ancient astronomical calculator<br>found in a shipwreck off the greek island of Antikythera.<br>"+ |
| 129 | + "<br><font color=rgb(255,185,15)><b>Golden</b></font> indicator: <b>Sun</b><br><font color=rgb(128,128,128)><b>Gray</b></font> indicator: <b>Moon</b><br>"+ |
| 130 | + "<font color=rgb(0,0,205)><b>Blue</b></font> indicator: <b>Metonic Cycle</b><br><font color=rgb(139,28,98)><b>Purple</b></font> indicator: "+ |
| 131 | + "<b>Callippic Cycle</b><br><font color=rgb(0,100,0)><b>Green</b></font> indicator: <b>Olympiad</b><br><font color=rgb(255,0,0)><b>Red</b></font> "+ |
| 132 | + "indicator: <b>Saros</b><br><font color=rgb(0,0,0)><b>Black</b></font> indicator: <b>Exeligmos</b><br><br><b>More information:<br>Official website:</b> "+ |
| 133 | + "<font color=\"blue\">www.antikythera-mechanism.gr</font><br><b>Wikipedia article:</b> "+ |
| 134 | + "<font color=\"blue\">en.wikipedia.org/wiki/Antikythera_mechanism</font></div></html>"),"Description",JOptionPane.PLAIN_MESSAGE); |
| 135 | + sim.changeIndicatorColors(); |
| 136 | + }}); |
| 137 | + about_button.addActionListener(new ActionListener(){ |
| 138 | + public void actionPerformed(ActionEvent ae){ |
| 139 | + JOptionPane.showMessageDialog(frame,new JLabel("<html><div align=center><b><font size=\"6\" color=\"blue\">Antikythera Mechanism Simulation</font></b><br>"+ |
| 140 | + "<font size=\"5\">A simple, minimalistic simulation of the<br>Antikythera Mechanism's main functions.</font><br><br><br>"+ |
| 141 | + "<font size=\"3\">Copyright 2013 Fotis Xenakis<br>This is free software published under<br>the Apache Licence, Version 2.0<br>"+ |
| 142 | + "A copy of the licence is distributed with the application.</font></div></html>"),"About",JOptionPane.PLAIN_MESSAGE); |
| 143 | + }}); |
| 144 | + rbutton1.addActionListener(new ActionListener(){ |
| 145 | + public void actionPerformed(ActionEvent ae){ |
| 146 | + sim.setAnimationSpeed(0); |
| 147 | + }}); |
| 148 | + rbutton2.addActionListener(new ActionListener(){ |
| 149 | + public void actionPerformed(ActionEvent ae){ |
| 150 | + sim.setAnimationSpeed(1); |
| 151 | + }}); |
| 152 | + rbutton3.addActionListener(new ActionListener(){ |
| 153 | + public void actionPerformed(ActionEvent ae){ |
| 154 | + sim.setAnimationSpeed(2); |
| 155 | + }}); |
| 156 | + |
| 157 | + //Add mouselistener to control the simulation with the mouse |
| 158 | + sim.addMouseListener(new MouseAdapter(){ |
| 159 | + public void mouseClicked(MouseEvent me) |
| 160 | + { |
| 161 | + if(me.getButton()==MouseEvent.BUTTON2) |
| 162 | + { |
| 163 | + reset_button.doClick(); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + if(start_button.isEnabled()) |
| 168 | + start_button.doClick(); |
| 169 | + else |
| 170 | + pause_button.doClick(); |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + //Use pack() to fit widgets exactly |
| 176 | + frame.pack(); |
| 177 | + |
| 178 | + //Set frame's on-screen location and show it |
| 179 | + frame.setLocationRelativeTo(null); //show window in the center |
| 180 | + //frame.setLocationByPlatform(true); //let the window manager place the window as it does with all new ones |
| 181 | + frame.setVisible(true); |
| 182 | + |
| 183 | + //Start the animation |
| 184 | + start_button.doClick(); |
| 185 | + } |
| 186 | +} |
0 commit comments