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.java192
1 files changed, 192 insertions, 0 deletions
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
new file mode 100644
index 0000000..8008c58
--- /dev/null
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -0,0 +1,192 @@
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Toolkit;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+public class PunchingBagGUI implements MouseListener, MouseMotionListener {
+
+ public static enum Mode {
+ Menu, Interactive, Game
+ };
+
+ 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
+ };
+
+ final boolean useDebugScreen = true;
+
+ public static void main(String[] args) throws InterruptedException {
+ SlaveArduino.listPorts();
+
+ //new PunchingBagGUI();
+ }
+
+ PunchingBagGUI() throws InterruptedException {
+ frame = new JFrame("Punching Bag GUI");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.setSize(300, 300);
+ 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());
+
+ draw();
+ }
+
+ void draw() throws InterruptedException {
+ bagSimFrame.paintComponents(bagSimFrame.getGraphics());
+ // bagSimFrame.paintAll(bagSimFrame.getGraphics());
+ Thread.sleep(16);
+ draw();
+ }
+
+ public void runInteractively(Contact contact) {
+ bagSimFrame.add(new Ripple(contact));
+ }
+
+ 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) {
+ // Max size of ripple is 60cm
+ // Ripple will expand 60cm * strength/100
+
+ SlaveArduino.Colour colour = SlaveArduino.Colour.Red;
+ g.setColor(new Color(255, 0, 0, 255 - width));
+ boolean drawnSomething = false;
+
+ for (double i = 0; i < 360; i++) {
+ int x = contact.area.x
+ + (int) Math.round(currentWidth * Math.cos(i));
+ int y = contact.area.y
+ + (int) Math.round(currentWidth * Math.sin(i));
+
+ if (x >= 0 && x <= 8 && y >= 0 && y <= 19) {
+ SlaveArduino.setLED(colour, x, y);
+ drawnSomething =true;
+ }
+
+ if (useDebugScreen) {
+ g.fillOval(40 * x, 40 * y, 6, 6);
+
+ }
+ }
+ if (!drawnSomething) bagSimFrame.remove(this);
+
+ currentWidth += 0.1 + (currentWidth/10) + (contact.strength/130) ;
+ }
+ }
+
+ 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
+
+ }
+
+ public void mouseEntered(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseExited(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ 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()));
+ }
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ public void mouseReleased(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseDragged(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseMoved(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+}