diff --git a/README.md b/README.md index 2be3fd6..80165a3 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,35 @@ - # PaintingCanvas +## Todo + +I would like to redo the drawable system a little bit - adding support for textures and gradient fills. +In addition, adding interfaces like `Colorable`, `Resizable`, `Rotatable`, etc. to avoid useless methods would be nice. +Also - anchors would be cool. + ## What is this? - -This is a complete overhaul to the old graphics library used in our AP comp science class. It adds more shapes, more features, a *much* more robust and capable animation system, and more! + +This is a complete overhaul to the old graphics library used in our AP comp science class. It adds more shapes, more +features, a *much* more robust and capable animation system, and more! ## Features The default font is comic sans (important feature) (you're welcome). -This is a relatively capable library, and it has most of the features you would expect out of a canvas. You can draw shapes, rotate them, and move them around. If you would like a more complete overview of what this library is capable of, check out the [javadoc](https://aspiringlich.github.io/PaintingCanvas/paintingcanvas/package-summary.html). +This is a relatively capable library, and it has most of the features you would expect out of a canvas. You can draw +shapes, rotate them, and move them around. If you would like a more complete overview of what this library is capable +of, check out the [javadoc]. ## Simplicity First! -This library is designed to be as simple as possible, while still being somewhat robust. It's made to be simple enough for absolute beginners to use, but powerful and extensible enough to satisfy all the biggest nerds out there. +This library is designed to be as simple as possible, while still being somewhat robust. It's made to be simple enough +for absolute beginners to use, but powerful and extensible enough to satisfy all the biggest nerds out there. So don't complain about how the rendering runs completely asynchronously from the main thread. ## Examples -You can find various examples in `src/test/java/examples/`. These are all runnable, and they should give you a good idea of how to use the library, and give good *examples* (haha get it) of the library's capabilities. +You can find various examples in `src/test/java/examples/`. These are all runnable, and they should give you a good idea +of how to use the library, and give good *examples* (haha get it) of the library's capabilities. ### Animation Test @@ -28,3 +38,5 @@ https://user-images.githubusercontent.com/48413902/223738287-396ffca5-8bcd-48a8- ### Easing Demo https://user-images.githubusercontent.com/48413902/224180302-c79f398b-5772-42b3-9aa5-6139518b5d22.mp4 + +[javadoc]: https://aspiringlich.github.io/PaintingCanvas/paintingcanvas/package-summary.html \ No newline at end of file diff --git a/build.gradle b/build.gradle index 583f6ff..03496bd 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { // what should the group-id be breon group 'paintingcanvas' -version '1.4.1' +version '1.4.2' repositories {} dependencies {} diff --git a/src/main/java/paintingcanvas/drawable/Drawable.java b/src/main/java/paintingcanvas/drawable/Drawable.java index b9ac6fd..0720916 100644 --- a/src/main/java/paintingcanvas/drawable/Drawable.java +++ b/src/main/java/paintingcanvas/drawable/Drawable.java @@ -152,7 +152,9 @@ public T removeOutline() { * @param g Graphics context * @return The object's center-point */ - public abstract Point center(Graphics g); + public Point center(Graphics g) { + return new Point(this.x, this.y); + } /** * Actually render the object itself diff --git a/src/main/java/paintingcanvas/drawable/Ellipse.java b/src/main/java/paintingcanvas/drawable/Ellipse.java index 85b1bdf..07fc61c 100644 --- a/src/main/java/paintingcanvas/drawable/Ellipse.java +++ b/src/main/java/paintingcanvas/drawable/Ellipse.java @@ -66,11 +66,6 @@ protected void drawFilled(Graphics2D gc) { gc.fillOval(x - width / 2, y - height / 2, width, height); } - @Override - public Point center(Graphics g) { - return new Point(x, y); - } - @Override protected Ellipse getThis() { return this; diff --git a/src/main/java/paintingcanvas/drawable/Image.java b/src/main/java/paintingcanvas/drawable/Image.java new file mode 100644 index 0000000..1eba6e4 --- /dev/null +++ b/src/main/java/paintingcanvas/drawable/Image.java @@ -0,0 +1,78 @@ +package paintingcanvas.drawable; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +/** + *

+ * Draws an image from the specified path. + *

+ *

+ * {@code setColor} and {@code getColor} don't do anything for this drawable as + * it is not a traditional solid color shape + *

