Skip to content

Commit 90cb4da

Browse files
committed
improve pen and fix null pointer in isTouchingSprite
1 parent b58f284 commit 90cb4da

File tree

8 files changed

+41
-15
lines changed

8 files changed

+41
-15
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ name: Changelog
33
index: 4
44
---
55

6+
## 3.2.2
7+
8+
- 🎨 Visual: Improve Pen rendering
9+
- 🐎 Perf: Pen does not draw everything again, but only the last additions.
10+
- 🐛 Fix: isTouchingSprite threw an error, if a sprite did not have a costume.
11+
- 🐛 Fix: exit method not found for Window class.
12+
613
## 3.2.1
714

815
- 🐛 Fix: Pen only drawing dots

docs/en/archives/Halloween/HalloweenStage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class HalloweenStage extends Stage
77
GhostSprite ghost;
88

99
public HalloweenStage() {
10-
super(400, 400, Renderer.P2D);
10+
super(400, 400);
1111
this.addBackdrop("bg", "sprites/background.jpg");
1212
this.addSound("bg", "sounds/background.wav");
1313

@@ -25,4 +25,8 @@ public HalloweenStage() {
2525
public void run() {
2626
this.playSound("bg");
2727
}
28+
29+
public static void main(String[] args) {
30+
new HalloweenStage();
31+
}
2832
}
Loading

examples/reference/StageDebug.gif

-189 KB
Loading

src/org/openpatch/scratch/Sprite.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,11 @@ public boolean isTouchingMousePointer() {
673673
int relativeMouseX = Math.round(mouse[0] - topLeftCornerX);
674674
int relativeMouseY = Math.round(mouse[1] - topLeftCornerY);
675675

676-
int color = this.costumes.get(this.getCurrentCostumeIndex())
677-
.getPixel(relativeMouseX, relativeMouseY);
678-
return Applet.getInstance().alpha(color) != 0;
676+
if (this.costumes.size() > this.getCurrentCostumeIndex()) {
677+
int color = this.costumes.get(this.getCurrentCostumeIndex())
678+
.getPixel(relativeMouseX, relativeMouseY);
679+
return Applet.getInstance().alpha(color) != 0;
680+
}
679681
}
680682

681683
return false;
@@ -688,7 +690,10 @@ public boolean isTouchingMousePointer() {
688690
* @return true if outside
689691
*/
690692
public boolean isTouchingEdge() {
691-
Image currentCostume = this.costumes.get(this.getCurrentCostumeIndex());
693+
Image currentCostume = null;
694+
if (this.costumes.size() > this.getCurrentCostumeIndex()) {
695+
currentCostume = this.costumes.get(this.getCurrentCostumeIndex());
696+
}
692697
PApplet parent = Applet.getInstance();
693698
float costumeWidth = currentCostume != null
694699
? currentCostume.getWidth()

src/org/openpatch/scratch/Stage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.openpatch.scratch;
22

3+
import processing.core.PConstants;
34
import processing.core.PGraphics;
45
import processing.event.KeyEvent;
56
import processing.event.MouseEvent;
@@ -48,6 +49,7 @@ public Stage(int width, int height, boolean debug) {
4849
}
4950
Applet applet = Applet.getInstance();
5051
this.penBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer());
52+
this.penBuffer.smooth(8);
5153
this.timer = new ConcurrentHashMap<>();
5254
this.timer.put("default", new Timer());
5355
this.display = new Text(null, 0, applet.height, true, TextStyle.BOX);

src/org/openpatch/scratch/Window.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ public void switchStage(String name) {
6363
Applet.getInstance().switchStage(name);
6464
}
6565

66+
public void exit() {
67+
Applet.getInstance().exit();
68+
}
69+
6670
}

src/org/openpatch/scratch/extensions/Pen.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import org.openpatch.scratch.Stage;
66
import processing.core.PGraphics;
77

8-
import java.util.Stack;
98
import java.util.concurrent.CopyOnWriteArrayList;
9+
import java.util.ArrayList;
1010
import java.util.Iterator;
11+
import java.util.List;
1112

1213
public class Pen implements Drawable {
1314
class Point {
@@ -29,8 +30,9 @@ class Point {
2930
private Color color = new Color(0);
3031
private float transparency = 255;
3132
private float size = 1;
32-
private Stack<CopyOnWriteArrayList<Point>> pointsBuffer = new Stack<>();
33+
private List<CopyOnWriteArrayList<Point>> pointsBuffer = new ArrayList<>();
3334
private boolean down = false;
35+
private Point previousPoint = null;
3436
private Stage stage;
3537

3638
public Pen() {
@@ -45,7 +47,7 @@ public Pen(Pen p) {
4547
this.color = new Color(p.color);
4648
this.size = p.size;
4749
this.transparency = p.transparency;
48-
this.pointsBuffer = new Stack<>();
50+
this.pointsBuffer = new ArrayList<>();
4951
this.pointsBuffer.add(new CopyOnWriteArrayList<>());
5052
this.down = p.down;
5153
}
@@ -145,7 +147,7 @@ public void changeTransparency(float step) {
145147
*/
146148
public void setPosition(float x, float y) {
147149
if (this.down) {
148-
if (this.pointsBuffer.empty()) {
150+
if (this.pointsBuffer.isEmpty()) {
149151
this.pointsBuffer.add(new CopyOnWriteArrayList<>());
150152
}
151153
this.pointsBuffer.get(this.pointsBuffer.size() - 1)
@@ -181,7 +183,8 @@ public void eraseAll() {
181183
* Draw the line which the pen has drawn.
182184
*/
183185
public void draw() {
184-
if (stage == null) return;
186+
if (stage == null)
187+
return;
185188
PGraphics buffer = stage.getPenBuffer();
186189
int pointsBufferSize = this.pointsBuffer.size();
187190
if (pointsBufferSize <= 0)
@@ -195,24 +198,25 @@ public void draw() {
195198
CopyOnWriteArrayList<Point> points = pointsBufferIter.next();
196199
Iterator<Point> pointsIter = points.iterator();
197200

198-
Point previousPoint = null;
199-
int pointsSize = points.size();
200201
while (pointsIter.hasNext()) {
201202
Point point = pointsIter.next();
202-
if (pointsSize > 1 && previousPoint != null) {
203+
if (previousPoint != null) {
203204
buffer.stroke(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
204205
buffer.strokeWeight(point.size);
205206
buffer.line(previousPoint.x, previousPoint.y, point.x, point.y);
206-
} else if (pointsSize == 1) {
207+
} else if (previousPoint == null && !this.down) {
207208
buffer.stroke(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
208209
buffer.fill(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
209210
buffer.strokeWeight(point.size);
210211
buffer.circle(point.x, point.y, point.size);
211212
}
212213
previousPoint = point;
213214
}
214-
if (!this.down) {
215+
if (!this.down || pointsBufferIter.hasNext()) {
216+
previousPoint = null;
215217
pointsBufferIter.remove();
218+
} else {
219+
points.clear();
216220
}
217221
}
218222
buffer.endDraw();

0 commit comments

Comments
 (0)