diff --git a/examples/reference/SpriteBroadcast.java b/examples/reference/SpriteBroadcast.java index 422b049d..e73a6ff8 100644 --- a/examples/reference/SpriteBroadcast.java +++ b/examples/reference/SpriteBroadcast.java @@ -7,9 +7,7 @@ public SpriteBroadcast() { Stage myStage = new ReceiveStage(); myStage.add(new BroadcastSprite()); myStage.add(new ReceiveSprite()); - GifRecorder recorder = new GifRecorder( - "" + this.getClass().getName() + ".gif" - ); + GifRecorder recorder = new GifRecorder("" + this.getClass().getName() + ".gif"); recorder.start(); while (myStage.getTimer().forMillis(3000)) {} recorder.stop(); diff --git a/src/org/openpatch/scratch/Sprite.java b/src/org/openpatch/scratch/Sprite.java index 748ac951..b83b8d91 100644 --- a/src/org/openpatch/scratch/Sprite.java +++ b/src/org/openpatch/scratch/Sprite.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; - import org.openpatch.scratch.extensions.hitbox.Hitbox; import org.openpatch.scratch.extensions.math.Vector2; import org.openpatch.scratch.extensions.pen.Pen; @@ -15,7 +14,6 @@ import org.openpatch.scratch.internal.Drawable; import org.openpatch.scratch.internal.Image; import org.openpatch.scratch.internal.Sound; - import processing.core.PApplet; import processing.event.KeyEvent; import processing.event.MouseEvent; @@ -106,10 +104,9 @@ public Stage getStage() { } /** - * Add a costume to the sprite. If a costume with the received name already - * exists do nothing. + * Add a costume to the sprite. If a costume with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param imagePath a image path */ public void addCostume(String name, final String imagePath) { @@ -123,7 +120,12 @@ public void addCostume(String name, final String imagePath) { this.costumes.add(costume); } - public void addCostume(String name, final String spriteSheetPath, final int x, final int y, final int width, + public void addCostume( + String name, + final String spriteSheetPath, + final int x, + final int y, + final int width, final int height) { for (Image costume : this.costumes) { if (costume.getName().equals(name)) { @@ -161,8 +163,7 @@ public void nextCostume() { * @return a costume name */ public String getCurrentCostumeName() { - if (this.costumes.size() == 0) - return null; + if (this.costumes.size() == 0) return null; return this.costumes.get(this.currentCostume).getName(); } @@ -177,10 +178,9 @@ public int getCurrentCostumeIndex() { } /** - * Add a sound to the sprite. If a sound with the received name already exists - * do nothing. + * Add a sound to the sprite. If a sound with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param soundPath a sound path */ public void addSound(String name, final String soundPath) { @@ -271,8 +271,7 @@ public void setTint(Color c) { * @see Image#setTint(float, float, float) */ public void setTint(float r, final float g, final float b) { - if (this.costumes.size() == 0) - return; + if (this.costumes.size() == 0) return; for (Image costume : this.costumes) { costume.setTint(r, g, b); @@ -285,8 +284,7 @@ public void setTint(float r, final float g, final float b) { * @see Image#setTint(float) */ public void setTint(float h) { - if (this.costumes.size() == 0) - return; + if (this.costumes.size() == 0) return; for (Image costume : this.costumes) { costume.setTint(h); @@ -299,8 +297,7 @@ public void setTint(float h) { * @see Image#changeTint(float) */ public void changeTint(float step) { - if (this.costumes.size() == 0) - return; + if (this.costumes.size() == 0) return; for (Image costume : this.costumes) { costume.changeTint(step); @@ -318,8 +315,7 @@ public void changeTint(double step) { * @param transparency 0 full transparency, 255 no transparency */ public void setTransparency(float transparency) { - if (this.costumes.size() == 0) - return; + if (this.costumes.size() == 0) return; for (Image costume : this.costumes) { costume.setTransparency(transparency); @@ -332,8 +328,7 @@ public void setTransparency(float transparency) { * @see Image#changeTransparency(float) */ public void changeTransparency(float step) { - if (this.costumes.size() == 0) - return; + if (this.costumes.size() == 0) return; for (Image costume : this.costumes) { costume.changeTransparency(step); @@ -402,8 +397,7 @@ public void changeSize(double amount) { } /** - * Sets if the sprite should bounce when hitting the edge of the screen. This - * method is for making + * Sets if the sprite should bounce when hitting the edge of the screen. This method is for making * is attribute perment. * * @param b @@ -486,8 +480,7 @@ public void turnRight(float degrees) { } /** - * Sets the direction of the sprite to a given degrees. When this value is 0 the - * sprite move + * Sets the direction of the sprite to a given degrees. When this value is 0 the sprite move * right, when it is 180 is moves to the left. * * @param degrees between 0 and 360 @@ -584,8 +577,7 @@ public void move(double steps) { } /** - * Moves the sprite in the direction of the given vector. The length of the - * vector determines how + * Moves the sprite in the direction of the given vector. The length of the vector determines how * move the sprite will move in this direction. * * @param v a vector @@ -670,27 +662,23 @@ public void changeY(double y) { } /** - * Return the width of the current costume or the pen size, when no costume is - * available. + * Return the width of the current costume or the pen size, when no costume is available. * * @return the width of the sprite */ public int getWidth() { - if (this.costumes.size() == 0) - return (int) this.getPen().getSize(); + if (this.costumes.size() == 0) return (int) this.getPen().getSize(); return this.costumes.get(this.currentCostume).getWidth(); } /** - * Return the height of the current costume or the pen size, when no costume is - * available. + * Return the height of the current costume or the pen size, when no costume is available. * * @return the height of the sprite */ public int getHeight() { - if (this.costumes.size() == 0) - return (int) this.getPen().getSize(); + if (this.costumes.size() == 0) return (int) this.getPen().getSize(); return this.costumes.get(this.currentCostume).getHeight(); } @@ -719,8 +707,7 @@ public Timer getTimer(String name) { * @param name the name of the timer */ public void addTimer(String name) { - if ("default".equals(name)) - return; + if ("default".equals(name)) return; this.timer.put(name, new Timer()); } @@ -731,8 +718,7 @@ public void addTimer(String name) { * @param name the name of the timer */ public void removeTimer(String name) { - if ("default".equals(name)) - return; + if ("default".equals(name)) return; this.timer.remove(name); } @@ -740,20 +726,17 @@ public void removeTimer(String name) { private float calculateAngleOfReflection(float angleOfIncidence, final boolean horizontalWall) { if (horizontalWall) { float angleOfReflection = 360 - angleOfIncidence; - while (angleOfReflection < 0) - angleOfReflection += 360; + while (angleOfReflection < 0) angleOfReflection += 360; return angleOfReflection; } else { float angleOfReflection = 180 - angleOfIncidence; - while (angleOfReflection < 0) - angleOfReflection += 360; + while (angleOfReflection < 0) angleOfReflection += 360; return angleOfReflection; } } /** - * Returns true is the mouse pointer is touching a non transparent area of the - * sprite. + * Returns true is the mouse pointer is touching a non transparent area of the sprite. * * @return true if touching */ @@ -764,21 +747,24 @@ public boolean isTouchingMousePointer() { float bottomRightCornerX = this.x + this.getWidth() / 2.0f; float bottomRightCornerY = this.y + this.getHeight() / 2.0f; - float[] mouse = Stage.rotateXY(this.getMouseX(), this.getMouseY(), this.x, this.y, -this.direction); + float[] mouse = + Stage.rotateXY(this.getMouseX(), this.getMouseY(), this.x, this.y, -this.direction); - boolean touching = mouse[0] > topLeftCornerX - && mouse[1] > topLeftCornerY - && mouse[0] < bottomRightCornerX - && mouse[1] < bottomRightCornerY; + boolean touching = + mouse[0] > topLeftCornerX + && mouse[1] > topLeftCornerY + && mouse[0] < bottomRightCornerX + && mouse[1] < bottomRightCornerY; if (touching) { int relativeMouseX = Math.round(mouse[0] - topLeftCornerX); int relativeMouseY = Math.round(mouse[1] - topLeftCornerY); if (this.costumes.size() > this.getCurrentCostumeIndex()) { - int color = this.costumes - .get(this.getCurrentCostumeIndex()) - .getPixel(relativeMouseX, relativeMouseY); + int color = + this.costumes + .get(this.getCurrentCostumeIndex()) + .getPixel(relativeMouseX, relativeMouseY); return Applet.getInstance().alpha(color) != 0; } } @@ -787,8 +773,7 @@ public boolean isTouchingMousePointer() { } /** - * Returns true if the rectangle which contains the image is outside of the - * stage + * Returns true if the rectangle which contains the image is outside of the stage * * @return true if outside */ @@ -803,17 +788,37 @@ public boolean isTouchingEdge() { float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); - float[] cornerTopLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, - this.direction); - float[] cornerTopRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, - this.direction); - float[] cornerBottomLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, - this.direction); - float[] cornerBottomRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, - this.y, this.direction); + float[] cornerTopLeft = + Stage.rotateXY( + this.x - spriteWidth / 2.0f, + this.y - spriteHeight / 2.0f, + this.x, + this.y, + this.direction); + float[] cornerTopRight = + Stage.rotateXY( + this.x + spriteWidth / 2.0f, + this.y - spriteHeight / 2.0f, + this.x, + this.y, + this.direction); + float[] cornerBottomLeft = + Stage.rotateXY( + this.x - spriteWidth / 2.0f, + this.y + spriteHeight / 2.0f, + this.x, + this.y, + this.direction); + float[] cornerBottomRight = + Stage.rotateXY( + this.x + spriteWidth / 2.0f, + this.y + spriteHeight / 2.0f, + this.x, + this.y, + this.direction); float[][] corners = { - cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight, + cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight, }; for (float[] corner : corners) { @@ -878,18 +883,43 @@ public Hitbox getHitbox() { if (this.hitbox != null) { this.hitbox.translateAndRotateAndResize( - this.direction, this.x, this.y, this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.size); + this.direction, + this.x, + this.y, + this.x - spriteWidth / 2.0f, + this.y - spriteHeight / 2.0f, + this.size); return this.hitbox; } - float[] cornerTopLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, - this.direction); - float[] cornerTopRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, - this.direction); - float[] cornerBottomLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, - this.direction); - float[] cornerBottomRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, - this.y, this.direction); + float[] cornerTopLeft = + Stage.rotateXY( + this.x - spriteWidth / 2.0f, + this.y - spriteHeight / 2.0f, + this.x, + this.y, + this.direction); + float[] cornerTopRight = + Stage.rotateXY( + this.x + spriteWidth / 2.0f, + this.y - spriteHeight / 2.0f, + this.x, + this.y, + this.direction); + float[] cornerBottomLeft = + Stage.rotateXY( + this.x - spriteWidth / 2.0f, + this.y + spriteHeight / 2.0f, + this.x, + this.y, + this.direction); + float[] cornerBottomRight = + Stage.rotateXY( + this.x + spriteWidth / 2.0f, + this.y + spriteHeight / 2.0f, + this.x, + this.y, + this.direction); int[] xPoints = new int[4]; int[] yPoints = new int[4]; @@ -907,8 +937,7 @@ public Hitbox getHitbox() { } public boolean isTouchingSprite(Sprite sprite) { - if (sprite == null || !sprite.show) - return false; + if (sprite == null || !sprite.show) return false; return this.getHitbox().intersects(sprite.getHitbox()); } @@ -1069,21 +1098,17 @@ public void keyEvent(KeyEvent e) { } } - public void whenKeyPressed(int keyCode) { - } + public void whenKeyPressed(int keyCode) {} - public void whenKeyReleased(int keyCode) { - } + public void whenKeyReleased(int keyCode) {} public void mouseEvent(MouseEvent e) { this.whenMouseMoved(e.getX(), e.getY()); } - public void whenMouseMoved(float x, final float y) { - } + public void whenMouseMoved(float x, final float y) {} - public void whenClicked() { - } + public void whenClicked() {} public void goToFrontLayer() { this.stage.goToFrontLayer(this); @@ -1101,8 +1126,7 @@ public void goLayersBackwards(int number) { this.stage.goLayersBackwards(this, number); } - public void whenBackdropSwitches(String name) { - } + public void whenBackdropSwitches(String name) {} public int pickRandom(int from, final int to) { if (to < from) { @@ -1142,14 +1166,11 @@ public void broadcast(String message) { } } - public void whenIReceive(String message) { - - } + public void whenIReceive(String message) {} /** Draws the sprite if it is not hidden. */ public void draw() { - if (this.stage == null) - return; + if (this.stage == null) return; this.pen.draw(); if (this.costumes.size() > 0 && this.show) { this.costumes @@ -1165,6 +1186,5 @@ public void draw() { this.run(); } - public void run() { - } + public void run() {} } diff --git a/src/org/openpatch/scratch/Stage.java b/src/org/openpatch/scratch/Stage.java index 4e783d12..7b287bce 100644 --- a/src/org/openpatch/scratch/Stage.java +++ b/src/org/openpatch/scratch/Stage.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; - import org.openpatch.scratch.extensions.text.Text; import org.openpatch.scratch.extensions.text.TextStyle; import org.openpatch.scratch.extensions.timer.Timer; @@ -17,7 +16,6 @@ import org.openpatch.scratch.internal.Drawable; import org.openpatch.scratch.internal.Image; import org.openpatch.scratch.internal.Sound; - import processing.core.PGraphics; import processing.event.KeyEvent; import processing.event.MouseEvent; @@ -93,11 +91,9 @@ public void add(Drawable drawable) { public void goLayersBackwards(Drawable drawable, final int number) { int index = this.drawables.indexOf(drawable); - if (index == -1) - return; + if (index == -1) return; int newIndex = index - number; - if (newIndex < 0) - newIndex = 0; + if (newIndex < 0) newIndex = 0; newIndex = Math.min(newIndex, this.drawables.size() - 1); this.drawables.remove(index); this.drawables.add(newIndex, drawable); @@ -105,11 +101,9 @@ public void goLayersBackwards(Drawable drawable, final int number) { public void goLayersForwards(Drawable drawable, final int number) { int index = this.drawables.indexOf(drawable); - if (index == -1) - return; + if (index == -1) return; int newIndex = index + number; - if (newIndex < 0) - newIndex = 0; + if (newIndex < 0) newIndex = 0; newIndex = Math.min(newIndex, this.drawables.size() - 1); this.drawables.remove(index); this.drawables.add(newIndex, drawable); @@ -171,10 +165,9 @@ public List find(Class c) { } /** - * Add a backdrop to the stage. If a backdrop with the received name already - * exists do nothing. + * Add a backdrop to the stage. If a backdrop with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param imagePath a image path */ public void addBackdrop(String name, final String imagePath) { @@ -232,8 +225,7 @@ private void emitBackdropSwitch() { this.whenBackdropSwitches(name); } - public void whenBackdropSwitches(String name) { - } + public void whenBackdropSwitches(String name) {} /** Switch to the next backdrop. */ public void nextBackdrop() { @@ -281,10 +273,9 @@ public void eraseAll() { } /** - * Add a sound to the stage. If a sound with the received name already exists do - * nothing. + * Add a sound to the stage. If a sound with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param soundPath a sound path */ public void addSound(String name, final String soundPath) { @@ -414,8 +405,7 @@ public void changeColor(double h) { * @see Image#setTint(float, float, float) */ public void setTint(int r, final int g, final int b) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).setTint(r, g, b); } @@ -425,8 +415,7 @@ public void setTint(int r, final int g, final int b) { * @see Image#setTint(float) */ public void setTint(float h) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).setTint(h); } @@ -436,8 +425,7 @@ public void setTint(float h) { * @see Image#changeTint(float) */ public void changeTint(float step) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).changeTint(step); } @@ -461,16 +449,14 @@ public void setTransparency(double transparency) { * @see Image#changeTransparency(float) */ public void changeTransparency(float step) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).changeTransparency(step); } /** - * @deprecated since v3.2.0: Use Window.getInstance().getWidth() instead Return - * the width of the - * current costume or the pen size, when no costume is available. + * @deprecated since v3.2.0: Use Window.getInstance().getWidth() instead Return the width of the + * current costume or the pen size, when no costume is available. * @return the width of the sprite */ public int getWidth() { @@ -478,9 +464,8 @@ public int getWidth() { } /** - * @deprecated since v3.2.0: Use Window.getInstance().getHeight() instead Return - * the height of the - * current costume or the pen size, when no costume is available. + * @deprecated since v3.2.0: Use Window.getInstance().getHeight() instead Return the height of the + * current costume or the pen size, when no costume is available. * @return the height of the sprite */ public int getHeight() { @@ -512,8 +497,7 @@ public Timer getTimer(String name) { * @param name the name of the timer */ public void addTimer(String name) { - if ("default".equals(name)) - return; + if ("default".equals(name)) return; this.timer.put(name, new Timer()); } @@ -524,8 +508,7 @@ public void addTimer(String name) { * @param name the name of the timer */ public void removeTimer(String name) { - if ("default".equals(name)) - return; + if ("default".equals(name)) return; this.timer.remove(name); } @@ -577,8 +560,7 @@ public boolean isMouseDown() { return this.mouseDown; } - public void whenKeyPressed(int keyCode) { - } + public void whenKeyPressed(int keyCode) {} public void keyEvent(KeyEvent e) { switch (e.getAction()) { @@ -723,15 +705,12 @@ public void broadcast(String message) { }); } - public void whenIReceive(String message) { - - } + public void whenIReceive(String message) {} /** Draws the current backdrop or if none a solid color */ public void pre() { Applet applet = Applet.getInstance(); - if (applet == null) - return; + if (applet == null) return; // redraw background to clear screen applet.background(this.color.getRed(), this.color.getGreen(), this.color.getBlue()); @@ -761,13 +740,11 @@ public void wait(int millis) { } } - public void run() { - } + public void run() {} public void draw() { Applet applet = Applet.getInstance(); - if (applet == null) - return; + if (applet == null) return; for (Drawable d : this.drawables) { d.draw(); } @@ -787,7 +764,8 @@ public void draw() { this.run(); } - public static float[] rotateXY(float x, float y, final float originX, final float originY, final float degrees) { + public static float[] rotateXY( + float x, float y, final float originX, final float originY, final float degrees) { float[] rotatedXY = new float[2]; double radians = degrees * Math.PI / 180.0; diff --git a/src/org/openpatch/scratch/Window.java b/src/org/openpatch/scratch/Window.java index b0a94a65..cc2e2e56 100644 --- a/src/org/openpatch/scratch/Window.java +++ b/src/org/openpatch/scratch/Window.java @@ -4,7 +4,7 @@ public class Window { - public static final int[] DEBUG_COLOR = { 255, 0, 0 }; + public static final int[] DEBUG_COLOR = {255, 0, 0}; private static Window instance; public Window() { diff --git a/src/org/openpatch/scratch/extensions/animation/AnimatedSprite.java b/src/org/openpatch/scratch/extensions/animation/AnimatedSprite.java index d6ae76be..8dfa4e9b 100644 --- a/src/org/openpatch/scratch/extensions/animation/AnimatedSprite.java +++ b/src/org/openpatch/scratch/extensions/animation/AnimatedSprite.java @@ -39,8 +39,8 @@ public void addAnimation(String name, String path, int frames, int width, int he animations.put(name, animation); } - public void addAnimation(String name, String path, int frames, int width, int height, int column, - boolean useColumns) { + public void addAnimation( + String name, String path, int frames, int width, int height, int column, boolean useColumns) { String[] animation = new String[frames]; for (int row = 0; row < animation.length; row++) { String costumeName = "_animation_" + name + "_" + column + "_" + row; diff --git a/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java b/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java index 76bf0844..88391b58 100644 --- a/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java +++ b/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java @@ -5,8 +5,7 @@ import java.io.*; /** - * Class AnimatedGifEncoder - Encodes a GIF file consisting of one or more - * frames. + * Class AnimatedGifEncoder - Encodes a GIF file consisting of one or more frames. * *
  *  Example:
@@ -18,10 +17,8 @@
  *     e.finish();
  * 
* - * No copyright asserted on the source code of this class. May be used for any - * purpose, however, - * refer to the Unisys LZW patent for restrictions on use of the associated - * LZWEncoder class. Please + * No copyright asserted on the source code of this class. May be used for any purpose, however, + * refer to the Unisys LZW patent for restrictions on use of the associated LZWEncoder class. Please * forward any corrections to kweiner@fmsware.com. * * @author Kevin Weiner, FM Software @@ -70,8 +67,7 @@ public class AnimatedGifEncoder { protected int sample = 10; // default sample interval for quantizer /** - * Sets the delay time between each frame, or changes it for subsequent frames - * (applies to last + * Sets the delay time between each frame, or changes it for subsequent frames (applies to last * frame added). * * @param ms int delay time in milliseconds @@ -85,8 +81,7 @@ public OutputStream getOut() { } /** - * Sets the GIF frame disposal code for the last added frame and any subsequent - * frames. Default is + * Sets the GIF frame disposal code for the last added frame and any subsequent frames. Default is * 0 if no transparent color has been set, otherwise 2. * * @param code int disposal code. @@ -98,8 +93,7 @@ public void setDispose(int code) { } /** - * Sets the number of times the set of GIF frames should be played. Default is - * 1; 0 means play + * Sets the number of times the set of GIF frames should be played. Default is 1; 0 means play * indefinitely. Must be invoked before the first image is added. * * @param iter int number of iterations. @@ -112,12 +106,9 @@ public void setRepeat(int iter) { } /** - * Sets the transparent color for the last added frame and any subsequent - * frames. Since all colors - * are subject to modification in the quantization process, the color in the - * final palette for - * each frame closest to the given color becomes the transparent color for that - * frame. May be set + * Sets the transparent color for the last added frame and any subsequent frames. Since all colors + * are subject to modification in the quantization process, the color in the final palette for + * each frame closest to the given color becomes the transparent color for that frame. May be set * to null to indicate no transparent color. * * @param c Color to be treated as transparent on display. @@ -139,12 +130,9 @@ public boolean addFrame(int[] pixels, int width, int height) { } /** - * Adds next GIF frame. The frame is not written immediately, but is actually - * deferred until the - * next frame is received so that timing data can be inserted. Invoking - * finish() - * flushes all frames. If setSize was not invoked, the size of the - * first image is + * Adds next GIF frame. The frame is not written immediately, but is actually deferred until the + * next frame is received so that timing data can be inserted. Invoking finish() + * flushes all frames. If setSize was not invoked, the size of the first image is * used for all subsequent frames. * * @param im BufferedImage containing frame to write. @@ -186,13 +174,11 @@ public boolean addFrame(BufferedImage im) { } /** - * Flushes any pending data and closes output file. If writing to an - * OutputStream, the stream is + * Flushes any pending data and closes output file. If writing to an OutputStream, the stream is * not closed. */ public boolean finish() { - if (!started) - return false; + if (!started) return false; boolean ok = true; started = false; try { @@ -219,8 +205,7 @@ public boolean finish() { } /** - * Sets frame rate in frames per second. Equivalent to - * setDelay(1000/fps). + * Sets frame rate in frames per second. Equivalent to setDelay(1000/fps). * * @param fps float frame rate (frames per second) */ @@ -231,53 +216,43 @@ public void setFrameRate(float fps) { } /** - * Sets quality of color quantization (conversion of images to the maximum 256 - * colors allowed by - * the GIF specification). Lower values (minimum = 1) produce better colors, but - * slow processing - * significantly. 10 is the default, and produces good color mapping at - * reasonable speeds. Values + * Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by + * the GIF specification). Lower values (minimum = 1) produce better colors, but slow processing + * significantly. 10 is the default, and produces good color mapping at reasonable speeds. Values * greater than 20 do not yield significant improvements in speed. * * @param quality int greater than 0. * @return */ public void setQuality(int quality) { - if (quality < 1) - quality = 1; + if (quality < 1) quality = 1; sample = quality; } /** - * Sets the GIF frame size. The default size is the size of the first frame - * added if this method + * Sets the GIF frame size. The default size is the size of the first frame added if this method * is not invoked. * * @param w int frame width. * @param h int frame width. */ public void setSize(int w, int h) { - if (started && !firstFrame) - return; + if (started && !firstFrame) return; width = w; height = h; - if (width < 1) - width = 320; - if (height < 1) - height = 240; + if (width < 1) width = 320; + if (height < 1) height = 240; sizeSet = true; } /** - * Initiates GIF file creation on the given stream. The stream is not closed - * automatically. + * Initiates GIF file creation on the given stream. The stream is not closed automatically. * * @param os OutputStream on which GIF images are written. * @return false if initial write failed. */ public boolean start(OutputStream os) { - if (os == null) - return false; + if (os == null) return false; boolean ok = true; closeStream = false; out = os; @@ -340,15 +315,14 @@ protected void analyzePixels() { /** Returns index of palette color closest to c */ protected int findClosest(Color c) { - if (colorTab == null) - return -1; + if (colorTab == null) return -1; int r = (int) c.getRed(); int g = (int) c.getGreen(); int b = (int) c.getBlue(); int minpos = 0; int dmin = 256 * 256 * 256; int len = colorTab.length; - for (int i = 0; i < len;) { + for (int i = 0; i < len; ) { int dr = r - (colorTab[i++] & 0xff); int dg = g - (colorTab[i++] & 0xff); int db = b - (colorTab[i] & 0xff); @@ -646,8 +620,7 @@ public NeuQuant(byte[] thepic, int len, int sample) { public byte[] colorMap() { byte[] map = new byte[3 * netsize]; int[] index = new int[netsize]; - for (int i = 0; i < netsize; i++) - index[network[i][3]] = i; + for (int i = 0; i < netsize; i++) index[network[i][3]] = i; int k = 0; for (int i = 0; i < netsize; i++) { int j = index[i]; @@ -705,15 +678,13 @@ public void inxbuild() { /* smallval entry is now in position i */ if (smallval != previouscol) { netindex[previouscol] = (startpos + i) >> 1; - for (j = previouscol + 1; j < smallval; j++) - netindex[j] = i; + for (j = previouscol + 1; j < smallval; j++) netindex[j] = i; previouscol = smallval; startpos = i; } } netindex[previouscol] = (startpos + maxnetpos) >> 1; - for (j = previouscol + 1; j < 256; j++) - netindex[j] = maxnetpos; /* really 256 */ + for (j = previouscol + 1; j < 256; j++) netindex[j] = maxnetpos; /* really 256 */ } /* @@ -726,8 +697,7 @@ public void learn() { byte[] p; int pix, lim; - if (lengthcount < minpicturebytes) - samplefac = 1; + if (lengthcount < minpicturebytes) samplefac = 1; alphadec = 30 + ((samplefac - 1) / 3); p = thepicture; pix = 0; @@ -738,25 +708,18 @@ public void learn() { radius = initradius; rad = radius >> radiusbiasshift; - if (rad <= 1) - rad = 0; - for (i = 0; i < rad; i++) - radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad)); + if (rad <= 1) rad = 0; + for (i = 0; i < rad; i++) radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad)); // fprintf(stderr,"beginning 1D learning: initial radius=%d\n", rad); - if (lengthcount < minpicturebytes) - step = 3; - else if ((lengthcount % prime1) != 0) - step = 3 * prime1; + if (lengthcount < minpicturebytes) step = 3; + else if ((lengthcount % prime1) != 0) step = 3 * prime1; else { - if ((lengthcount % prime2) != 0) - step = 3 * prime2; + if ((lengthcount % prime2) != 0) step = 3 * prime2; else { - if ((lengthcount % prime3) != 0) - step = 3 * prime3; - else - step = 3 * prime4; + if ((lengthcount % prime3) != 0) step = 3 * prime3; + else step = 3 * prime4; } } @@ -768,22 +731,18 @@ else if ((lengthcount % prime1) != 0) j = contest(b, g, r); altersingle(alpha, j, b, g, r); - if (rad != 0) - alterneigh(rad, j, b, g, r); /* alter neighbours */ + if (rad != 0) alterneigh(rad, j, b, g, r); /* alter neighbours */ pix += step; - if (pix >= lim) - pix -= lengthcount; + if (pix >= lim) pix -= lengthcount; i++; - if (delta == 0) - delta = 1; + if (delta == 0) delta = 1; if (i % delta == 0) { alpha -= alpha / alphadec; radius -= radius / radiusdec; rad = radius >> radiusbiasshift; - if (rad <= 1) - rad = 0; + if (rad <= 1) rad = 0; for (j = 0; j < rad; j++) radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad)); } @@ -812,20 +771,16 @@ public int map(int b, int g, int r) { if (i < netsize) { p = network[i]; dist = p[1] - g; /* inx key */ - if (dist >= bestd) - i = netsize; /* stop iter */ + if (dist >= bestd) i = netsize; /* stop iter */ else { i++; - if (dist < 0) - dist = -dist; + if (dist < 0) dist = -dist; a = p[0] - b; - if (a < 0) - a = -a; + if (a < 0) a = -a; dist += a; if (dist < bestd) { a = p[2] - r; - if (a < 0) - a = -a; + if (a < 0) a = -a; dist += a; if (dist < bestd) { bestd = dist; @@ -837,20 +792,16 @@ public int map(int b, int g, int r) { if (j >= 0) { p = network[j]; dist = g - p[1]; /* inx key - reverse dif */ - if (dist >= bestd) - j = -1; /* stop iter */ + if (dist >= bestd) j = -1; /* stop iter */ else { j--; - if (dist < 0) - dist = -dist; + if (dist < 0) dist = -dist; a = p[0] - b; - if (a < 0) - a = -a; + if (a < 0) a = -a; dist += a; if (dist < bestd) { a = p[2] - r; - if (a < 0) - a = -a; + if (a < 0) a = -a; dist += a; if (dist < bestd) { bestd = dist; @@ -900,11 +851,9 @@ protected void alterneigh(int rad, int i, int b, int g, int r) { int[] p; lo = i - rad; - if (lo < -1) - lo = -1; + if (lo < -1) lo = -1; hi = i + rad; - if (hi > netsize) - hi = netsize; + if (hi > netsize) hi = netsize; j = i + 1; k = i - 1; @@ -967,15 +916,12 @@ protected int contest(int b, int g, int r) { for (i = 0; i < netsize; i++) { n = network[i]; dist = n[0] - b; - if (dist < 0) - dist = -dist; + if (dist < 0) dist = -dist; a = n[1] - g; - if (a < 0) - a = -a; + if (a < 0) a = -a; dist += a; a = n[2] - r; - if (a < 0) - a = -a; + if (a < 0) a = -a; dist += a; if (dist < bestd) { bestd = dist; @@ -1094,8 +1040,8 @@ class LZWEncoder { int cur_bits = 0; int masks[] = { - 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, - 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF + 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, + 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; // Number of characters so far in this 'packet' @@ -1116,8 +1062,7 @@ class LZWEncoder { // characters, flush the packet to disk. void char_out(byte c, OutputStream outs) throws IOException { accum[a_count++] = c; - if (a_count >= 254) - flush_char(outs); + if (a_count >= 254) flush_char(outs); } // Clear out the hash table @@ -1133,8 +1078,7 @@ void cl_block(OutputStream outs) throws IOException { // reset code table void cl_hash(int hsize) { - for (int i = 0; i < hsize; ++i) - htab[i] = -1; + for (int i = 0; i < hsize; ++i) htab[i] = -1; } void compress(int init_bits, OutputStream outs) throws IOException { @@ -1163,8 +1107,7 @@ void compress(int init_bits, OutputStream outs) throws IOException { ent = nextPixel(); hshift = 0; - for (fcode = hsize; fcode < 65536; fcode *= 2) - ++hshift; + for (fcode = hsize; fcode < 65536; fcode *= 2) ++hshift; hshift = 8 - hshift; // set hash code range bound hsize_reg = hsize; @@ -1172,7 +1115,8 @@ void compress(int init_bits, OutputStream outs) throws IOException { output(ClearCode, outs); - outer_loop: while ((c = nextPixel()) != EOF) { + outer_loop: + while ((c = nextPixel()) != EOF) { fcode = (c << maxbits) + ent; i = (c << hshift) ^ ent; // xor hashing @@ -1182,11 +1126,9 @@ void compress(int init_bits, OutputStream outs) throws IOException { } else if (htab[i] >= 0) // non-empty slot { disp = hsize_reg - i; // secondary hash (after G. Knott) - if (i == 0) - disp = 1; + if (i == 0) disp = 1; do { - if ((i -= disp) < 0) - i += hsize_reg; + if ((i -= disp) < 0) i += hsize_reg; if (htab[i] == fcode) { ent = codetab[i]; @@ -1199,8 +1141,7 @@ void compress(int init_bits, OutputStream outs) throws IOException { if (free_ent < maxmaxcode) { codetab[i] = free_ent++; // code -> hashtable htab[i] = fcode; - } else - cl_block(outs); + } else cl_block(outs); } // Put out the final code. output(ent, outs); @@ -1236,8 +1177,7 @@ int MAXCODE(int n_bits) { // Return the next pixel from the image // ---------------------------------------------------------------------------- private int nextPixel() { - if (remaining == 0) - return EOF; + if (remaining == 0) return EOF; --remaining; @@ -1249,10 +1189,8 @@ private int nextPixel() { void output(int code, OutputStream outs) throws IOException { cur_accum &= masks[cur_bits]; - if (cur_bits > 0) - cur_accum |= (code << cur_bits); - else - cur_accum = code; + if (cur_bits > 0) cur_accum |= (code << cur_bits); + else cur_accum = code; cur_bits += n_bits; @@ -1270,10 +1208,8 @@ void output(int code, OutputStream outs) throws IOException { clear_flg = false; } else { ++n_bits; - if (n_bits == maxbits) - maxcode = maxmaxcode; - else - maxcode = MAXCODE(n_bits); + if (n_bits == maxbits) maxcode = maxmaxcode; + else maxcode = MAXCODE(n_bits); } } diff --git a/src/org/openpatch/scratch/internal/Applet.java b/src/org/openpatch/scratch/internal/Applet.java index 18a7bffe..a25186a0 100644 --- a/src/org/openpatch/scratch/internal/Applet.java +++ b/src/org/openpatch/scratch/internal/Applet.java @@ -6,10 +6,8 @@ import java.nio.file.Path; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; - import org.openpatch.scratch.KeyCode; import org.openpatch.scratch.Stage; - import processing.core.PApplet; import processing.core.PConstants; import processing.core.PImage; @@ -145,8 +143,7 @@ public void redrawSketch() { public void runSketch() { if (!this.isRunning) { super.runSketch(); - while (this.surface.isStopped()) { - } + while (this.surface.isStopped()) {} this.isRunning = true; } } @@ -157,7 +154,8 @@ public void setup() { this.rectMode(PConstants.CENTER); this.loading = this.loadImage("loading.png"); var loadingScaleX = this.INITIAL_WIDTH / 480.0; - var loadingScaleY = this.INITIAL_HEIGHT / (360.0 + 150); // normal height + padding for loading text + var loadingScaleY = + this.INITIAL_HEIGHT / (360.0 + 150); // normal height + padding for loading text var scale = Math.min(1, Math.min(loadingScaleX, loadingScaleY)); this.loading.resize((int) (this.loading.width * scale), (int) (this.loading.height * scale)); } @@ -177,27 +175,21 @@ public void loadAssets() { try { this.loadingText = "Finding files..."; var p = Path.of(ClassLoader.getSystemResource(this.assets).toURI()); - var imageFiles = Files.find( - p, - Integer.MAX_VALUE, - (filePath, fileAttr) -> fileAttr.isRegularFile()) - .map(f -> f.toString()) - .filter(f -> f.endsWith(".png") || f.endsWith(".jpg") || f.endsWith(".jpeg")) - .collect(Collectors.toList()); - var soundFiles = Files.find( - p, - Integer.MAX_VALUE, - (filePath, fileAttr) -> fileAttr.isRegularFile()) - .map(f -> f.toString()) - .filter(f -> f.endsWith(".mp3") || f.endsWith(".wav")) - .collect(Collectors.toList()); - var fontFiles = Files.find( - p, - Integer.MAX_VALUE, - (filePath, fileAttr) -> fileAttr.isRegularFile()) - .map(f -> f.toString()) - .filter(f -> f.endsWith(".ttf") || f.endsWith(".otf")) - .collect(Collectors.toList()); + var imageFiles = + Files.find(p, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) + .map(f -> f.toString()) + .filter(f -> f.endsWith(".png") || f.endsWith(".jpg") || f.endsWith(".jpeg")) + .collect(Collectors.toList()); + var soundFiles = + Files.find(p, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) + .map(f -> f.toString()) + .filter(f -> f.endsWith(".mp3") || f.endsWith(".wav")) + .collect(Collectors.toList()); + var fontFiles = + Files.find(p, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) + .map(f -> f.toString()) + .filter(f -> f.endsWith(".ttf") || f.endsWith(".otf")) + .collect(Collectors.toList()); this.numberAssets += imageFiles.size(); this.numberAssets += soundFiles.size(); this.numberAssets += fontFiles.size(); @@ -261,11 +253,12 @@ public void draw() { this.stroke(0xf58219); this.textSize(20); this.textSize(14); - this.text(this.loadingText, this.width / 2, this.height / 2 + - this.loading.height / 2 + 20); + this.text(this.loadingText, this.width / 2, this.height / 2 + this.loading.height / 2 + 20); this.textSize(20); - this.text(round(this.loadingStatus() * 100) + "%", this.width / 2, this.height / 2 + - this.loading.height / 2 + 40); + this.text( + round(this.loadingStatus() * 100) + "%", + this.width / 2, + this.height / 2 + this.loading.height / 2 + 40); this.textSize(14); } else if (sizeStages > 0) { if (this.currentStage > sizeStages - 1) {