diff options
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java')
-rw-r--r-- | PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java | 195 |
1 files changed, 164 insertions, 31 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java index d05016d..5cbc3f5 100644 --- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java @@ -1,9 +1,13 @@ package uk.ac.open.punchingbag; + import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; +import java.awt.DisplayMode; import java.awt.Graphics; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; @@ -13,10 +17,16 @@ import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import javax.imageio.ImageIO; import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; @@ -43,16 +53,21 @@ import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.AudioFormat.Encoding; import javax.sound.sampled.Mixer.Info; +import PicHelper.ImagePanel; + public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener { public static enum Mode { - Menu, Interactive, Game + Debug, Menu }; - Mode currentMode = Mode.Interactive; + Mode currentMode = Mode.Menu; - static JFrame frame; + JFrame frame; + + JPanel bagDebugingPanel = new JPanel(); + JPanel programOptionsPanel = new JPanel(); // Contact public enum Direction { @@ -67,13 +82,16 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, new File(soundDir + "E5.wav"), new File(soundDir + "F5.wav"), new File(soundDir + "G5.wav") }; + String imageDirectory = System.getProperty("user.home"); + static PunchingBag bag = PunchingBag.getBag(); final boolean useDebugScreen = true; + boolean debugImages = true; + public static void main(String[] args) throws InterruptedException { new PunchingBagGUI(); - } PunchingBagGUI() throws InterruptedException { @@ -82,13 +100,18 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, // System.out.println(AudioSystem.getMixerInfo()[0].getDescription()); // Mixer mix = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]); - bag.connectToArduinos(); + try { + // bag.connectToArduinos(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } frame = new JFrame("Punching Bag GUI"); + frame.setUndecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // frame.setSize(600, 600); - frame.setVisible(true); frame.addMouseListener(this); frame.addMouseMotionListener(this); @@ -106,34 +129,132 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(), "Allows control of individual LED's"); + tabbedPane.addTab("GUI Control Panel", null, new GUIControlPanel(), + "Allows control of the GUI"); + centerTopSplitPane.setLeftComponent(new PunchingBagSim()); centerTopSplitPane.setRightComponent(tabbedPane); bottomTopSplitPane.setTopComponent(centerTopSplitPane); bottomTopSplitPane.setBottomComponent(new SerialPanel()); - frame.add(bottomTopSplitPane, BorderLayout.CENTER); + bagDebugingPanel.add(bottomTopSplitPane, BorderLayout.CENTER); + + programOptionsPanel.add(new DisplayPanel()); + + //frame.add(bagDebugingPanel); + frame.add(programOptionsPanel); + + //setMode(currentMode); + + GraphicsDevice device; + device = GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice(); + if (device.isFullScreenSupported()) { + device.setFullScreenWindow(frame); + } else { + System.err.println("Full screen not supported"); + } + frame.setVisible(true); // frame.pack(); } - void playKey(int key) { - try { - AudioInputStream sound = AudioSystem.getAudioInputStream(keys[key]); - DataLine.Info dataLine = new DataLine.Info(Clip.class, - sound.getFormat()); - Clip clip = (Clip) AudioSystem.getLine(dataLine); - clip.open(sound); - clip.start(); - } catch (LineUnavailableException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (UnsupportedAudioFileException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + void setMode(Mode mode) { + this.currentMode = mode; + if (currentMode == Mode.Menu) { + bagDebugingPanel.setVisible(false); + programOptionsPanel.setVisible(true); + } else if (currentMode == Mode.Debug) { + bagDebugingPanel.setVisible(true); + programOptionsPanel.setVisible(false); + } + } + + class MenuPanel extends JPanel implements ActionListener { + MenuPanel() { + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + + this.add(Box.createVerticalGlue()); + + JPanel bottom = new JPanel(); + bottom.setAlignmentX(1f); + bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS)); + + JButton debugMode = new JButton("Debug Mode"); + debugMode.addActionListener(this); + + bottom.add(debugMode); + bottom.add(Box.createRigidArea(new Dimension(15, 0))); + + this.add(bottom); + this.add(Box.createRigidArea(new Dimension(0, 15))); + } + + @Override + public void actionPerformed(ActionEvent e) { + setMode(Mode.Debug); + } + } + + class DisplayPanel extends JPanel implements Runnable { + ArrayList<File> files = new ArrayList<File>(); + ImagePanel imagePanel = new ImagePanel(); + int image = 0; + + DisplayPanel() { + this.add(imagePanel); + new Thread(this).start(); + } + + /*public void paintComponent(Graphics g) { + if (images.size() != 0) { + if (debugImages) + System.out.println("Displaying Image"); + g.drawImage(images.get(image), 0, 0, frame.getSize().width, frame.getSize().height, null); + } + }*/ + + public void run() { + loadImages(); + while (true) { + try { + imagePanel.loadImage(files.get(image)); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + if (image == (files.size() - 1)) { + image = 0; + } else { + image++; + } + if (debugImages) + System.out.println("Moving to the next image "); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + public void loadImages() { + if (debugImages) + System.out.println("Loading images..."); + File imageDir = new File(imageDirectory); + System.out.println("Looking in " + imageDirectory); + File[] dirFiles = imageDir.listFiles(); + for (int i = 0; i < dirFiles.length; i++) { + if (dirFiles[i].getName().contains(".jpg")) { + if (debugImages) + System.out.println("Found .jpg " + dirFiles[i].getName()); + files.add(dirFiles[i]); + } + } + if (debugImages) + System.out.println("Finished loading images"); } } @@ -247,6 +368,21 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, } } + class GUIControlPanel extends JPanel implements ActionListener { + public GUIControlPanel() { + JButton menuButton = new JButton("Menu"); + menuButton.addActionListener(this); + + this.add(menuButton); + } + + @Override + public void actionPerformed(ActionEvent e) { + setMode(Mode.Menu); + + } + } + class LEDControlPanel extends JPanel implements ActionListener { String[] colours = { " ", "R", "Y", "G" }; @@ -519,10 +655,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, @Override public void mousePressed(MouseEvent me) { - if (currentMode == Mode.Interactive) { - // bag.buttonPressed(me.getX()/(buttonSize+ledSize), - // me.getY()/(buttonSize+ledSize)); - } + } @Override @@ -534,7 +667,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, @Override public void contact(Contact c) { // TODO Auto-generated method stub - + } } @@ -589,8 +722,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, @Override public void buttonPressed(int x, int y) { System.out.println("Button Pressed: " + x + " " + y); - //bag.circleExpand(x, y, 16); - //bag.setLED(x, y, Color.red); + // bag.circleExpand(x, y, 16); + // bag.setLED(x, y, Color.red); bag.noise(new Rectangle(0, 0, 9, 20), 5000); /* * for (int bx=0; bx<bag.ledWidth; bx++) { for (int by=0; |