|
3 | 3 | import processing.core.PShape; |
4 | 4 |
|
5 | 5 | public class Rectangles extends PApplet { |
6 | | - |
7 | | - private PShape square; |
| 6 | + |
| 7 | + private ShapeObj square; |
| 8 | + private ShapeObj rectangle; |
8 | 9 |
|
9 | 10 | public void settings() { |
10 | | - System.out.println(Test.testString); |
11 | | - size(500, 500); |
12 | | - //this.square = createShape(RECT, 0, 0, 50, 50); |
13 | | - //this.square.setFill(color(0, 0, 255)); |
14 | | - //this.square.setStroke(false); |
| 11 | + size(640, 360); |
| 12 | + } |
| 13 | + |
| 14 | + public void setup() { |
| 15 | + background(0); |
| 16 | + frameRate(60); |
| 17 | + // Place square and rectangle in bottom corners of screen |
| 18 | + this.square = new ShapeObj(50, 50, 0, height - 50); |
| 19 | + this.rectangle = new ShapeObj(100, 50, this.width - 100, this.height - 50); |
| 20 | + |
15 | 21 | } |
16 | 22 |
|
17 | 23 | public void draw() { |
18 | | - rect(30, 20, 25, 25); |
| 24 | + background(0); |
| 25 | + this.square.update(1/60); |
| 26 | + System.out.println("(" + this.square.py.getxPos() + ", " + |
| 27 | + this.square.py.getyPos() + ")"); |
| 28 | + } |
| 29 | + |
| 30 | + public void keyPressed() { |
| 31 | + if (key == CODED) { |
| 32 | + if (keyCode == LEFT) { |
| 33 | + System.out.println("LEFT pressed"); |
| 34 | + this.square.py.setxA(-1); |
| 35 | + } |
| 36 | + if (keyCode == RIGHT) { |
| 37 | + System.out.println("RIGHT pressed"); |
| 38 | + this.square.py.setxA(1); |
| 39 | + } |
| 40 | + } |
| 41 | + if (key == ' ') { |
| 42 | + System.out.println("SPACE pressed"); |
| 43 | + this.square.py.setyA(20); |
| 44 | + } |
19 | 45 | } |
20 | 46 |
|
21 | | - //API stuff from https://happycoding.io/tutorials/java/processing-in-java |
| 47 | + // API stuff from https://happycoding.io/tutorials/java/processing-in-java |
22 | 48 |
|
23 | 49 | public static void main(String[] args) { |
24 | 50 | String[] processingArgs = {"Rectangles"}; |
25 | 51 | Rectangles sketch = new Rectangles(); |
26 | 52 | PApplet.runSketch(processingArgs, sketch); |
27 | 53 | } |
| 54 | + |
| 55 | + private class ShapeObj { |
| 56 | + public Physics py; |
| 57 | + public PShape shape; |
| 58 | + |
| 59 | + public ShapeObj(float width, float height, float x, float y) { |
| 60 | + this.py = new Physics(x, y, width, height); |
| 61 | + this.shape = createShape(RECT, 0, 0, width, height); |
| 62 | + this.shape.setFill(color(random(255), random(255), random(255))); |
| 63 | + this.shape.setStroke(false); |
| 64 | + shape(this.shape, x, y); |
| 65 | + } |
| 66 | + |
| 67 | + public void update(float t) { |
| 68 | + this.py.updateX(t); |
| 69 | + this.py.updateY(t); |
| 70 | + shape(this.shape, this.py.getxPos(), this.py.getyPos()); |
| 71 | + } |
| 72 | + |
| 73 | + public void setPos(float x, float y) { |
| 74 | + this.py.setxPos(x); |
| 75 | + this.py.setyPos(y); |
| 76 | + shape(this.shape, x, y); |
| 77 | + } |
| 78 | + |
| 79 | + } |
28 | 80 | } |
0 commit comments