diff options
Diffstat (limited to 'PunchingBag/src')
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 110 | ||||
-rw-r--r-- | PunchingBag/src/PunchingBagGUI.java | 356 |
2 files changed, 376 insertions, 90 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 00be6e0..7833eaf 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -58,7 +58,7 @@ public class PunchingBag implements Runnable { ledListenerList.add(LEDListener.class, l); } - public void addLEDChangeListener(AccelListener l) { + public void addAccelChangeListener(AccelListener l) { accelListenerList.add(AccelListener.class, l); } @@ -66,7 +66,7 @@ public class PunchingBag implements Runnable { return leds[x][y]; } - public boolean setLED(int x, int y, Colour colour) { + private boolean setLEDInternal(int x, int y, Colour colour) { if (x >= 0 && x < ledWidth && y >= 0 && y < ledHeight) { if (leds[x][y] != colour) { leds[x][y] = colour; @@ -78,8 +78,12 @@ public class PunchingBag implements Runnable { return false; } } + + public void setLED(int x, int y, Colour colour) { + runningEffects.add(new Point(x,y,colour)); + } - void CircleExpand(int x, int y, int intensity) { + void circleExpand(int x, int y, int intensity) { runningEffects.add(new CircleExpand(x, y, 100)); } @@ -95,6 +99,23 @@ public class PunchingBag implements Runnable { abstract public void draw(); } + + class Point extends Effect { + final int x; + final int y; + final Colour colour; + + public Point(int x, int y, Colour colour) { + this.x = x; + this.y = y; + this.colour = colour; + } + + + public void draw() { + setLEDInternal(x,y,colour); + } + } class Text extends Effect { final String string; @@ -148,16 +169,16 @@ public class PunchingBag implements Runnable { // longhand: currentRadius = currentRadius + 0.1 + (currentRadius / // 10); - if (!DrawEllipse(x, y, (int) currentRadius, (int) currentRadius, + if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, colour)) stop = true; } } - public boolean DrawEllipse(int x, int y, int radx, int rady, Colour colour) { + public boolean drawEllipse(int x, int y, int radx, int rady, Colour colour) { if (radx == 0 && rady == 0) { - return setLED(x, y, colour); + return setLEDInternal(x, y, colour); } boolean doneSomething = false; @@ -167,16 +188,16 @@ public class PunchingBag implements Runnable { int err = b2 - (2 * rady - 1) * a2, e2; /* error value in the first step */ do { - if (setLED(x + dx, y + dy, colour)) {/* I. Quadrant */ + if (setLEDInternal(x + dx, y + dy, colour)) {/* I. Quadrant */ doneSomething = true; } - if (setLED(x - dx, y + dy, colour)) {/* II. Quadrant */ + if (setLEDInternal(x - dx, y + dy, colour)) {/* II. Quadrant */ doneSomething = true; } - if (setLED(x - dx, y - dy, colour)) {/* III. Quadrant */ + if (setLEDInternal(x - dx, y - dy, colour)) {/* III. Quadrant */ doneSomething = true; } - if (setLED(x + dx, y - dy, colour)) {/* IV. Quadrant */ + if (setLEDInternal(x + dx, y - dy, colour)) {/* IV. Quadrant */ doneSomething = true; } @@ -192,10 +213,10 @@ public class PunchingBag implements Runnable { } while (dy >= 0); while (dx++ < radx) { /* correction for flat ellipses (b=1) */ - if (setLED(x + dx, y, colour)) { + if (setLEDInternal(x + dx, y, colour)) { doneSomething = true; } - if (setLED(x - dx, y, colour)) { + if (setLEDInternal(x - dx, y, colour)) { doneSomething = true; } } @@ -232,7 +253,7 @@ public class PunchingBag implements Runnable { } /* Clears the led grid and stops all running effects */ - void clearLEDGrid() { + private void clearLEDGrid() { /* * for (Iterator<Effect> iter = runningEffects.iterator(); * iter.hasNext();) { ((Effect) iter.next()).stop(); } @@ -260,44 +281,38 @@ public class PunchingBag implements Runnable { public void run() { while (true) { - synchronized (runningEffects) { // Should prevent - // ConcurrentModificationException's - // (havent managed to produce one - // though - for (Iterator<Effect> iter = runningEffects.iterator(); iter - .hasNext();) { - Effect ef = (Effect) iter.next(); - if (ef.stop) { - iter.remove(); - } else {// if ((ef.lastRefresh + (1000 / ef.refreshRate)) <= - // Systems.currentTimeMillis()) { - ef.draw(); - ef.lastRefresh = System.currentTimeMillis(); + synchronized (leds) { + clearLEDGrid(); + + synchronized (runningEffects) { // Should prevent + // ConcurrentModificationException's + // (havent managed to produce one + // though + for (Iterator<Effect> iter = runningEffects.iterator(); iter + .hasNext();) { + Effect ef = (Effect) iter.next(); + if (ef.stop) { + iter.remove(); + } else {// if ((ef.lastRefresh + (1000 / + // ef.refreshRate)) <= + // Systems.currentTimeMillis()) { + ef.draw(); + ef.lastRefresh = System.currentTimeMillis(); + } } } } - try { - ledArduino.write(((byte) 108)); - ledArduino.write(((byte) 101)); - ledArduino.write(rawLeds); - } catch (IOException e1) { - e1.printStackTrace(); - } - - try { - while (true) { - int value = ledArduino.read(); - if (value != -1) { - System.out.print(ledArduino.read()); - } else { - break; - } - } - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } + /* + * try { ledArduino.write(((byte) 108)); ledArduino.write(((byte) + * 101)); ledArduino.write(rawLeds); } catch (IOException e1) { + * e1.printStackTrace(); } + * + * try { while (true) { int value = ledArduino.read(); if (value != + * -1) { System.out.print(ledArduino.read()); } else { break; } } } + * catch (IOException e1) { // TODO Auto-generated catch block + * e1.printStackTrace(); } + */ if (ledsChanged) { LEDListener[] LEDListeners = ledListenerList @@ -328,7 +343,6 @@ public class PunchingBag implements Runnable { } // TODO: Hack? - clearLEDGrid(); try { Thread.sleep(1000 / 60); diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 63f05ae..60cacaa 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -1,10 +1,26 @@ +import java.awt.BorderLayout;
import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
import java.awt.Graphics;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
+import javax.swing.BorderFactory;
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JComponent;
import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JPanel;
+import javax.swing.ListCellRenderer;
public class PunchingBagGUI implements MouseListener, MouseMotionListener,
ButtonListener, LEDListener {
@@ -15,47 +31,306 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Mode currentMode = Mode.Interactive;
- final int bagXSize = 360;
- final int bagYSize = 840;
-
static JFrame frame;
- static JFrame bagSimFrame;
// Contact
public enum Direction {
None, Up, Down, Left, Right
};
-
- PunchingBag bag = new PunchingBag();
+
+ static PunchingBag bag = new PunchingBag();
final boolean useDebugScreen = true;
public static void main(String[] args) throws InterruptedException {
- // SlaveArduino.listPorts();
new PunchingBagGUI();
}
PunchingBagGUI() throws InterruptedException {
+ bag.buttonArduino.listPorts();
+
frame = new JFrame("Punching Bag GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(300, 300);
+ // frame.setSize(600, 600);
frame.setVisible(true);
- bagSimFrame = new JFrame("Punching Bag Sim");
- bagSimFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- bagSimFrame.setSize(bagXSize, bagYSize);
- bagSimFrame.addMouseListener(this);
- bagSimFrame.addMouseMotionListener(this);
- bagSimFrame.setVisible(true);
-
- System.out.println("Bag X: " + bagSimFrame.getX() + " Bag Y: "
- + bagSimFrame.getY());
+ frame.addMouseListener(this);
+ frame.addMouseMotionListener(this);
bag.addButtonListener(this);
bag.addLEDChangeListener(this);
+
+ frame.add(new PunchingBagSim(), BorderLayout.WEST);
+
+ frame.add(new LEDControlPanel(), BorderLayout.CENTER);
+
+ }
+
+ class LEDControlPanel extends JPanel implements ActionListener {
+ String[] colours = {" ", "R", "Y", "G" };
+
+ public LEDControlPanel() {
+ this.setLayout(new GridBagLayout());
+
+ GridBagConstraints c = new GridBagConstraints();
+ c.fill = GridBagConstraints.BOTH;
+ c.anchor = GridBagConstraints.CENTER;
+ // c.weightx = 0.2;
+ // c.weighty = 0.2;
+
+ for (int y = 0; y < bag.ledHeight; y++) {
+ for (int x = 0; x < bag.ledWidth; x++) {
+ c.gridx = x;
+ c.gridy = y;
+
+ RYGComboBoxRenderer renderer = new RYGComboBoxRenderer();
+ JComboBox rygCombo = new JComboBox(colours);
+ rygCombo.setSelectedIndex(0);
+ rygCombo.addActionListener(this);
+ rygCombo.setActionCommand(String.valueOf(x) + "," + String.valueOf(y));
+ //rygCombo.setRenderer(renderer);
+
+ this.add(rygCombo, c);
+ }
+ }
+ }
+
+ public void actionPerformed(ActionEvent a) {
+ PunchingBag.Colour colour = PunchingBag.Colour.None;
+ if (((JComboBox)a.getSource()).getSelectedIndex() == 1) {
+ colour = PunchingBag.Colour.Red;
+ } else if (((JComboBox)a.getSource()).getSelectedIndex() == 2) {
+ colour = PunchingBag.Colour.Yellow;
+ } 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);
+ }
+
+ class RYGComboBoxRenderer extends JLabel implements ListCellRenderer {
+ public RYGComboBoxRenderer() {
+ super();
+ }
+
+ 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.)
+
+ if (isSelected) {
+ setBackground(list.getSelectionBackground());
+ setForeground(list.getSelectionForeground());
+ } else {
+ setBackground(list.getBackground());
+ setForeground(list.getForeground());
+ }
+
+ if (index != -1)
+ setText(colours[index]);
+
+ return this;
+ }
+ }
+ }
+
+ class PunchingBagSim extends JPanel implements ButtonListener, LEDListener,
+ MouseListener {
+ int buttonMinSize = 10;
+ int buttonMaxSize = 50;
+
+ int ledMinSize = 10;
+ int ledMaxSize = 50;
+
+ int buttonSize = 40;
+ int ledSize = 8;
+
+ public PunchingBagSim() {
+ /*
+ * this.setMinimumSize(new Dimension((bag.buttonWidth *
+ * buttonMinSize) + (bag.ledWidth * ledMinSize), (bag.buttonHeight *
+ * buttonMinSize) + (bag.ledHeight * ledMaxSize)));
+ * this.setMaximumSize(new Dimension((bag.buttonWidth *
+ * buttonMaxSize) + (bag.ledWidth * ledMaxSize), (bag.buttonHeight *
+ * buttonMaxSize) + (bag.ledHeight * ledMaxSize)));
+ */
+ bag.addButtonListener(this);
+ bag.addLEDChangeListener(this);
+
+ this.addMouseListener(this);
+
+ GridBagLayout gbl = new GridBagLayout();
+ this.setLayout(gbl);
+ GridBagConstraints c = new GridBagConstraints();
+
+ // c.insets = new Insets(1, 1, 1, 1);
+ c.fill = GridBagConstraints.BOTH;
+ c.anchor = GridBagConstraints.CENTER;
+ // c.weightx = 0.2;
+ // c.weighty = 0.2;
+ for (int y = 0; y < bag.ledHeight; y++) {
+ for (int x = 0; x < bag.ledWidth; x++) {
+ c.gridx = x * 2;
+ c.gridy = y * 2;
+
+ this.add(new LED(x, y), c);
+ }
+ }
+
+ // c.weightx = 0.8;
+ // c.weighty = 0.8;
+ for (int y = 0; y < bag.buttonHeight; y++) {
+ for (int x = 0; x < bag.buttonWidth; x++) {
+ c.gridx = (x * 2) + 1;
+ c.gridy = (y * 2) + 1;
+
+ this.add(new Button(x, y), c);
+ }
+ }
+
+ // this.setMinimumSize(new Dimension(500,500));
+ // this.setPreferredSize(new Dimension(500,500));
+
+ // this.setBackground(Color.BLUE);
+
+ frame.pack();
+ frame.setSize(500, 500);
+
+ this.setBorder(BorderFactory.createLineBorder(Color.black));
+ }
+
+ /*
+ * public Dimension getPreferredSize() { if () return new
+ * Dimension(super.,10); }
+ */
+
+ class LED extends JComponent implements LEDListener {
+ final int x;
+ final int y;
+
+ public LED(int x, int y) {
+ this.x = x;
+ this.y = y;
+ /*
+ * this.setMinimumSize(new Dimension(50,50)); this.setSize(50,
+ * 50); this.setPreferredSize(new Dimension(50,50));
+ */
+ bag.addLEDChangeListener(this);
+
+ }
+
+ public Dimension getPreferredSize() {
+ return new Dimension(10, 10);
+ }
+
+ public void paintComponent(Graphics g) {
+ if (bag.getLED(x, y) == PunchingBag.Colour.Red) {
+ g.setColor(Color.red);
+ } else if (bag.getLED(x, y) == PunchingBag.Colour.Green) {
+ g.setColor(Color.green);
+ } else if (bag.getLED(x, y) == PunchingBag.Colour.Yellow) {
+ g.setColor(Color.yellow);
+ } else {
+ g.setColor(Color.white);
+ }
+
+ g.fillOval(0, 0, this.getWidth(), this.getHeight());
+ }
+
+ @Override
+ public void LEDchanged() {
+ this.repaint();
+ }
+
+ }
+
+ class Button extends JPanel implements MouseListener {
+ final int x;
+ final int y;
+
+ public Button(int x, int y) {
+ this.x = x;
+ this.y = y;
+ setBorder(BorderFactory.createLineBorder(Color.black));
+ this.addMouseListener(this);
+ }
+
+ public Dimension getPreferredSize() {
+ return new Dimension(30, 30);
+ }
+
+ public void paintComponent(Graphics g) {
+ super.paintComponent(g);
+
+ g.setColor(Color.black);
+ g.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
+
+ }
+
+ public void mouseClicked(MouseEvent arg0) {
+ }
+
+ public void mouseEntered(MouseEvent arg0) {
+ }
+
+ public void mouseExited(MouseEvent arg0) {
+ }
+
+ public void mousePressed(MouseEvent arg0) {
+ System.out.println("Button Pressed");
+ bag.buttonPressed(x, y);
+ }
+
+ public void mouseReleased(MouseEvent arg0) {
+ }
+
+ }
+
+ public void buttonPressed(int x, int y) {
+ this.repaint();
+ }
+
+ public void LEDchanged() {
+ this.repaint();
+
+ }
+
+ @Override
+ public void mouseClicked(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mouseEntered(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mouseExited(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mousePressed(MouseEvent me) {
+ if (currentMode == Mode.Interactive) {
+ // bag.buttonPressed(me.getX()/(buttonSize+ledSize),
+ // me.getY()/(buttonSize+ledSize));
+ }
+ }
+
+ @Override
+ public void mouseReleased(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
}
public void runInteractively(Contact contact) {
@@ -79,10 +354,11 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, }
public void mousePressed(MouseEvent arg0) {
- if (currentMode == Mode.Interactive) {
- runInteractively(new Contact(System.currentTimeMillis(), (arg0
- .getX() - 4) / 40, (arg0.getY() - 30) / 40, 0));
- }
+ /*
+ * if (currentMode == Mode.Interactive) { runInteractively(new
+ * Contact(System.currentTimeMillis(), (arg0 .getX() - 4) / 40,
+ * (arg0.getY() - 30) / 40, 0)); }
+ */
}
public void mouseReleased(MouseEvent arg0) {
@@ -108,33 +384,29 @@ 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.circleExpand(x, y, 16);
}
@Override
public void LEDchanged() {
- for (int x = 0; x < bag.ledWidth; x++) {
- for (int y = 0; y < bag.ledHeight; y++) {
- if (useDebugScreen) {
- Graphics g = bagSimFrame.getGraphics();
- if (bag.getLED(x, y) == PunchingBag.Colour.Red) {
- g.setColor(Color.red);
- g.fillOval(40 * x, 40 * y, 6, 6);
- } else if (bag.getLED(x, y) == PunchingBag.Colour.Green) {
- g.setColor(Color.green);
- g.fillOval(40 * x, 40 * y, 6, 6);
- } else if (bag.getLED(x, y) == PunchingBag.Colour.Yellow) {
- g.setColor(Color.yellow);
- g.fillOval(40 * x, 40 * y, 6, 6);
- } else {
- g.setColor(Color.white);
- g.fillOval(40 * x, 40 * y, 6, 6);
- }
- }
- }
- }
+ /*
+ * for (int x = 0; x < bag.ledWidth; x++) { for (int y = 0; y <
+ * bag.ledHeight; y++) { if (useDebugScreen) { Graphics g =
+ * bagSimFrame.getGraphics(); if (bag.getLED(x, y) ==
+ * PunchingBag.Colour.Red) { g.setColor(Color.red); g.fillOval(40 * x,
+ * 40 * y, 6, 6); } else if (bag.getLED(x, y) ==
+ * PunchingBag.Colour.Green) { g.setColor(Color.green); g.fillOval(40 *
+ * x, 40 * y, 6, 6); } else if (bag.getLED(x, y) ==
+ * PunchingBag.Colour.Yellow) { g.setColor(Color.yellow); g.fillOval(40
+ * * x, 40 * y, 6, 6); } else { g.setColor(Color.white); g.fillOval(40 *
+ * x, 40 * y, 6, 6); } } } }
+ */
+
+ }
+ static PunchingBag getPunchingBag() {
+ return bag;
}
}
|