aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBagGUI.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/PunchingBagGUI.java')
-rw-r--r--PunchingBag/src/PunchingBagGUI.java101
1 files changed, 67 insertions, 34 deletions
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
index 19385f5..aacb2ac 100644
--- a/PunchingBag/src/PunchingBagGUI.java
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -27,7 +27,8 @@ import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
-public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener {
+public class PunchingBagGUI implements MouseListener, MouseMotionListener,
+ ButtonListener, LEDListener {
public static enum Mode {
Menu, Interactive, Game
@@ -53,7 +54,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
}
PunchingBagGUI() throws InterruptedException {
- //bag.buttonArduino.listPorts();
+ bag.connectToArduinos();
frame = new JFrame("Punching Bag GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -67,12 +68,15 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
bag.addButtonListener(this);
bag.addLEDChangeListener(this);
- JSplitPane bottomTopSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
+ JSplitPane bottomTopSplitPane = new JSplitPane(
+ JSplitPane.VERTICAL_SPLIT);
- JSplitPane centerTopSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
+ JSplitPane centerTopSplitPane = new JSplitPane(
+ JSplitPane.HORIZONTAL_SPLIT);
JTabbedPane tabbedPane = new JTabbedPane();
- tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(), "Allows control of individual LED's");
+ tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(),
+ "Allows control of individual LED's");
centerTopSplitPane.setLeftComponent(new PunchingBagSim());
centerTopSplitPane.setRightComponent(tabbedPane);
@@ -81,11 +85,12 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
bottomTopSplitPane.setBottomComponent(new SerialPanel());
frame.add(bottomTopSplitPane, BorderLayout.CENTER);
-
- //frame.pack();
+
+ // frame.pack();
}
- class SerialPanel extends JPanel implements ActionListener {
+ class SerialPanel extends JPanel implements ActionListener,
+ SerialReadListener, SerialWriteListener {
JTextArea buttonIn = new JTextArea();
JTextArea buttonOut = new JTextArea();
JTextArea ledIn = new JTextArea();
@@ -93,8 +98,10 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
public SerialPanel() {
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- JSplitPane buttonSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- JSplitPane ledSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
+ JSplitPane buttonSplitPane = new JSplitPane(
+ JSplitPane.HORIZONTAL_SPLIT);
+ JSplitPane ledSplitPane = new JSplitPane(
+ JSplitPane.HORIZONTAL_SPLIT);
/*
* splitPane.setOneTouchExpandable(true);
* buttonSplitPane.setOneTouchExpandable(true);
@@ -108,56 +115,55 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
JButton clear = new JButton("Clear");
clear.addActionListener(this);
- // buttonIn.setColumns(20);
- // buttonIn.setRows(5);
- buttonIn.setBorder(BorderFactory.createTitledBorder("Serial In - Button Arduino"));
- // buttonOut.setColumns(20);
- // buttonOut.setRows(5);
- buttonOut.setBorder(BorderFactory.createTitledBorder("Serial Out - Button Arduino"));
- // ledIn.setColumns(20);
- // ledIn.setRows(5);
- ledIn.setBorder(BorderFactory.createTitledBorder("Serial In - LED Arduino"));
- // ledOut.setColumns(20);
- // ledOut.setRows(5);
- ledOut.setBorder(BorderFactory.createTitledBorder("Serial Out - LED Arduino"));
-
JPanel buttonInPanel = new JPanel();
JScrollPane areaScrollPane = new JScrollPane(buttonIn);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
buttonInPanel.add(areaScrollPane, BorderLayout.CENTER);
clear.setActionCommand("BI");
buttonInPanel.add(clear, BorderLayout.SOUTH);
+ buttonInPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial In - Button Arduino"));
buttonSplitPane.setLeftComponent(buttonInPanel);
JPanel buttonOutPanel = new JPanel();
areaScrollPane = new JScrollPane(buttonOut);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
buttonOutPanel.add(areaScrollPane, BorderLayout.CENTER);
clear = new JButton("Clear");
clear.setActionCommand("BO");
buttonOutPanel.add(clear, BorderLayout.SOUTH);
+ buttonOutPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial Out - Button Arduino"));
buttonSplitPane.setRightComponent(buttonOutPanel);
JPanel ledInPanel = new JPanel();
areaScrollPane = new JScrollPane(ledIn);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
ledInPanel.add(areaScrollPane, BorderLayout.CENTER);
clear = new JButton("Clear");
clear.setActionCommand("LI");
ledInPanel.add(clear, BorderLayout.SOUTH);
+ ledInPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial In - LED Arduino"));
ledSplitPane.setLeftComponent(ledInPanel);
JPanel ledOutPanel = new JPanel();
areaScrollPane = new JScrollPane(ledOut);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
ledOutPanel.add(areaScrollPane, BorderLayout.CENTER);
clear = new JButton("Clear");
clear.setActionCommand("LO");
ledOutPanel.add(clear, BorderLayout.SOUTH);
+ ledOutPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial Out - LED Arduino"));
ledSplitPane.setRightComponent(ledOutPanel);
splitPane.setLeftComponent(buttonSplitPane);
@@ -165,6 +171,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
this.add(splitPane);
+ bag.buttonArduino.addSerialReadListener(this);
+ bag.buttonArduino.addSerialWriteListener(this);
+
}
@Override
@@ -172,6 +181,22 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
// TODO Auto-generated method stub
}
+
+ @Override
+ public void serialWriteEvent(byte b) {
+ buttonOut.append(String.valueOf(b));
+ }
+
+ @Override
+ public void serialWriteEvent(byte[] b) {
+ buttonOut.append(String.valueOf(b));
+ }
+
+ @Override
+ public void serialReadEvent(int b) {
+ if (b != -1)
+ buttonIn.append(String.valueOf(b));
+ }
}
class LEDControlPanel extends JPanel implements ActionListener {
@@ -195,7 +220,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
JComboBox rygCombo = new JComboBox(colours);
rygCombo.setSelectedIndex(0);
rygCombo.addActionListener(this);
- rygCombo.setActionCommand(String.valueOf(x) + "," + String.valueOf(y));
+ rygCombo.setActionCommand(String.valueOf(x) + ","
+ + String.valueOf(y));
// rygCombo.setRenderer(renderer);
this.add(rygCombo, c);
@@ -225,8 +251,12 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
} else if (((JComboBox) a.getSource()).getSelectedIndex() == 3) {
colour = PunchingBag.Colour.Green;
}
- System.out.println("Setting " + a.getActionCommand().split(",")[0] + " " + a.getActionCommand().split(",")[1]);
- bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]), Integer.valueOf(a.getActionCommand().split(",")[1]), colour);
+ System.out.println("Setting "
+ + a.getActionCommand().split(",")[0] + " "
+ + a.getActionCommand().split(",")[1]);
+ bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]),
+ Integer.valueOf(a.getActionCommand().split(",")[1]),
+ colour);
}
}
@@ -235,7 +265,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
super();
}
- public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+ public Component getListCellRendererComponent(JList list,
+ Object value, int index, boolean isSelected,
+ boolean cellHasFocus) {
// Get the selected index. (The index param isn't
// always valid, so just use the value.)
@@ -256,7 +288,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
}
}
- class PunchingBagSim extends JPanel implements ButtonListener, LEDListener, MouseListener {
+ class PunchingBagSim extends JPanel implements ButtonListener, LEDListener,
+ MouseListener {
int buttonMinSize = 10;
int buttonMaxSize = 50;
@@ -385,8 +418,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
g.setColor(Color.black);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
- g.setColor(new Color(220,220,220));
- g.fillRect(4, 4, this.getWidth()-8, this.getHeight()-8);
+ g.setColor(new Color(220, 220, 220));
+ g.fillRect(4, 4, this.getWidth() - 8, this.getHeight() - 8);
}