From 4667303e15c534d390abee467eeaaee850e2d6f5 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Wed, 24 Aug 2011 22:39:49 +0100 Subject: New API for bag. --- PunchingBag/bin/ButtonListener.class | Bin 0 -> 173 bytes PunchingBag/bin/Contact.class | Bin 0 -> 444 bytes PunchingBag/bin/PunchingBag.class | Bin 0 -> 755 bytes PunchingBag/bin/PunchingBagGUI$Area.class | Bin 595 -> 0 bytes PunchingBag/bin/PunchingBagGUI$Contact.class | Bin 974 -> 0 bytes PunchingBag/bin/PunchingBagGUI$Direction.class | Bin 1191 -> 1191 bytes PunchingBag/bin/PunchingBagGUI$Mode.class | Bin 1070 -> 1070 bytes PunchingBag/bin/PunchingBagGUI$Ripple.class | Bin 1943 -> 1614 bytes PunchingBag/bin/PunchingBagGUI.class | Bin 3842 -> 3613 bytes PunchingBag/src/ButtonListener.java | 7 +++ PunchingBag/src/Contact.java | 19 +++++++ PunchingBag/src/PunchingBag.java | 24 ++++++++ PunchingBag/src/PunchingBagGUI.java | 73 +++++++------------------ 13 files changed, 69 insertions(+), 54 deletions(-) create mode 100644 PunchingBag/bin/ButtonListener.class create mode 100644 PunchingBag/bin/Contact.class create mode 100644 PunchingBag/bin/PunchingBag.class delete mode 100644 PunchingBag/bin/PunchingBagGUI$Area.class delete mode 100644 PunchingBag/bin/PunchingBagGUI$Contact.class create mode 100644 PunchingBag/src/ButtonListener.java create mode 100644 PunchingBag/src/Contact.java create mode 100644 PunchingBag/src/PunchingBag.java diff --git a/PunchingBag/bin/ButtonListener.class b/PunchingBag/bin/ButtonListener.class new file mode 100644 index 0000000..acd177b Binary files /dev/null and b/PunchingBag/bin/ButtonListener.class differ diff --git a/PunchingBag/bin/Contact.class b/PunchingBag/bin/Contact.class new file mode 100644 index 0000000..2e6fbc5 Binary files /dev/null and b/PunchingBag/bin/Contact.class differ diff --git a/PunchingBag/bin/PunchingBag.class b/PunchingBag/bin/PunchingBag.class new file mode 100644 index 0000000..65f48c7 Binary files /dev/null and b/PunchingBag/bin/PunchingBag.class differ diff --git a/PunchingBag/bin/PunchingBagGUI$Area.class b/PunchingBag/bin/PunchingBagGUI$Area.class deleted file mode 100644 index 49b8d90..0000000 Binary files a/PunchingBag/bin/PunchingBagGUI$Area.class and /dev/null differ diff --git a/PunchingBag/bin/PunchingBagGUI$Contact.class b/PunchingBag/bin/PunchingBagGUI$Contact.class deleted file mode 100644 index 54a4389..0000000 Binary files a/PunchingBag/bin/PunchingBagGUI$Contact.class and /dev/null differ diff --git a/PunchingBag/bin/PunchingBagGUI$Direction.class b/PunchingBag/bin/PunchingBagGUI$Direction.class index acb7f1a..654a7b0 100644 Binary files a/PunchingBag/bin/PunchingBagGUI$Direction.class and b/PunchingBag/bin/PunchingBagGUI$Direction.class differ diff --git a/PunchingBag/bin/PunchingBagGUI$Mode.class b/PunchingBag/bin/PunchingBagGUI$Mode.class index 27359c3..1b33757 100644 Binary files a/PunchingBag/bin/PunchingBagGUI$Mode.class and b/PunchingBag/bin/PunchingBagGUI$Mode.class differ diff --git a/PunchingBag/bin/PunchingBagGUI$Ripple.class b/PunchingBag/bin/PunchingBagGUI$Ripple.class index 4a7e871..b824480 100644 Binary files a/PunchingBag/bin/PunchingBagGUI$Ripple.class and b/PunchingBag/bin/PunchingBagGUI$Ripple.class differ diff --git a/PunchingBag/bin/PunchingBagGUI.class b/PunchingBag/bin/PunchingBagGUI.class index 424f5bc..362e5e7 100644 Binary files a/PunchingBag/bin/PunchingBagGUI.class and b/PunchingBag/bin/PunchingBagGUI.class differ diff --git a/PunchingBag/src/ButtonListener.java b/PunchingBag/src/ButtonListener.java new file mode 100644 index 0000000..9514da6 --- /dev/null +++ b/PunchingBag/src/ButtonListener.java @@ -0,0 +1,7 @@ +import java.util.EventListener; + +interface ButtonListener extends EventListener { + + void contact(Contact c); + +} diff --git a/PunchingBag/src/Contact.java b/PunchingBag/src/Contact.java new file mode 100644 index 0000000..8e8970b --- /dev/null +++ b/PunchingBag/src/Contact.java @@ -0,0 +1,19 @@ + + + class Contact { + // Height of bag is + + final long time; // Time of the contact with the bag (in millis) + final int x; + final int y; + final int strength; // 0 - 100 + + Contact(long time, int x, int y, int strength) { + this.time = time; + this.x = x; + this.y = y; + this.strength = strength; + } + + + } \ No newline at end of file diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java new file mode 100644 index 0000000..278a8fc --- /dev/null +++ b/PunchingBag/src/PunchingBag.java @@ -0,0 +1,24 @@ +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.event.EventListenerList; + + +public class PunchingBag { + protected EventListenerList listenerList = new EventListenerList(); + + /** + * Adds an ActionListener to the button. + * @param l the ActionListener to be added + */ + public void addButtonListener(ButtonListener l) { + listenerList.add(ButtonListener.class, l); + } + + public void setLED(byte x, byte y, byte intensity) { + + + } + +} diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 8008c58..1bd1415 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -1,14 +1,16 @@ import java.awt.Color; import java.awt.Graphics; +import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.geom.Area; import javax.swing.JFrame; import javax.swing.JPanel; -public class PunchingBagGUI implements MouseListener, MouseMotionListener { +public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener { public static enum Mode { Menu, Interactive, Game @@ -21,6 +23,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { static JFrame frame; static JFrame bagSimFrame; + + static PunchingBag bag = new PunchingBag(); // Contact public enum Direction { @@ -30,9 +34,10 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { final boolean useDebugScreen = true; public static void main(String[] args) throws InterruptedException { - SlaveArduino.listPorts(); + //SlaveArduino.listPorts(); + + new PunchingBagGUI(); - //new PunchingBagGUI(); } PunchingBagGUI() throws InterruptedException { @@ -51,6 +56,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { System.out.println("Bag X: " + bagSimFrame.getX() + " Bag Y: " + bagSimFrame.getY()); + + bag.addButtonListener(this); draw(); } @@ -68,19 +75,11 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { class Ripple extends JPanel { final Contact contact; - final int width; double currentWidth; Ripple(Contact contact) { super(); this.contact = contact; - if (contact.area.width >= contact.area.height) { - width = contact.area.width; - } else { - width = contact.area.height; - } - currentWidth = width; - if (currentWidth <= 0.5) currentWidth = 0.5; } public void paintComponent(Graphics g) { @@ -88,13 +87,13 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { // Ripple will expand 60cm * strength/100 SlaveArduino.Colour colour = SlaveArduino.Colour.Red; - g.setColor(new Color(255, 0, 0, 255 - width)); + g.setColor(new Color(255, 0, 0, 255)); boolean drawnSomething = false; for (double i = 0; i < 360; i++) { - int x = contact.area.x + int x = contact.x + (int) Math.round(currentWidth * Math.cos(i)); - int y = contact.area.y + int y = contact.y + (int) Math.round(currentWidth * Math.sin(i)); if (x >= 0 && x <= 8 && y >= 0 && y <= 19) { @@ -113,41 +112,6 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { } } - class Contact { - // Height of bag is - - final long time; // Time of the contact with the bag (in millis) - final Area area; - final Direction direction; - final int strength; // 0 - 100 - - Contact(long time, Area area, Direction direction, int strength) { - this.time = time; - this.area = area; - this.direction = direction; - this.strength = strength; - } - - Contact(long time, Area area) { - this(time, area, Direction.None, 50); - } - - } - - class Area { - public final int x; - public final int y; - public final int width; - public final int height; - - Area(int x, int y, int width, int height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - - } public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub @@ -165,13 +129,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { } public void mousePressed(MouseEvent arg0) { - System.out.println("Mouse moved"); if (currentMode == Mode.Interactive) { - System.out.println("Button: " + arg0.getButton()); - runInteractively(new Contact(System.currentTimeMillis(), new Area( - (arg0.getX() - 4) / 40, (arg0.getY() - 30) / 40, 0, 0), Direction.None , 30 * arg0.getButton())); + runInteractively(new Contact(System.currentTimeMillis(), (arg0.getX() - 4)/ 40 , (arg0.getY() - 30) / 40, 0)); } - Toolkit.getDefaultToolkit().beep(); } public void mouseReleased(MouseEvent arg0) { @@ -189,4 +149,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener { } + public void contact(Contact c) { + // TODO Auto-generated method stub + + } + } -- cgit v1.2.3