aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBagGUI.java
blob: 8008c58b7bd90bed0a57987399588efffab285c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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

	}

}