+ * + *
{@code
+ * // get the working directory of the program / where it is looking for your image
+ * System.out.println(System.getProperty("user.dir"));
+ * // create a new image object
+ * Image img = new Image(100, 100, "path/to/image.png");
+ * }
+ */ +public class Image extends Drawable { + BufferedImage image; + int width, height; + + /** + * Create a new Image element. + * @param x The X-position of the image + * @param y The Y-position of the image + * @param src The path to the image file + */ + public Image(int x, int y, String src) { + super(x, y, Color.BLACK); + try { + image = ImageIO.read(new File(src)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + width = image.getWidth(); + height = image.getHeight(); + } + + /** + * Set the scale of the image to the specified multiplier. + * + *
{@code
+     * Image img = new Image(0, 0, "image.png");
+     * img.setScale(2.0, 1.0); // the image is now stretched in the x direction
+     * }
+ * @param x The multiplier to scale x by + * @param y The multiplier to scale y by + * @return The original object to allow method chaining + */ + public Image setScale(double x, double y) { + this.width = (int)(image.getWidth() * x); + this.height = (int)(image.getHeight() * y); + return this; + } + + @Override + protected void drawFilled(Graphics2D gc) { + gc.drawImage(image, x - width / 2, y - height / 2, width, height, null); + } + + @Override + protected void drawOutline(Graphics2D gc) { + gc.drawRect(x - width / 2, y - height / 2, width, height); + } + + @Override + protected Image getThis() { + return this; + } +} diff --git a/src/main/java/paintingcanvas/drawable/Rectangle.java b/src/main/java/paintingcanvas/drawable/Rectangle.java index 7d07411..4a99a69 100644 --- a/src/main/java/paintingcanvas/drawable/Rectangle.java +++ b/src/main/java/paintingcanvas/drawable/Rectangle.java @@ -67,11 +67,6 @@ protected void drawFilled(Graphics2D gc) { gc.fillRect(x - width / 2, y - height / 2, width, height); } - @Override - public Point center(Graphics g) { - return new Point(x, y); - } - @Override protected Rectangle getThis() { return this; diff --git a/src/main/java/paintingcanvas/drawable/Square.java b/src/main/java/paintingcanvas/drawable/Square.java index a7d2a1c..494f74b 100644 --- a/src/main/java/paintingcanvas/drawable/Square.java +++ b/src/main/java/paintingcanvas/drawable/Square.java @@ -58,11 +58,6 @@ protected void drawOutline(Graphics2D gc) { gc.drawRect(x - size / 2, y - size / 2, size, size); } - @Override - public Point center(Graphics g) { - return new Point(x, y); - } - @Override public Square getThis() { return this; diff --git a/src/main/java/paintingcanvas/drawable/Triangle.java b/src/main/java/paintingcanvas/drawable/Triangle.java index 618ec10..aca7cc5 100644 --- a/src/main/java/paintingcanvas/drawable/Triangle.java +++ b/src/main/java/paintingcanvas/drawable/Triangle.java @@ -120,11 +120,6 @@ protected void drawOutline(Graphics2D gc) { gc.drawPolygon(this.getPolygon()); } - @Override - public Point center(Graphics g) { - return new Point(x, y); - } - @Override protected Triangle getThis() { return this; diff --git a/src/test/java/examples/Test.java b/src/test/java/examples/Test.java index e1e25d6..da05776 100644 --- a/src/test/java/examples/Test.java +++ b/src/test/java/examples/Test.java @@ -1,12 +1,14 @@ package examples; import paintingcanvas.canvas.Canvas; +import paintingcanvas.drawable.Image; import paintingcanvas.drawable.Line; import paintingcanvas.drawable.Polygon; import paintingcanvas.extensions.FrameCounter; import paintingcanvas.extensions.InfoDisplay; -import java.awt.*; +import java.awt.Color; + @SuppressWarnings("unused") public class Test { @@ -16,26 +18,11 @@ static Color randomColor() { public static void main(String[] args) { Canvas canvas = new Canvas(); - new FrameCounter().line( - () -> String.format("Animations: %d", canvas.animations.size()) - ).attach(); - new InfoDisplay().attach(); - Line l = new Line(0, 0, 100, 100, Color.RED).setColor(Color.blue); - Polygon[] shapes = new Polygon[10]; - for (int i = 0; i < shapes.length; i++) { - shapes[i] = new Polygon(0, 200, randomColor()); - for (int j = 0; j < 5; j++) { - int x = (int) (Math.random() * 200); - int y = (int) (Math.random() * 200); - shapes[i].point(x, y); - } - } - while (true) { - for (int i = 0; i < 20; i++) { - for (Polygon c : shapes) c.move(10, 0); - for (Polygon c : shapes) c.move(-10, 0); - } - } + String dir = System.getProperty("user.dir"); + Image image = new Image(0, 0, "src/test/java/examples/flop.jpg") + .rotate(90); + + while(true) {} } } diff --git a/src/test/java/examples/flop.jpg b/src/test/java/examples/flop.jpg new file mode 100644 index 0000000..6d8d8d7 Binary files /dev/null and b/src/test/java/examples/flop.jpg differ diff --git a/todo.md b/todo.md deleted file mode 100644 index beab6c5..0000000 --- a/todo.md +++ /dev/null @@ -1,8 +0,0 @@ -# ToDo - - - [ ] Clean up documentation - - [ ] Rewrite wiki documentation (yaaay) - - [x] Resize thingy - - [ ] Make it based on real time (?????) - - [ ] remove `TimeUnit`? - - [ ] `setThickness` `setOutline` shenanigans \ No newline at end of file