From 3b4ffbeba17ac249c401a7df25f81a8b34fd67f7 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:23:13 +0100 Subject: [PATCH 01/25] add cleaned canvas class with javadoc commments --- .../simplegraphics/graphics/Canvas.java | 258 +++++++++++------- 1 file changed, 161 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java index d1d5ed3..7b2e03b 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java @@ -9,180 +9,244 @@ import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; -import java.awt.image.ImageObserver; import java.awt.image.RescaleOp; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Iterator; /** - * Container of shapes + * Singleton Canvas class responsible for managing and displaying graphical shapes. */ public class Canvas { private static final int MIN_SIZE = 100; private static final int MARGIN = 10; private static final int LOCATION_OFFSET = 120; + private static int MAX_X = 0; private static int MAX_Y = 0; - private static Canvas canvas = new Canvas(); - private ArrayList shapes = new ArrayList(); + private static Canvas instance = new Canvas(); + + private ArrayList shapes = new ArrayList<>(); private BufferedImage background; - private JFrame frame = new JFrame(); - private Canvas.CanvasComponent component = new Canvas.CanvasComponent(); + private JFrame frame; + private CanvasComponent component; /** - * Defines a limit to the width of the canvas - * @param var0 + * Private constructor to enforce Singleton pattern. */ - public static void setMaxX(int var0) { - MAX_X = var0; + private Canvas() { + component = new CanvasComponent(); + + frame = new JFrame(); + frame.add(component); + frame.pack(); + frame.setLocation(LOCATION_OFFSET, LOCATION_OFFSET); + frame.setVisible(true); } /** - * Defines a limit to the height of the canvas - * @param var0 + * Sets the maximum width of the canvas. + * @param width maximum width */ - public static void setMaxY(int var0) { - MAX_Y = var0; + public static void setMaxX(int width) { + MAX_X = width; } - private Canvas() { - this.frame.add(this.component); - this.frame.pack(); - this.frame.setLocation(LOCATION_OFFSET, LOCATION_OFFSET); - this.frame.setVisible(true); + /** + * Sets the maximum height of the canvas. + * @param height maximum height + */ + public static void setMaxY(int height) { + MAX_Y = height; } + /** + * Returns the singleton instance of the Canvas. + * @return Canvas instance + */ public static Canvas getInstance() { - return canvas; + return instance; } + + /** + * Pauses so that the user can see the picture before it is transformed. + */ public static void pause() { - JFrame var0 = getInstance().frame; - if (var0 != null) { - JOptionPane.showMessageDialog(var0, "Click Ok to continue"); + JFrame frame = getInstance().frame; + + if (frame != null) { + return; } + + JOptionPane.showMessageDialog(frame, "Click Ok to continue"); } + /** + * Takes a snapshot of the screen, fades it, and sets it as the background. + */ public static void snapshot() { - Dimension var0 = getInstance().component.getPreferredSize(); - java.awt.Rectangle var1 = new java.awt.Rectangle(0, 0, var0.width, var0.height); - BufferedImage var2 = new BufferedImage(var1.width, var1.height, 1); - Graphics var3 = var2.getGraphics(); - var3.setColor(java.awt.Color.WHITE); - var3.fillRect(0, 0, var1.width, var1.height); - var3.setColor(java.awt.Color.BLACK); - getInstance().component.paintComponent(var3); - float var4 = 0.8F; - float var5 = 255.0F * (1.0F - var4); - RescaleOp var6 = new RescaleOp(var4, var5, (RenderingHints)null); - BufferedImage var7 = new BufferedImage(var2.getWidth(), var2.getHeight(), var2.getType()); - var6.filter(var2, var7); - getInstance().background = var7; + Dimension dim = getInstance().component.getPreferredSize(); + Rectangle rect = new Rectangle(0, 0, dim.width, dim.height); + BufferedImage image = new BufferedImage(rect.width, rect.height, 1); + + Graphics g = image.getGraphics(); + g.setColor(Color.WHITE); + g.fillRect(0, 0, rect.width, rect.height); + g.setColor(Color.BLACK); + getInstance().component.paintComponent(g); + + float factor = 0.8f; + float base = 255f * (1f - factor); + RescaleOp op = new RescaleOp(factor, base, null); + BufferedImage filteredImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); + op.filter(image, filteredImage); + getInstance().background = filteredImage; getInstance().component.repaint(); } - public void show(Shape var1) { - if (!this.shapes.contains(var1)) { - this.shapes.add(var1); + /** + * Displays the given shape on the canvas. + * @param shape the shape to display + */ + public void show(Shape shape) { + if (!shapes.contains(shape)) { + shapes.add(shape); } - this.repaint(); + repaint(); } - public void hide(Shape var1) { - if (this.shapes.contains(var1)) { - this.shapes.remove(var1); + /** + * Hides the given shape from the canvas. + * @param shape the shape to hide + */ + public void hide(Shape shape) { + if (shapes.contains(shape)) { + shapes.remove(shape); } - this.repaint(); + repaint(); } + /** + * Repaints the canvas if needed. + */ public void repaint() { - if (this.frame != null) { - Dimension var1 = this.component.getPreferredSize(); - if (var1.getWidth() <= (double)this.component.getWidth() && var1.getHeight() <= (double)this.component.getHeight()) { - this.frame.repaint(); - } else { - this.frame.pack(); - } + if (frame != null) { + return; + } + + Dimension dim = component.getPreferredSize(); + if (dim.getWidth() <= (double) component.getWidth() && dim.getHeight() <= (double) component.getHeight()) { + frame.repaint(); + } else { + frame.pack(); } } - public void saveToDisk(String var1) { - Dimension var2 = this.component.getPreferredSize(); - java.awt.Rectangle var3 = new Rectangle(0, 0, var2.width, var2.height); - BufferedImage var4 = new BufferedImage(var3.width, var3.height, 1); - Graphics2D var5 = (Graphics2D)var4.getGraphics(); - var5.setColor(java.awt.Color.WHITE); - var5.fill(var3); - var5.setColor(java.awt.Color.BLACK); - this.component.paintComponent(var5); - String var6 = var1.substring(var1.lastIndexOf(46) + 1); + /** + * Saves the current canvas view to disk as an image. + * @param fileName the path and fileName to save to + */ + public void saveToDisk(String fileName) { + Dimension dim = component.getPreferredSize(); + Rectangle rect = new Rectangle(0, 0, dim.width, dim.height); + BufferedImage image = new BufferedImage(rect.width, rect.height, 1); + + Graphics2D g = (Graphics2D) image.getGraphics(); + g.setColor(Color.WHITE); + g.fill(rect); + g.setColor(Color.BLACK); + component.paintComponent(g); + + String extension = fileName.substring(fileName.lastIndexOf('.') + 1); try { - ImageIO.write(var4, var6, new File(var1)); - } catch (IOException var8) { - System.err.println("Was unable to save the image to " + var1); + ImageIO.write(image, extension, new File(fileName)); + + } catch (IOException e) { + System.err.println("Was unable to save the image to " + fileName); } - var5.dispose(); + g.dispose(); } - public void addKeyListener(KeyListener var1) { - this.frame.addKeyListener(var1); + /** + * Adds a KeyListener to the canvas. + * @param handler the KeyListener to add + */ + public void addKeyListener(KeyListener handler) { + frame.addKeyListener(handler); } - public void addMouseListener(MouseListener var1) { - this.frame.addMouseListener(var1); + /** + * Adds a MouseListener to the canvas. + * @param handler the MouseListener to add + */ + public void addMouseListener(MouseListener handler) { + frame.addMouseListener(handler); } - public void addMouseMotionListener(MouseMotionListener var1) { - this.frame.addMouseMotionListener(var1); + /** + * Adds a MouseMotionListener to the canvas. + * @param handler the MouseMotionListener to add + */ + public void addMouseMotionListener(MouseMotionListener handler) { + frame.addMouseMotionListener(handler); } + /** + * Inner component class responsible for custom painting of the canvas. + */ class CanvasComponent extends JComponent { CanvasComponent() { } - public void paintComponent(Graphics var1) { - var1.setColor(java.awt.Color.WHITE); - var1.fillRect(0, 0, this.getWidth(), this.getHeight()); - var1.setColor(Color.BLACK); - if (Canvas.this.background != null) { - var1.drawImage(Canvas.this.background, 0, 0, (ImageObserver)null); + /** + * Paints the component by drawing the background and all visible shapes. + * @param g the Graphics context in which to paint + */ + public void paintComponent(Graphics g) { + g.setColor(Color.WHITE); + g.fillRect(0, 0, getWidth(), getHeight()); + g.setColor(Color.BLACK); + + if (background != null) { + g.drawImage(background, 0, 0, null); } - Iterator var2 = (new ArrayList(Canvas.this.shapes)).iterator(); - - while(var2.hasNext()) { - Shape var3 = (Shape)var2.next(); - Graphics2D var4 = (Graphics2D)var1.create(); - var3.paintShape(var4); - var4.dispose(); + for (Shape s : new ArrayList<>(shapes)) { + Graphics2D g2D = (Graphics2D) g.create(); + s.paintShape(g2D); + g2D.dispose(); } - } + /** + * Calculates the preferred size of the canvas based on the shapes and background image. + * @return the preferred Dimension of the canvas + */ public Dimension getPreferredSize() { - int var1 = Canvas.MAX_X == 0 ? MIN_SIZE : Canvas.MAX_X; - int var2 = Canvas.MAX_Y == 0 ? MIN_SIZE : Canvas.MAX_Y; - if (Canvas.this.background != null) { - var1 = Math.max(var1, Canvas.this.background.getWidth()); - var2 = Math.max(var1, Canvas.this.background.getHeight()); + int width = Canvas.MAX_X == 0 ? MIN_SIZE : Canvas.MAX_X; + int height = Canvas.MAX_Y == 0 ? MIN_SIZE : Canvas.MAX_Y; + + if (background != null) { + width = Math.max(width, background.getWidth()); + height = Math.max(width, background.getHeight()); } - Shape var4; if (Canvas.MAX_X == 0 && Canvas.MAX_Y == 0) { - for(Iterator var3 = Canvas.this.shapes.iterator(); var3.hasNext(); var2 = Math.max(var2, var4.getY() + var4.getHeight())) { - var4 = (Shape)var3.next(); - var1 = Math.max(var1, var4.getX() + var4.getWidth()); + + for(Shape s : shapes) { + width = Math.max(width, s.getX() + s.getWidth()); + height = Math.max(height, s.getY() + s.getHeight()); } + } - return new Dimension(var1 + MARGIN, var2 + MARGIN); + return new Dimension(width + MARGIN, height + MARGIN); } } } \ No newline at end of file From a485f3d103a648e744c2af6aeb7cae760b940f2a Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:25:18 +0100 Subject: [PATCH 02/25] mild clean on Color class --- .../online/simplegraphics/graphics/Color.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java index 9f8c275..9bce047 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java @@ -27,8 +27,7 @@ public class Color { * @param green the green value of the color (between 0 and 255) * @param blue the blue value of the color (between 0 and 255) */ - public Color(int red, int green, int blue) - { + public Color(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; @@ -38,8 +37,7 @@ public Color(int red, int green, int blue) * Gets the red value of this color. * @return the red value (between 0 and 255) */ - public int getRed() - { + public int getRed() { return red; } @@ -47,8 +45,7 @@ public int getRed() * Gets the green value of this color. * @return the green value (between 0 and 255) */ - public int getGreen() - { + public int getGreen() { return green; } @@ -56,8 +53,7 @@ public int getGreen() * Gets the blue value of this color. * @return the blue value (between 0 and 255) */ - public int getBlue() - { + public int getBlue() { return blue; } } From c69ee1c59b9dcac424acfb98753254fa01b711ac Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:27:00 +0100 Subject: [PATCH 03/25] reformat on canvas and color classes --- .../simplegraphics/graphics/Canvas.java | 13 ++++++++- .../online/simplegraphics/graphics/Color.java | 27 ++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java index 7b2e03b..a36ec2a 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java @@ -46,6 +46,7 @@ private Canvas() { /** * Sets the maximum width of the canvas. + * * @param width maximum width */ public static void setMaxX(int width) { @@ -54,6 +55,7 @@ public static void setMaxX(int width) { /** * Sets the maximum height of the canvas. + * * @param height maximum height */ public static void setMaxY(int height) { @@ -62,6 +64,7 @@ public static void setMaxY(int height) { /** * Returns the singleton instance of the Canvas. + * * @return Canvas instance */ public static Canvas getInstance() { @@ -107,6 +110,7 @@ public static void snapshot() { /** * Displays the given shape on the canvas. + * * @param shape the shape to display */ public void show(Shape shape) { @@ -119,6 +123,7 @@ public void show(Shape shape) { /** * Hides the given shape from the canvas. + * * @param shape the shape to hide */ public void hide(Shape shape) { @@ -148,6 +153,7 @@ public void repaint() { /** * Saves the current canvas view to disk as an image. + * * @param fileName the path and fileName to save to */ public void saveToDisk(String fileName) { @@ -175,6 +181,7 @@ public void saveToDisk(String fileName) { /** * Adds a KeyListener to the canvas. + * * @param handler the KeyListener to add */ public void addKeyListener(KeyListener handler) { @@ -183,6 +190,7 @@ public void addKeyListener(KeyListener handler) { /** * Adds a MouseListener to the canvas. + * * @param handler the MouseListener to add */ public void addMouseListener(MouseListener handler) { @@ -191,6 +199,7 @@ public void addMouseListener(MouseListener handler) { /** * Adds a MouseMotionListener to the canvas. + * * @param handler the MouseMotionListener to add */ public void addMouseMotionListener(MouseMotionListener handler) { @@ -206,6 +215,7 @@ class CanvasComponent extends JComponent { /** * Paints the component by drawing the background and all visible shapes. + * * @param g the Graphics context in which to paint */ public void paintComponent(Graphics g) { @@ -226,6 +236,7 @@ public void paintComponent(Graphics g) { /** * Calculates the preferred size of the canvas based on the shapes and background image. + * * @return the preferred Dimension of the canvas */ public Dimension getPreferredSize() { @@ -239,7 +250,7 @@ public Dimension getPreferredSize() { if (Canvas.MAX_X == 0 && Canvas.MAX_Y == 0) { - for(Shape s : shapes) { + for (Shape s : shapes) { width = Math.max(width, s.getX() + s.getWidth()); height = Math.max(height, s.getY() + s.getHeight()); } diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java index 9bce047..c89437d 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java @@ -1,15 +1,11 @@ package com.codeforall.online.simplegraphics.graphics; public class Color { - private int red; - private int green; - private int blue; - - // Color constants - public static final Color RED = new Color(255, 0, 0); public static final Color GREEN = new Color(0, 255, 0); public static final Color BLUE = new Color(0, 0, 255); + + // Color constants public static final Color WHITE = new Color(255, 255, 255); public static final Color LIGHT_GRAY = new Color(192, 192, 192); public static final Color GRAY = new Color(128, 128, 128); @@ -20,12 +16,16 @@ public class Color { public static final Color YELLOW = new Color(255, 255, 0); public static final Color PINK = new Color(255, 175, 175); public static final Color ORANGE = new Color(255, 200, 0); - + private int red; + private int green; + private int blue; + /** * Constructs a new Color object. - * @param red the red value of the color (between 0 and 255) + * + * @param red the red value of the color (between 0 and 255) * @param green the green value of the color (between 0 and 255) - * @param blue the blue value of the color (between 0 and 255) + * @param blue the blue value of the color (between 0 and 255) */ public Color(int red, int green, int blue) { this.red = red; @@ -35,25 +35,28 @@ public Color(int red, int green, int blue) { /** * Gets the red value of this color. + * * @return the red value (between 0 and 255) */ public int getRed() { - return red; + return red; } /** * Gets the green value of this color. + * * @return the green value (between 0 and 255) */ public int getGreen() { - return green; + return green; } /** * Gets the blue value of this color. + * * @return the blue value (between 0 and 255) */ public int getBlue() { - return blue; + return blue; } } From 35f0a39895a2e5a4bf7f8cbc096f493725e6ac6a Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:29:17 +0100 Subject: [PATCH 04/25] add javadoc comment on top of the color class --- .../codeforall/online/simplegraphics/graphics/Color.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java index c89437d..0207b9d 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java @@ -1,5 +1,13 @@ package com.codeforall.online.simplegraphics.graphics; +/** + * Represents a simple RGB color definition used for graphical shapes. + * This class provides predefined constants for commonly used colors + * and allows the creation of custom colors using red, green, and blue components. + * Values for red, green, and blue should be between 0 and 255. + * Note: This class is distinct from {@code java.awt.Color} + * and is intended for use within the simplegraphics library. + */ public class Color { public static final Color RED = new Color(255, 0, 0); public static final Color GREEN = new Color(0, 255, 0); From e5c1cd295a940fd0913e213b82e92e9f7d9d7ebe Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:35:08 +0100 Subject: [PATCH 05/25] add javadoc comments that were missing on ellipse class and reformat code on ellipse, canvas and color class --- .../simplegraphics/graphics/Canvas.java | 1 + .../online/simplegraphics/graphics/Color.java | 1 + .../simplegraphics/graphics/Ellipse.java | 35 ++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java index a36ec2a..68f02a9 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java @@ -18,6 +18,7 @@ * Singleton Canvas class responsible for managing and displaying graphical shapes. */ public class Canvas { + private static final int MIN_SIZE = 100; private static final int MARGIN = 10; private static final int LOCATION_OFFSET = 120; diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java index 0207b9d..fd568f4 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java @@ -9,6 +9,7 @@ * and is intended for use within the simplegraphics library. */ public class Color { + public static final Color RED = new Color(255, 0, 0); public static final Color GREEN = new Color(0, 255, 0); public static final Color BLUE = new Color(0, 0, 255); diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java index e058ef2..231185e 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java @@ -3,7 +3,21 @@ import java.awt.*; import java.awt.geom.Ellipse2D; +/** + * Represents an ellipse shape that can be drawn, filled, moved, and resized. + * The ellipse is defined by its bounding box, which determines its x/y position, width, and height. + * This class integrates with the {@link Canvas} to visually render itself. + *

+ * Implements: + *

    + *
  • {@link Shape} – for rendering and bounding box info
  • + *
  • {@link Colorable} – for setting its color
  • + *
  • {@link Fillable} – for supporting fill operations
  • + *
  • {@link Movable} – for translation and scaling
  • + *
+ */ public class Ellipse implements Shape, Colorable, Fillable, Movable { + private Color color = Color.BLACK; private boolean filled = false; private double x; @@ -126,17 +140,30 @@ public void fill() { Canvas.getInstance().show(this); } + /** + * Returns a string representation of this ellipse. + * + * @return a string describing the ellipse's position and dimensions + */ public String toString() { return "Ellipse[x=" + getX() + ",y=" + getY() + ",width=" + getWidth() + ",height=" + getHeight() + "]"; } - public void paintShape(Graphics2D g2) { + /** + * Paints this ellipse using the given Graphics2D context. + * This is called by the canvas to render the shape. + * + * @param g2D the Graphics2D context to draw with + */ + public void paintShape(Graphics2D g2D) { Ellipse2D.Double ellipse = new Ellipse2D.Double(getX(), getY(), getWidth(), getHeight()); - g2.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); + g2D.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); + if (filled) { - g2.fill(ellipse); + g2D.fill(ellipse); + } else { - g2.draw(ellipse); + g2D.draw(ellipse); } } } From dbf64aebc767c43bbbae633df1391dd064b953ed Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:36:22 +0100 Subject: [PATCH 06/25] small fix on fillable interface --- .../com/codeforall/online/simplegraphics/graphics/Fillable.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java index f59c6b6..ea4ddf2 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java @@ -9,5 +9,4 @@ public interface Fillable { * Paints the shape with the current shape color */ void fill(); - } From 02673c0e4e36e0ba36bd6fa1c1248df325ff3d42 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:42:45 +0100 Subject: [PATCH 07/25] fixes on Line class --- .../online/simplegraphics/graphics/Line.java | 99 ++++++++++++------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java index 75ad6e5..8344353 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java @@ -3,26 +3,39 @@ import java.awt.*; import java.awt.geom.Line2D; +/** + * Represents a straight line defined by two endpoints. + * The line can be drawn, translated, resized, or deleted from a canvas. + * It supports color customization and integrates with the {@link Canvas} for rendering. + * + * Implements: + *
    + *
  • {@link Shape} – provides geometric and canvas-renderable properties
  • + *
  • {@link Colorable} – allows setting the drawing color
  • + *
  • {@link Movable} – supports translation and resizing via grow
  • + *
+ */ public class Line implements Shape, Colorable, Movable { + private Color color = Color.BLACK; - private double x1; - private double y1; - private double x2; - private double y2; + private double startX; + private double startY; + private double endX; + private double endY; /** * Constructs a line with a given starting and ending location. * - * @param x1 the x-coordinate of the starting point - * @param y1 the y-coordinate of the starting point - * @param x2 the x-coordinate of the ending point - * @param y2 the y-coordinate of the ending point + * @param startX the x-coordinate of the starting point + * @param startY the y-coordinate of the starting point + * @param endX the x-coordinate of the ending point + * @param endY the y-coordinate of the ending point */ - public Line(double x1, double y1, double x2, double y2) { - this.x1 = x1; - this.x2 = x2; - this.y1 = y1; - this.y2 = y2; + public Line(double startX, double startY, double endX, double endY) { + this.startX = startX; + this.endX = endX; + this.startY = startY; + this.endY = endY; } /** @@ -31,7 +44,7 @@ public Line(double x1, double y1, double x2, double y2) { * @return the leftmost x-position */ public int getX() { - return (int) Math.round(Math.min(x1, x2)); + return (int) Math.round(Math.min(startX, endX)); } /** @@ -40,7 +53,7 @@ public int getX() { * @return the topmost y-position */ public int getY() { - return (int) Math.round(Math.min(y1, y2)); + return (int) Math.round(Math.min(startY, endY)); } /** @@ -49,7 +62,7 @@ public int getY() { * @return the width */ public int getWidth() { - return (int) Math.round(Math.abs(x2 - x1)); + return (int) Math.round(Math.abs(endX - startX)); } /** @@ -58,7 +71,7 @@ public int getWidth() { * @return the height */ public int getHeight() { - return (int) Math.round(Math.abs(y2 - y1)); + return (int) Math.round(Math.abs(endY - startY)); } /** @@ -68,10 +81,10 @@ public int getHeight() { * @param dy the amount by which to move in y-direction */ public void translate(double dx, double dy) { - x1 += dx; - y1 += dy; - x2 += dx; - y2 += dy; + startX += dx; + startY += dy; + endX += dx; + endY += dy; Canvas.getInstance().repaint(); } @@ -82,19 +95,23 @@ public void translate(double dx, double dy) { * @param dh the amount by which to resize the height on each side */ public void grow(double dw, double dh) { - if (x1 <= x2) { - x1 -= dw; - x2 += dw; + + if (startX <= endX) { + startX -= dw; + endX += dw; + } else { - x1 += dw; - x2 -= dw; + startX += dw; + endX -= dw; } - if (y1 <= y2) { - y1 -= dh; - y2 += dh; + + if (startY <= endY) { + startY -= dh; + endY += dh; + } else { - y1 += dh; - y2 -= dh; + startY += dh; + endY -= dh; } Canvas.getInstance().repaint(); @@ -124,15 +141,25 @@ public void delete() { Canvas.getInstance().hide(this); } + /** + * Returns a string representation of the line, including both endpoints. + * + * @return a string describing the line's coordinates + */ public String toString() { - return "Line[x1=" + x1 + ",y1=" + y1 + ",x2=" + x2 + ",y2=" + y2 + "]"; + return "Line[startX=" + startX + ",startY=" + startY + ",endX=" + endX + ",endY=" + endY + "]"; } - public void paintShape(Graphics2D g2) { + /** + * Paints the line using the specified Graphics2D context. + * + * @param g2D the graphics context to paint with + */ + public void paintShape(Graphics2D g2D) { if (color != null) { - g2.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); - Line2D.Double line = new Line2D.Double(x1, y1, x2, y2); - g2.draw(line); + g2D.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); + Line2D.Double line = new Line2D.Double(startX, startY, endX, endY); + g2D.draw(line); } } } From 64b27e67b2701cb380013675bb01362a05183106 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:43:54 +0100 Subject: [PATCH 08/25] minor fix on movable interface --- .../com/codeforall/online/simplegraphics/graphics/Movable.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java index d90e9d7..3e88dd9 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java @@ -11,6 +11,5 @@ public interface Movable { * @param dx the amount by which to move in x-direction * @param dy the amount by which to move in y-direction */ - public void translate(double dx, double dy); - + void translate(double dx, double dy); } From bbaeac6d0809d6ee43d9a270015e1d705f5f895a Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 11:54:26 +0100 Subject: [PATCH 09/25] fixes on rectangle class and small fix on ellipse class --- .../simplegraphics/graphics/Ellipse.java | 2 +- .../simplegraphics/graphics/Rectangle.java | 36 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java index 231185e..1103ad6 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java @@ -7,7 +7,7 @@ * Represents an ellipse shape that can be drawn, filled, moved, and resized. * The ellipse is defined by its bounding box, which determines its x/y position, width, and height. * This class integrates with the {@link Canvas} to visually render itself. - *

+ * * Implements: *

    *
  • {@link Shape} – for rendering and bounding box info
  • diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java index 6fd4c43..59dbb70 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java @@ -3,7 +3,24 @@ import java.awt.*; import java.awt.geom.Rectangle2D; +/** + * The {@code Rectangle} class represents a drawable and optionally filled rectangle + * that can be displayed on a canvas. This class allows manipulation of the rectangle's + * size, position, and color, and provides basic drawing and transformation capabilities. + * + * Implements: + *
      + *
    • {@link Shape} – for rendering and bounding box info
    • + *
    • {@link Colorable} – for setting its color
    • + *
    • {@link Fillable} – for supporting fill operations
    • + *
    • {@link Movable} – for translation and scaling
    • + *
    + * + * Note: This class is distinct from {@code java.awt.Rectangle} + * and is intended for use within the simplegraphics library. + */ public class Rectangle implements Shape, Colorable, Fillable, Movable { + private Color color = Color.BLACK; private boolean filled = false; private double x; @@ -139,22 +156,31 @@ public void fill() { Canvas.getInstance().show(this); } + /** + * Returns a string representation of the rectangle, including both endpoints. + * + * @return a string describing the rectangle's coordinates + */ @Override public String toString() { return "Rectangle[x=" + getX() + ",y=" + getY() + ",width=" + getWidth() + ",height=" + getHeight() + "]"; } + /** + * Paints the line using the specified Graphics2D context. + * + * @param g2D the graphics context to paint with + */ @Override - public void paintShape(Graphics2D g2) { + public void paintShape(Graphics2D g2D) { Rectangle2D.Double rect = new Rectangle2D.Double(getX(), getY(), getWidth(), getHeight()); - g2.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); + g2D.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); if (filled) { - - g2.fill(rect); + g2D.fill(rect); } else { - g2.draw(rect); + g2D.draw(rect); } } } From 54f5748be6bdb7b42a50970c54338b70bbdf9e82 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 12:01:45 +0100 Subject: [PATCH 10/25] add javadoc comments on text class and small fixes on shape interface and rectangle class --- .../simplegraphics/graphics/Rectangle.java | 2 +- .../online/simplegraphics/graphics/Shape.java | 6 ++-- .../online/simplegraphics/graphics/Text.java | 36 ++++++++++++++++--- .../simplegraphics/pictures/Picture.java | 10 +++--- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java index 59dbb70..d3b3b5d 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java @@ -167,7 +167,7 @@ public String toString() { } /** - * Paints the line using the specified Graphics2D context. + * Paints the rectangle using the specified Graphics2D context. * * @param g2D the graphics context to paint with */ diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java index f1a8589..51ad7cb 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java @@ -6,6 +6,7 @@ * A shape that can be drawn on a canvas. */ public interface Shape { + /** * Gets the leftmost x-position of the shape. * @@ -55,8 +56,7 @@ public interface Shape { /** * Paints the shape * - * @param g2 the graphics object + * @param g2D the graphics object */ - void paintShape(Graphics2D g2); - + void paintShape(Graphics2D g2D); } diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java index ce74b9a..8549827 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java @@ -3,7 +3,23 @@ import javax.swing.*; import java.awt.*; +/** + * The {@code Text} class represents a drawable text label that can be displayed + * on a canvas. It allows setting the text content, changing its position, resizing + * the bounding box, and customizing its color. + * The text is rendered using a {@link JLabel} and drawn via a {@link Graphics2D} + * context. + * + * Implements: + *
      + *
    • {@link Shape} – for rendering and bounding box info
    • + *
    • {@link Colorable} – for setting its color
    • + *
    • {@link Movable} – for translation and scaling
    • + *
    + * + */ public class Text implements Shape, Colorable, Movable { + private Color color = Color.BLACK; private JLabel label = new JLabel(); private double x; @@ -119,19 +135,31 @@ public void delete() { Canvas.getInstance().hide(this); } + /** + * Returns a string representation of the text, including both endpoints and the content. + * + * @return a string describing the text's coordinates and content + */ public String toString() { return "Text[x=" + getX() + ",y=" + getY() + ",message=" + label.getText() + "]"; } - public void paintShape(Graphics2D g2) { + /** + * Paints the text using the specified Graphics2D context. + * + * @param g2D the graphics context to paint with + */ + public void paintShape(Graphics2D g2D) { + if (color != null) { label.setForeground(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); Dimension dim = label.getPreferredSize(); + if (dim.width > 0 && dim.height > 0) { label.setBounds(0, 0, dim.width, dim.height); - g2.translate(getX(), getY()); - g2.scale(getWidth() / dim.width, getHeight() / dim.height); - label.paint(g2); + g2D.translate(getX(), getY()); + g2D.scale(getWidth() / dim.width, getHeight() / dim.height); + label.paint(g2D); } } } diff --git a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java b/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java index cf0ca42..3a40a40 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java +++ b/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java @@ -291,17 +291,17 @@ public void delete() { /** * Draws this shape. * - * @param g2 the graphics context + * @param g2D the graphics context */ - public void paintShape(Graphics2D g2) { + public void paintShape(Graphics2D g2D) { if (image != null) { Dimension dim = label.getPreferredSize(); if (dim.width > 0 && dim.height > 0) { label.setBounds(0, 0, dim.width, dim.height); - g2.translate(getX(), getY()); - g2.scale((image.getWidth() + 2 * xGrow) / dim.width, + g2D.translate(getX(), getY()); + g2D.scale((image.getWidth() + 2 * xGrow) / dim.width, (image.getHeight() + 2 * yGrow) / dim.height); - label.paint(g2); + label.paint(g2D); } } } From ad591e3e9885931efd9c2997f9f4c8144175c3dd Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 12:09:46 +0100 Subject: [PATCH 11/25] add @override annotations where needed --- .../online/simplegraphics/graphics/Ellipse.java | 8 ++++++++ .../online/simplegraphics/graphics/Line.java | 11 +++++++++++ .../online/simplegraphics/graphics/Rectangle.java | 1 + .../online/simplegraphics/graphics/Text.java | 2 ++ 4 files changed, 22 insertions(+) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java index 1103ad6..55b41f2 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java @@ -87,6 +87,7 @@ public int getHeight() { * @param dx the amount by which to move in x-direction * @param dy the amount by which to move in y-direction */ + @Override public void translate(double dx, double dy) { x += dx; y += dy; @@ -99,6 +100,7 @@ public void translate(double dx, double dy) { * @param dw the amount by which to resize the width on each side * @param dh the amount by which to resize the height on each side */ + @Override public void grow(double dw, double dh) { width += 2 * dw; height += 2 * dh; @@ -112,6 +114,7 @@ public void grow(double dw, double dh) { * * @param newColor the new color */ + @Override public void setColor(Color newColor) { color = newColor; Canvas.getInstance().repaint(); @@ -120,6 +123,7 @@ public void setColor(Color newColor) { /** * Draws this ellipse on the canvas. */ + @Override public void draw() { filled = false; Canvas.getInstance().show(this); @@ -128,6 +132,7 @@ public void draw() { /** * Deletes this ellipse from the canvas */ + @Override public void delete() { Canvas.getInstance().hide(this); } @@ -135,6 +140,7 @@ public void delete() { /** * Fills this ellipse. */ + @Override public void fill() { filled = true; Canvas.getInstance().show(this); @@ -145,6 +151,7 @@ public void fill() { * * @return a string describing the ellipse's position and dimensions */ + @Override public String toString() { return "Ellipse[x=" + getX() + ",y=" + getY() + ",width=" + getWidth() + ",height=" + getHeight() + "]"; } @@ -155,6 +162,7 @@ public String toString() { * * @param g2D the Graphics2D context to draw with */ + @Override public void paintShape(Graphics2D g2D) { Ellipse2D.Double ellipse = new Ellipse2D.Double(getX(), getY(), getWidth(), getHeight()); g2D.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java index 8344353..5eb6619 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java @@ -43,6 +43,7 @@ public Line(double startX, double startY, double endX, double endY) { * * @return the leftmost x-position */ + @Override public int getX() { return (int) Math.round(Math.min(startX, endX)); } @@ -52,6 +53,7 @@ public int getX() { * * @return the topmost y-position */ + @Override public int getY() { return (int) Math.round(Math.min(startY, endY)); } @@ -61,6 +63,7 @@ public int getY() { * * @return the width */ + @Override public int getWidth() { return (int) Math.round(Math.abs(endX - startX)); } @@ -70,6 +73,7 @@ public int getWidth() { * * @return the height */ + @Override public int getHeight() { return (int) Math.round(Math.abs(endY - startY)); } @@ -80,6 +84,7 @@ public int getHeight() { * @param dx the amount by which to move in x-direction * @param dy the amount by which to move in y-direction */ + @Override public void translate(double dx, double dy) { startX += dx; startY += dy; @@ -94,6 +99,7 @@ public void translate(double dx, double dy) { * @param dw the amount by which to resize the width on each side * @param dh the amount by which to resize the height on each side */ + @Override public void grow(double dw, double dh) { if (startX <= endX) { @@ -122,6 +128,7 @@ public void grow(double dw, double dh) { * * @param newColor the new color */ + @Override public void setColor(Color newColor) { color = newColor; Canvas.getInstance().repaint(); @@ -130,6 +137,7 @@ public void setColor(Color newColor) { /** * Shows this line on the canvas. */ + @Override public void draw() { Canvas.getInstance().show(this); } @@ -137,6 +145,7 @@ public void draw() { /** * Deletes this line from the canvas. */ + @Override public void delete() { Canvas.getInstance().hide(this); } @@ -146,6 +155,7 @@ public void delete() { * * @return a string describing the line's coordinates */ + @Override public String toString() { return "Line[startX=" + startX + ",startY=" + startY + ",endX=" + endX + ",endY=" + endY + "]"; } @@ -155,6 +165,7 @@ public String toString() { * * @param g2D the graphics context to paint with */ + @Override public void paintShape(Graphics2D g2D) { if (color != null) { g2D.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue())); diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java index d3b3b5d..e50d095 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java @@ -99,6 +99,7 @@ public int getHeight() { * @param dx the amount by which to move in x-direction * @param dy the amount by which to move in y-direction */ + @Override public void translate(double dx, double dy) { x += dx; y += dy; diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java index 8549827..a580eba 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java @@ -84,6 +84,7 @@ public int getHeight() { * @param dx the amount by which to move in x-direction * @param dy the amount by which to move in y-direction */ + @Override public void translate(double dx, double dy) { x += dx; y += dy; @@ -107,6 +108,7 @@ public void grow(double dw, double dh) { * * @param newColor the new color */ + @Override public void setColor(Color newColor) { color = newColor; Canvas.getInstance().repaint(); From d48174ed33c5be1411b7ca2a857a7bda3f05e8b3 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 13:39:58 +0100 Subject: [PATCH 12/25] minor fixes on keyboard class --- .../simplegraphics/keyboard/Keyboard.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java b/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java index 7637262..24458d0 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java +++ b/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java @@ -1,6 +1,7 @@ package com.codeforall.online.simplegraphics.keyboard; import com.codeforall.online.simplegraphics.graphics.Canvas; + import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; @@ -13,45 +14,43 @@ public class Keyboard implements KeyListener { KeyboardHandler handler; - List keyboardEventArrayList; + List keyboardEvents; - /** - * @param handler the keyboard handler - */ public Keyboard(KeyboardHandler handler) { - Canvas.getInstance().addKeyListener(this); this.handler = handler; - keyboardEventArrayList = new ArrayList(); - + keyboardEvents = new ArrayList<>(); } /** * Add a new Keyboard event listener + * * @param keyboardEvent the event to add */ public void addEventListener(KeyboardEvent keyboardEvent) { - keyboardEventArrayList.add(keyboardEvent); + keyboardEvents.add(keyboardEvent); } /** * Remove an existing Keyboard event listener + * * @param keyboardEvent the event to remove */ public void removeEventListener(KeyboardEvent keyboardEvent) { - keyboardEventArrayList.remove(keyboardEvent); + keyboardEvents.remove(keyboardEvent); } /** - * @see KeyListener#keyTyped(KeyEvent) * @param e the event + * @see KeyListener#keyTyped(KeyEvent) */ @Override public void keyTyped(KeyEvent e) { } /** + * @param e the event * @see KeyboardHandler#keyPressed(KeyboardEvent) */ @Override @@ -61,17 +60,20 @@ public void keyPressed(KeyEvent e) { return; } - Iterator iterator = keyboardEventArrayList.iterator(); + Iterator iterator = keyboardEvents.iterator(); + while (iterator.hasNext()) { - KeyboardEvent ke = iterator.next(); - if (ke.getKeyboardEventType() == KeyboardEventType.KEY_PRESSED && - ke.getKey() == e.getKeyCode()) { - handler.keyPressed(ke); + KeyboardEvent event = iterator.next(); + + if (event.getKeyboardEventType() == KeyboardEventType.KEY_PRESSED && + event.getKey() == e.getKeyCode()) { + handler.keyPressed(event); } } } /** + * @param e the event * @see KeyboardHandler#keyReleased(KeyboardEvent) */ @Override @@ -81,12 +83,14 @@ public void keyReleased(KeyEvent e) { return; } - Iterator iterator = keyboardEventArrayList.iterator(); + Iterator iterator = keyboardEvents.iterator(); + while (iterator.hasNext()) { - KeyboardEvent ke = iterator.next(); - if (ke.getKeyboardEventType() == KeyboardEventType.KEY_RELEASED && - ke.getKey() == e.getKeyCode()) { - handler.keyReleased(ke); + KeyboardEvent event = iterator.next(); + + if (event.getKeyboardEventType() == KeyboardEventType.KEY_RELEASED && + event.getKey() == e.getKeyCode()) { + handler.keyReleased(event); } } } From 8b17f4b0964523515ea21fa456f07854accc5572 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 13:42:19 +0100 Subject: [PATCH 13/25] minor fix on keyboard event type --- .../online/simplegraphics/keyboard/KeyboardEvent.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java index 0dae99f..38e8b65 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java +++ b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java @@ -74,8 +74,7 @@ public class KeyboardEvent { public static final int KEY_BACK_SLASH = KeyEvent.VK_BACK_SLASH; public static final int KEY_SHIFT = KeyEvent.VK_SHIFT; public static final int KEY_TAB = KeyEvent.VK_TAB; - - + private KeyboardEventType keyboardEventType; private int key; From 5646815c65c80fa6b693caaece7f680cb32a12a1 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 13:45:20 +0100 Subject: [PATCH 14/25] add minor fixes to classes on the keyboard package --- .../online/simplegraphics/keyboard/KeyboardEvent.java | 2 +- .../online/simplegraphics/keyboard/KeyboardEventType.java | 1 + .../online/simplegraphics/keyboard/KeyboardHandler.java | 5 ++--- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java index 38e8b65..61b7337 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java +++ b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java @@ -74,7 +74,7 @@ public class KeyboardEvent { public static final int KEY_BACK_SLASH = KeyEvent.VK_BACK_SLASH; public static final int KEY_SHIFT = KeyEvent.VK_SHIFT; public static final int KEY_TAB = KeyEvent.VK_TAB; - + private KeyboardEventType keyboardEventType; private int key; diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java index d8bf5aa..ffcec71 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java +++ b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java @@ -5,6 +5,7 @@ * @see Keyboard */ public enum KeyboardEventType { + KEY_PRESSED, KEY_RELEASED } diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java index 8ef6db5..9629ea2 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java +++ b/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java @@ -12,12 +12,11 @@ public interface KeyboardHandler { * @see java.awt.event.KeyListener#keyPressed(KeyEvent) * @param e the event */ - public void keyPressed(KeyboardEvent e); + void keyPressed(KeyboardEvent e); /** * @see java.awt.event.KeyListener#keyReleased(KeyEvent) * @param e the event */ - public void keyReleased(KeyboardEvent e); - + void keyReleased(KeyboardEvent e); } From 93a5784cee7c6110d10c230fe5c9aac1b15df882 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 13:53:46 +0100 Subject: [PATCH 15/25] change access modifiers on keyboard and mouse classes and create getters and setters for it --- .../simplegraphics/keyboard/Keyboard.java | 40 ++++++++++- .../online/simplegraphics/mouse/Mouse.java | 68 +++++++++++++++---- 2 files changed, 92 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java b/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java index 24458d0..3d3f492 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java +++ b/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java @@ -13,8 +13,8 @@ */ public class Keyboard implements KeyListener { - KeyboardHandler handler; - List keyboardEvents; + private KeyboardHandler handler; + private List keyboardEvents; public Keyboard(KeyboardHandler handler) { Canvas.getInstance().addKeyListener(this); @@ -94,4 +94,40 @@ public void keyReleased(KeyEvent e) { } } } + + /** + * Returns the current {@link KeyboardHandler} assigned to handle keyboard events. + * + * @return the keyboard handler + */ + public KeyboardHandler getHandler() { + return handler; + } + + /** + * Sets the {@link KeyboardHandler} that will be used to handle keyboard events. + * + * @param handler the keyboard handler to set + */ + public void setHandler(KeyboardHandler handler) { + this.handler = handler; + } + + /** + * Returns the list of registered {@link KeyboardEvent}s that this class listens for. + * + * @return the list of keyboard events + */ + public List getKeyboardEvents() { + return keyboardEvents; + } + + /** + * Sets the list of {@link KeyboardEvent}s that this class should listen for. + * + * @param keyboardEvents the list of keyboard events to set + */ + public void setKeyboardEvents(List keyboardEvents) { + this.keyboardEvents = keyboardEvents; + } } diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java b/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java index ed45375..be904b7 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java +++ b/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java @@ -6,36 +6,40 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import com.codeforall.online.simplegraphics.graphics.Canvas; +import com.codeforall.online.simplegraphics.graphics.Canvas; +/** + * Instantiate a Mouse for obtaining mouse handling capability + */ public class Mouse implements MouseListener, MouseMotionListener { - MouseHandler handler; - List mouseEventArrayList; + + private MouseHandler handler; + private List mouseEventTypes; public Mouse(MouseHandler var1) { Canvas.getInstance().addMouseListener(this); Canvas.getInstance().addMouseMotionListener(this); this.handler = var1; - this.mouseEventArrayList = new ArrayList(); + this.mouseEventTypes = new ArrayList(); } public void addEventListener(MouseEventType var1) { - this.mouseEventArrayList.add(var1); + this.mouseEventTypes.add(var1); } public void removeEventListener(MouseEventType var1) { - this.mouseEventArrayList.remove(var1); + this.mouseEventTypes.remove(var1); } public void mouseClicked(MouseEvent var1) { if (this.handler != null) { - Iterator var2 = this.mouseEventArrayList.iterator(); + Iterator var2 = this.mouseEventTypes.iterator(); - while(var2.hasNext()) { - MouseEventType var3 = (MouseEventType)var2.next(); + while (var2.hasNext()) { + MouseEventType var3 = (MouseEventType) var2.next(); if (var3 == MouseEventType.MOUSE_CLICKED) { - this.handler.mouseClicked(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double)var1.getX(), (double)var1.getY(), MouseEventType.MOUSE_CLICKED)); + this.handler.mouseClicked(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) var1.getX(), (double) var1.getY(), MouseEventType.MOUSE_CLICKED)); } } } @@ -44,12 +48,12 @@ public void mouseClicked(MouseEvent var1) { public void mouseMoved(MouseEvent var1) { if (this.handler != null) { - Iterator var2 = this.mouseEventArrayList.iterator(); + Iterator var2 = this.mouseEventTypes.iterator(); - while(var2.hasNext()) { - MouseEventType var3 = (MouseEventType)var2.next(); + while (var2.hasNext()) { + MouseEventType var3 = (MouseEventType) var2.next(); if (var3 == MouseEventType.MOUSE_MOVED) { - this.handler.mouseMoved(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double)var1.getX(), (double)var1.getY(), MouseEventType.MOUSE_MOVED)); + this.handler.mouseMoved(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) var1.getX(), (double) var1.getY(), MouseEventType.MOUSE_MOVED)); } } } @@ -70,4 +74,40 @@ public void mouseExited(MouseEvent var1) { public void mouseDragged(MouseEvent var1) { } + + /** + * Returns the current {@link MouseHandler} assigned to handle mouse events. + * + * @return the mouse handler + */ + public MouseHandler getHandler() { + return handler; + } + + /** + * Sets the {@link MouseHandler} that will be used to handle mouse events. + * + * @param handler the mouse handler to set + */ + public void setHandler(MouseHandler handler) { + this.handler = handler; + } + + /** + * Returns the list of registered {@link MouseEventType}s that this class listens for. + * + * @return the list of mouse event types + */ + public List getMouseEventTypes() { + return mouseEventTypes; + } + + /** + * Sets the list of {@link MouseEventType}s that this class should listen for. + * + * @param mouseEventTypes the list of mouse event types to set + */ + public void setMouseEventTypes(List mouseEventTypes) { + this.mouseEventTypes = mouseEventTypes; + } } From f10f5c695513db7eb276606deec534d98ae570f7 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 14:38:09 +0100 Subject: [PATCH 16/25] add changes to mouse package --- .../online/simplegraphics/mouse/Mouse.java | 162 +++++++++++++++--- .../simplegraphics/mouse/MouseEvent.java | 61 +++++-- .../simplegraphics/mouse/MouseEventType.java | 8 +- .../simplegraphics/mouse/MouseHandler.java | 36 +++- 4 files changed, 222 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java b/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java index be904b7..484ad21 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java +++ b/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java @@ -17,62 +17,170 @@ public class Mouse implements MouseListener, MouseMotionListener { private MouseHandler handler; private List mouseEventTypes; - public Mouse(MouseHandler var1) { + public Mouse(MouseHandler handler) { Canvas.getInstance().addMouseListener(this); Canvas.getInstance().addMouseMotionListener(this); - this.handler = var1; + + this.handler = handler; this.mouseEventTypes = new ArrayList(); } - public void addEventListener(MouseEventType var1) { - this.mouseEventTypes.add(var1); + /** + * Add a new Mouse event listener + * + * @param mouseEventType the event type to add + */ + public void addEventListener(MouseEventType mouseEventType) { + mouseEventTypes.add(mouseEventType); } - public void removeEventListener(MouseEventType var1) { - this.mouseEventTypes.remove(var1); + /** + * Remove an existing Mouse event listener + * + * @param mouseEventType the event to remove + */ + public void removeEventListener(MouseEventType mouseEventType) { + mouseEventTypes.remove(mouseEventType); } - public void mouseClicked(MouseEvent var1) { - if (this.handler != null) { - Iterator var2 = this.mouseEventTypes.iterator(); + /** + * @param e the event to be processed + * @see MouseListener#mouseClicked(MouseEvent) + */ + @Override + public void mouseClicked(MouseEvent e) { + + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); - while (var2.hasNext()) { - MouseEventType var3 = (MouseEventType) var2.next(); - if (var3 == MouseEventType.MOUSE_CLICKED) { - this.handler.mouseClicked(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) var1.getX(), (double) var1.getY(), MouseEventType.MOUSE_CLICKED)); + if (eventType == MouseEventType.MOUSE_CLICKED) { + handler.mouseClicked(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_CLICKED)); } } } - } - public void mouseMoved(MouseEvent var1) { - if (this.handler != null) { - Iterator var2 = this.mouseEventTypes.iterator(); + /** + * @param e the event to be processed + * @see MouseMotionListener#mouseMoved(MouseEvent) + */ + @Override + public void mouseMoved(MouseEvent e) { - while (var2.hasNext()) { - MouseEventType var3 = (MouseEventType) var2.next(); - if (var3 == MouseEventType.MOUSE_MOVED) { - this.handler.mouseMoved(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) var1.getX(), (double) var1.getY(), MouseEventType.MOUSE_MOVED)); + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); + + if (eventType == MouseEventType.MOUSE_MOVED) { + handler.mouseMoved(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_MOVED)); } } } - } - public void mousePressed(MouseEvent var1) { + /** + * @param e the event to be processed + * @see MouseListener#mousePressed(MouseEvent) + */ + @Override + public void mousePressed(MouseEvent e) { + + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); + + if (eventType == MouseEventType.MOUSE_PRESSED) { + handler.mousePressed(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_PRESSED)); + } + } + } } - public void mouseReleased(MouseEvent var1) { + /** + * @param e the event to be processed + * @see MouseListener#mouseReleased(MouseEvent) + */ + @Override + public void mouseReleased(MouseEvent e) { + + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); + + if (eventType == MouseEventType.MOUSE_RELEASED) { + handler.mouseReleased(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_RELEASED)); + } + } + } } - public void mouseEntered(MouseEvent var1) { + /** + * @param e the event to be processed + * @see MouseListener#mouseEntered(MouseEvent) + */ + @Override + public void mouseEntered(MouseEvent e) { + + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); + + if (eventType == MouseEventType.MOUSE_ENTERED) { + handler.mouseEntered(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_ENTERED)); + } + } + } } - public void mouseExited(MouseEvent var1) { + /** + * @param e the event to be processed + * @see MouseListener#mouseExited(MouseEvent) + */ + @Override + public void mouseExited(MouseEvent e) { + + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); + + if (eventType == MouseEventType.MOUSE_EXITED) { + handler.mouseExited(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_EXITED)); + } + } + } } - public void mouseDragged(MouseEvent var1) { + /** + * @param e the event to be processed + * @see MouseMotionListener#mouseDragged(MouseEvent) + */ + @Override + public void mouseDragged(MouseEvent e) { + + if (handler != null) { + Iterator iterator = mouseEventTypes.iterator(); + + while (iterator.hasNext()) { + MouseEventType eventType = (MouseEventType) iterator.next(); + + if (eventType == MouseEventType.MOUSE_DRAGGED) { + handler.mouseDragged(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_DRAGGED)); + } + } + } } /** diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java b/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java index 16a5d88..67e49e2 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java +++ b/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java @@ -1,37 +1,70 @@ package com.codeforall.online.simplegraphics.mouse; +/** + * Mouse event containing x and y coordinates and the mouse event type + */ public class MouseEvent { + private double x; private double y; private MouseEventType eventType; - public double getX() { - return this.x; + public MouseEvent(double x, double y, MouseEventType mouseEventType) { + this.x = x; + this.y = y; + this.eventType = mouseEventType; } - public double getY() { - return this.y; + /** + * Gets the X coordinate where the mouse clicked + * @return the x coordinate + */ + public double getX() { + return x; } - public void setX(double var1) { - this.x = var1; + /** + * Gets the Y coordinate where the mouse cliecked + * @return the y coordinate + */ + public double getY() { + return y; } - public void setY(double var1) { - this.y = var1; + /** + * Sets the x-coordinate of the mouse event. + * + * @param x the new x-coordinate + */ + public void setX(double x) { + this.x = x; } - public MouseEvent(double var1, double var3, MouseEventType var5) { - this.x = var1; - this.y = var3; - this.eventType = var5; + /** + * Sets the y-coordinate of the mouse event. + * + * @param y the new y-coordinate + */ + public void setY(double y) { + this.y = y; } + /** + * Gets the type of this mouse event. + * + * @return the {@link MouseEventType} for this event + */ public MouseEventType getEventType() { - return this.eventType; + return eventType; } + /** + * Returns a string representation of the mouse event, including coordinates and event type. + * + * @return a string describing the event + */ + @Override public String toString() { - return "MouseEvent{x=" + this.x + ", y=" + this.y + ", eventType=" + this.eventType + '}'; + return "MouseEvent{x=" + x + ", y=" + y + ", eventType=" + eventType + '}'; } } diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java b/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java index 01bd08e..e7e46fd 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java +++ b/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java @@ -1,8 +1,14 @@ package com.codeforall.online.simplegraphics.mouse; public enum MouseEventType { + MOUSE_CLICKED, - MOUSE_MOVED; + MOUSE_MOVED, + MOUSE_PRESSED, + MOUSE_RELEASED, + MOUSE_ENTERED, + MOUSE_EXITED, + MOUSE_DRAGGED; private MouseEventType() { } diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java b/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java index 83de890..957a88e 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java +++ b/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java @@ -2,20 +2,50 @@ /** * Interface to be implemented by all classes that want to receive mouse events + * * @see MouseEvent */ public interface MouseHandler { /** - * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) * @param e the event + * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) */ - public void mouseClicked(MouseEvent e); + void mouseClicked(MouseEvent e); /** + * @param e the event * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent) + */ + void mouseMoved(MouseEvent e); + + /** * @param e the event + * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) */ - public void mouseMoved(MouseEvent e); + void mousePressed(MouseEvent e); + /** + * @param e the event + * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) + */ + void mouseReleased(MouseEvent e); + + /** + * @param e the event + * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) + */ + void mouseEntered(MouseEvent e); + + /** + * @param e the event + * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) + */ + void mouseExited(MouseEvent e); + + /** + * @param e the event + * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent) + */ + void mouseDragged(MouseEvent e); } From 8e94ea8145da803a4b8821d8537af3a4a14268b7 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 14:47:51 +0100 Subject: [PATCH 17/25] fixes on the color classes --- .../online/simplegraphics/graphics/Color.java | 1 + .../online/simplegraphics/pictures/Color.java | 63 +++++++++++-------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java index fd568f4..ad912c2 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java +++ b/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java @@ -25,6 +25,7 @@ public class Color { public static final Color YELLOW = new Color(255, 255, 0); public static final Color PINK = new Color(255, 175, 175); public static final Color ORANGE = new Color(255, 200, 0); + private int red; private int green; private int blue; diff --git a/src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java b/src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java index f5eb330..fc67e28 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java +++ b/src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java @@ -1,7 +1,27 @@ package com.codeforall.online.simplegraphics.pictures; +/** + * A simple representation of an RGB color for pictures. + * This class encapsulates the red, green, and blue components of a color, + * each represented by an integer value between 0 and 255. + */ public class Color { + // Color constants + public static final Color RED = new Color(255, 0, 0); + public static final Color GREEN = new Color(0, 255, 0); + public static final Color BLUE = new Color(0, 0, 255); + public static final Color WHITE = new Color(255, 255, 255); + public static final Color LIGHT_GRAY = new Color(192, 192, 192); + public static final Color GRAY = new Color(128, 128, 128); + public static final Color DARK_GRAY = new Color(64, 64, 64); + public static final Color BLACK = new Color(0, 0, 0); + public static final Color CYAN = new Color(0, 255, 255); + public static final Color MAGENTA = new Color(255, 0, 255); + public static final Color YELLOW = new Color(255, 255, 0); + public static final Color PINK = new Color(255, 175, 175); + public static final Color ORANGE = new Color(255, 200, 0); + private int red; private int green; private int blue; @@ -20,49 +40,38 @@ public Color(int red, int green, int blue) { /** * Gets the red value of this color. + * * @return the red value (between 0 and 255) */ - public int getRed() - { - return red; + public int getRed() { + return red; } /** * Gets the green value of this color. + * * @return the green value (between 0 and 255) */ - public int getGreen() - { - return green; + public int getGreen() { + return green; } /** * Gets the blue value of this color. + * * @return the blue value (between 0 and 255) */ - public int getBlue() - { - return blue; + public int getBlue() { + return blue; } - public String toString() - { + /** + * Returns a string representation of the color. + * + * @return a string describing the RGB color + */ + @Override + public String toString() { return "Color[red=" + red + ",green=" + green + ",blue=" + blue + "]"; } - - // Color constants - - public static final Color RED = new Color(255, 0, 0); - public static final Color GREEN = new Color(0, 255, 0); - public static final Color BLUE = new Color(0, 0, 255); - public static final Color WHITE = new Color(255, 255, 255); - public static final Color LIGHT_GRAY = new Color(192, 192, 192); - public static final Color GRAY = new Color(128, 128, 128); - public static final Color DARK_GRAY = new Color(64, 64, 64); - public static final Color BLACK = new Color(0, 0, 0); - public static final Color CYAN = new Color(0, 255, 255); - public static final Color MAGENTA = new Color(255, 0, 255); - public static final Color YELLOW = new Color(255, 255, 0); - public static final Color PINK = new Color(255, 175, 175); - public static final Color ORANGE = new Color(255, 200, 0); } From 14983beeb03655d7d4c168cbc6c08f935e2686fd Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 15:05:48 +0100 Subject: [PATCH 18/25] changes to picture class --- .../simplegraphics/pictures/Picture.java | 85 +++++++++++++++++-- 1 file changed, 78 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java b/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java index 3a40a40..b692c3b 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java +++ b/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java @@ -14,18 +14,26 @@ /** * A picture from an image file. + * + * Implements: + *
      + *
    • {@link Shape} – for rendering and bounding box info
    • + *
    • {@link Movable} – for translation and scaling
    • + *
    */ public class Picture implements Shape, Movable { + private BufferedImage image; private JLabel label = new JLabel(); private String source; + private double x; private double y; private double xGrow; private double yGrow; /** - * Constructs a picture with no image. + * Constructs a picture object with no image. */ public Picture() { } @@ -39,6 +47,7 @@ public Picture() { public Picture(double width, double height) { image = new BufferedImage((int) Math.round(width), (int) Math.round(height), BufferedImage.TYPE_INT_RGB); + label.setIcon(new ImageIcon(image)); label.setText(""); } @@ -53,23 +62,46 @@ public Picture(double width, double height) { public Picture(double x, double y, String source) { this.x = x; this.y = y; + load(source); } + /** + * Constructs a picture from a 2D array of grayscale values. + * Each element in the array represents a pixel's grayscale intensity, where + * 0 is black, 255 is white, and values in between are shades of gray. + * The resulting image is built pixel by pixel using these values. + * + * @param grayLevels a 2D array representing grayscale values for the image; + * rows correspond to image height and columns to width + */ public Picture(int[][] grayLevels) { image = new BufferedImage(grayLevels[0].length, grayLevels.length, BufferedImage.TYPE_INT_RGB); - for (int i = 0; i < image.getWidth(); i++) + + for (int i = 0; i < image.getWidth(); i++) { + for (int j = 0; j < image.getHeight(); j++) { int gray = grayLevels[j][i]; - if (gray < 0) gray = 0; - if (gray > 255) gray = 255; + + if (gray < 0) { + gray = 0; + } + + if (gray > 255) { + gray = 255; + } + int rgb = gray * (65536 + 256 + 1); + image.setRGB(i, j, rgb); } + } + label.setIcon(new ImageIcon(image)); label.setText(""); } + /** * Loads a new image from a given file or URL. * @@ -81,7 +113,7 @@ public void load(String source) { this.source = source; // Load from the web - if (source.startsWith("http://")) { + if (source.startsWith("http://") || source.startsWith("https://")) { image = ImageIO.read(new URL(source).openStream()); @@ -89,8 +121,10 @@ public void load(String source) { // Attempt to load from the class path (as in JAR file..) URL url = getClass().getResource(source.startsWith("/") ? source : "/" + source); + if (url != null) { image = ImageIO.read(url.openStream()); + } else { // Load from file @@ -100,11 +134,13 @@ public void load(String source) { label.setIcon(new ImageIcon(image)); label.setText(""); + } catch (Exception ex) { image = null; label.setIcon(null); ex.printStackTrace(); } + Canvas.getInstance().repaint(); } @@ -113,6 +149,7 @@ public void load(String source) { * * @return the leftmost x-position */ + @Override public int getX() { return (int) Math.round(x - xGrow); } @@ -122,6 +159,7 @@ public int getX() { * * @return the topmost y-position */ + @Override public int getY() { return (int) Math.round(y - yGrow); } @@ -147,6 +185,7 @@ public int getMaxY() { /** * Gets the width of this picture. */ + @Override public int getWidth() { return (int) Math.round( (image == null ? 0 : image.getWidth()) + 2 * xGrow); @@ -155,6 +194,7 @@ public int getWidth() { /** * Gets the height of this picture. */ + @Override public int getHeight() { return (int) Math.round( (image == null ? 0 : image.getHeight()) + 2 * yGrow); @@ -168,24 +208,38 @@ public int getHeight() { public int pixels() { if (image == null) { return 0; + } else { return image.getWidth() * image.getHeight(); } } public int[][] getGrayLevels() { - if (image == null) return new int[0][0]; + + if (image == null) { + return new int[0][0]; + } + int[][] grayLevels = new int[getHeight()][getWidth()]; - for (int i = 0; i < grayLevels.length; i++) + for (int i = 0; i < grayLevels.length; i++) { + for (int j = 0; j < grayLevels[i].length; j++) { int rgb = image.getRGB(j, i); + // Use NTSC/PAL algorithm to convert RGB to gray level grayLevels[i][j] = (int) (0.2989 * ((rgb >> 16) & 0xFF) + 0.5866 * ((rgb >> 8) & 0xFF) + 0.1144 * (rgb & 0xFF)); } + } return grayLevels; } + /** + * Returns a string representation of the image. + * + * @return a string describing the image's coordinates, size and source + */ + @Override public String toString() { return "Picture[x=" + getX() + ",y=" + getY() + ",width=" + getWidth() + ",height=" + getHeight() + ",source=" + source + "]"; } @@ -197,8 +251,10 @@ public String toString() { * @return the color at pixel i */ public Color getColorAt(int i) { + if (image == null || i < 0 || i >= pixels()) { throw new IndexOutOfBoundsException("" + i); + } else { return getColorAt(i % image.getWidth(), i / image.getWidth()); } @@ -211,8 +267,10 @@ public Color getColorAt(int i) { * @param color the new color for the pixel */ public void setColorAt(int i, Color color) { + if (image == null || i < 0 || i >= pixels()) { throw new IndexOutOfBoundsException("" + i); + } else { setColorAt(i % image.getWidth(), i / image.getWidth(), color); } @@ -226,8 +284,10 @@ public void setColorAt(int i, Color color) { * @return the color of the pixel */ public Color getColorAt(int x, int y) { + if (image == null || x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight()) { throw new IndexOutOfBoundsException("(" + x + "," + y + ")"); + } else { int rgb = image.getRGB(x, y) & 0xFFFFFF; return new Color(rgb / 65536, (rgb / 256) % 256, rgb % 256); @@ -242,8 +302,10 @@ public Color getColorAt(int x, int y) { * @param color the color of the pixel at the given row and column */ public void setColorAt(int x, int y, Color color) { + if (image == null || x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight()) { throw new IndexOutOfBoundsException("(" + x + "," + y + ")"); + } else { image.setRGB(x, y, ((int) color.getRed()) * 65536 + ((int) color.getGreen()) * 256 + (int) color.getBlue()); Canvas.getInstance().repaint(); @@ -256,6 +318,7 @@ public void setColorAt(int x, int y, Color color) { * @param dx the amount by which to move in x-direction * @param dy the amount by which to move in y-direction */ + @Override public void translate(double dx, double dy) { x += dx; y += dy; @@ -268,6 +331,7 @@ public void translate(double dx, double dy) { * @param dw the amount by which to resize the width on each side * @param dh the amount by which to resize the height on each side */ + @Override public void grow(double dw, double dh) { xGrow += dw; yGrow += dh; @@ -277,6 +341,7 @@ public void grow(double dw, double dh) { /** * Shows this picture on the canvas. */ + @Override public void draw() { Canvas.getInstance().show(this); } @@ -284,6 +349,7 @@ public void draw() { /** * Deletes this picture from the canvas. */ + @Override public void delete() { Canvas.getInstance().hide(this); } @@ -293,14 +359,19 @@ public void delete() { * * @param g2D the graphics context */ + @Override public void paintShape(Graphics2D g2D) { + if (image != null) { Dimension dim = label.getPreferredSize(); + if (dim.width > 0 && dim.height > 0) { label.setBounds(0, 0, dim.width, dim.height); g2D.translate(getX(), getY()); + g2D.scale((image.getWidth() + 2 * xGrow) / dim.width, (image.getHeight() + 2 * yGrow) / dim.height); + label.paint(g2D); } } From a71a552bc776ae1bce879e1205e0d760fb3807f9 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 15:07:46 +0100 Subject: [PATCH 19/25] add minor fix on javadoc comment top-lebvel on picture class --- .../com/codeforall/online/simplegraphics/pictures/Picture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java b/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java index b692c3b..3ec1358 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java +++ b/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java @@ -14,7 +14,7 @@ /** * A picture from an image file. - * + * * Implements: *
      *
    • {@link Shape} – for rendering and bounding box info
    • From 17dda7c6cb4d3d16983116031315bfe8c218e4a4 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 15:09:15 +0100 Subject: [PATCH 20/25] add changes to pom.xml to compile on java 17 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 3fc1d12..8477195 100644 --- a/pom.xml +++ b/pom.xml @@ -35,8 +35,8 @@ maven-compiler-plugin 3.5.1 - 1.8 - 1.8 + 17 + 17 UTF-8 From 3081303d8ecd0c5fb2da42ac0b82bdc7a52021ca Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 15:36:45 +0100 Subject: [PATCH 21/25] change packages names --- pom.xml | 2 +- .../simplegraphics/graphics/Canvas.java | 10 +++---- .../simplegraphics/graphics/Color.java | 2 +- .../simplegraphics/graphics/Colorable.java | 2 +- .../simplegraphics/graphics/Ellipse.java | 2 +- .../simplegraphics/graphics/Fillable.java | 2 +- .../simplegraphics/graphics/Line.java | 2 +- .../simplegraphics/graphics/Movable.java | 2 +- .../simplegraphics/graphics/Rectangle.java | 4 +-- .../simplegraphics/graphics/Shape.java | 2 +- .../simplegraphics/graphics/Text.java | 2 +- .../simplegraphics/keyboard/Keyboard.java | 4 +-- .../keyboard/KeyboardEvent.java | 2 +- .../keyboard/KeyboardEventType.java | 2 +- .../keyboard/KeyboardHandler.java | 2 +- .../simplegraphics/mouse/Mouse.java | 18 +++++------ .../simplegraphics/mouse/MouseEvent.java | 2 +- .../simplegraphics/mouse/MouseEventType.java | 2 +- .../simplegraphics/mouse/MouseHandler.java | 2 +- .../simplegraphics/pictures/Color.java | 2 +- .../simplegraphics/pictures/Picture.java | 30 +++++++++---------- 21 files changed, 48 insertions(+), 50 deletions(-) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Canvas.java (94%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Color.java (97%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Colorable.java (82%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Ellipse.java (98%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Fillable.java (74%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Line.java (98%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Movable.java (83%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Rectangle.java (96%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Shape.java (95%) rename src/main/java/com/codeforall/{online => }/simplegraphics/graphics/Text.java (98%) rename src/main/java/com/codeforall/{online => }/simplegraphics/keyboard/Keyboard.java (96%) rename src/main/java/com/codeforall/{online => }/simplegraphics/keyboard/KeyboardEvent.java (98%) rename src/main/java/com/codeforall/{online => }/simplegraphics/keyboard/KeyboardEventType.java (72%) rename src/main/java/com/codeforall/{online => }/simplegraphics/keyboard/KeyboardHandler.java (89%) rename src/main/java/com/codeforall/{online => }/simplegraphics/mouse/Mouse.java (81%) rename src/main/java/com/codeforall/{online => }/simplegraphics/mouse/MouseEvent.java (96%) rename src/main/java/com/codeforall/{online => }/simplegraphics/mouse/MouseEventType.java (79%) rename src/main/java/com/codeforall/{online => }/simplegraphics/mouse/MouseHandler.java (96%) rename src/main/java/com/codeforall/{online => }/simplegraphics/pictures/Color.java (97%) rename src/main/java/com/codeforall/{online => }/simplegraphics/pictures/Picture.java (89%) diff --git a/pom.xml b/pom.xml index 8477195..29a73c3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.codeforall.online.simplegraphics + com.codeforall.simplegraphics simple-graphics 0.2.2-SNAPSHOT diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java b/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java similarity index 94% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java index 68f02a9..655f74c 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Canvas.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; import javax.imageio.ImageIO; import javax.swing.*; @@ -27,7 +27,7 @@ public class Canvas { private static int MAX_Y = 0; private static Canvas instance = new Canvas(); - private ArrayList shapes = new ArrayList<>(); + private ArrayList shapes = new ArrayList<>(); private BufferedImage background; private JFrame frame; private CanvasComponent component; @@ -114,7 +114,7 @@ public static void snapshot() { * * @param shape the shape to display */ - public void show(Shape shape) { + public void show(com.codeforall.simplegraphics.graphics.Shape shape) { if (!shapes.contains(shape)) { shapes.add(shape); } @@ -127,7 +127,7 @@ public void show(Shape shape) { * * @param shape the shape to hide */ - public void hide(Shape shape) { + public void hide(com.codeforall.simplegraphics.graphics.Shape shape) { if (shapes.contains(shape)) { shapes.remove(shape); } @@ -228,7 +228,7 @@ public void paintComponent(Graphics g) { g.drawImage(background, 0, 0, null); } - for (Shape s : new ArrayList<>(shapes)) { + for (com.codeforall.simplegraphics.graphics.Shape s : new ArrayList<>(shapes)) { Graphics2D g2D = (Graphics2D) g.create(); s.paintShape(g2D); g2D.dispose(); diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java b/src/main/java/com/codeforall/simplegraphics/graphics/Color.java similarity index 97% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Color.java index ad912c2..fd49524 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Color.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Color.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; /** * Represents a simple RGB color definition used for graphical shapes. diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Colorable.java b/src/main/java/com/codeforall/simplegraphics/graphics/Colorable.java similarity index 82% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Colorable.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Colorable.java index d71a1be..5c2afc0 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Colorable.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Colorable.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; /** * Methods for modifying a shape color diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java b/src/main/java/com/codeforall/simplegraphics/graphics/Ellipse.java similarity index 98% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Ellipse.java index 55b41f2..33c8bc4 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Ellipse.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Ellipse.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; import java.awt.*; import java.awt.geom.Ellipse2D; diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java b/src/main/java/com/codeforall/simplegraphics/graphics/Fillable.java similarity index 74% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Fillable.java index ea4ddf2..2ef4b9d 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Fillable.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Fillable.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; /** * Methods for filling a shape diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java b/src/main/java/com/codeforall/simplegraphics/graphics/Line.java similarity index 98% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Line.java index 5eb6619..284b836 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Line.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Line.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; import java.awt.*; import java.awt.geom.Line2D; diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java b/src/main/java/com/codeforall/simplegraphics/graphics/Movable.java similarity index 83% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Movable.java index 3e88dd9..a98bdbd 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Movable.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Movable.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; /** * Methods for moving a shape diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java b/src/main/java/com/codeforall/simplegraphics/graphics/Rectangle.java similarity index 96% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Rectangle.java index e50d095..3fb45ac 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Rectangle.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Rectangle.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; import java.awt.*; import java.awt.geom.Rectangle2D; @@ -10,7 +10,7 @@ * * Implements: *
        - *
      • {@link Shape} – for rendering and bounding box info
      • + *
      • {@link com.codeforall.simplegraphics.graphics.Shape} – for rendering and bounding box info
      • *
      • {@link Colorable} – for setting its color
      • *
      • {@link Fillable} – for supporting fill operations
      • *
      • {@link Movable} – for translation and scaling
      • diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java b/src/main/java/com/codeforall/simplegraphics/graphics/Shape.java similarity index 95% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Shape.java index 51ad7cb..7c4c313 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Shape.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Shape.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; import java.awt.*; diff --git a/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java b/src/main/java/com/codeforall/simplegraphics/graphics/Text.java similarity index 98% rename from src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java rename to src/main/java/com/codeforall/simplegraphics/graphics/Text.java index a580eba..53b7b3b 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/graphics/Text.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Text.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.graphics; +package com.codeforall.simplegraphics.graphics; import javax.swing.*; import java.awt.*; diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java b/src/main/java/com/codeforall/simplegraphics/keyboard/Keyboard.java similarity index 96% rename from src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java rename to src/main/java/com/codeforall/simplegraphics/keyboard/Keyboard.java index 3d3f492..6a2ce98 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/Keyboard.java +++ b/src/main/java/com/codeforall/simplegraphics/keyboard/Keyboard.java @@ -1,6 +1,6 @@ -package com.codeforall.online.simplegraphics.keyboard; +package com.codeforall.simplegraphics.keyboard; -import com.codeforall.online.simplegraphics.graphics.Canvas; +import com.codeforall.simplegraphics.graphics.Canvas; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java b/src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardEvent.java similarity index 98% rename from src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java rename to src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardEvent.java index 61b7337..e807f1b 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEvent.java +++ b/src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardEvent.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.keyboard; +package com.codeforall.simplegraphics.keyboard; import java.awt.event.KeyEvent; diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java b/src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardEventType.java similarity index 72% rename from src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java rename to src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardEventType.java index ffcec71..47bc4d8 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardEventType.java +++ b/src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardEventType.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.keyboard; +package com.codeforall.simplegraphics.keyboard; /** * The type of events supported by the Keyboard diff --git a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java b/src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardHandler.java similarity index 89% rename from src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java rename to src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardHandler.java index 9629ea2..eeb0779 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/keyboard/KeyboardHandler.java +++ b/src/main/java/com/codeforall/simplegraphics/keyboard/KeyboardHandler.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.keyboard; +package com.codeforall.simplegraphics.keyboard; import java.awt.event.KeyEvent; diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java b/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java similarity index 81% rename from src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java rename to src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java index 484ad21..cf6cc98 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/Mouse.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.mouse; +package com.codeforall.simplegraphics.mouse; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -7,7 +7,7 @@ import java.util.Iterator; import java.util.List; -import com.codeforall.online.simplegraphics.graphics.Canvas; +import com.codeforall.simplegraphics.graphics.Canvas; /** * Instantiate a Mouse for obtaining mouse handling capability @@ -57,7 +57,7 @@ public void mouseClicked(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_CLICKED) { - handler.mouseClicked(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_CLICKED)); + handler.mouseClicked(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_CLICKED)); } } } @@ -77,7 +77,7 @@ public void mouseMoved(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_MOVED) { - handler.mouseMoved(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_MOVED)); + handler.mouseMoved(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_MOVED)); } } } @@ -97,7 +97,7 @@ public void mousePressed(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_PRESSED) { - handler.mousePressed(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_PRESSED)); + handler.mousePressed(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_PRESSED)); } } } @@ -117,7 +117,7 @@ public void mouseReleased(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_RELEASED) { - handler.mouseReleased(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_RELEASED)); + handler.mouseReleased(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_RELEASED)); } } } @@ -137,7 +137,7 @@ public void mouseEntered(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_ENTERED) { - handler.mouseEntered(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_ENTERED)); + handler.mouseEntered(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_ENTERED)); } } } @@ -157,7 +157,7 @@ public void mouseExited(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_EXITED) { - handler.mouseExited(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_EXITED)); + handler.mouseExited(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_EXITED)); } } } @@ -177,7 +177,7 @@ public void mouseDragged(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_DRAGGED) { - handler.mouseDragged(new com.codeforall.online.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_DRAGGED)); + handler.mouseDragged(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_DRAGGED)); } } } diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java b/src/main/java/com/codeforall/simplegraphics/mouse/MouseEvent.java similarity index 96% rename from src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java rename to src/main/java/com/codeforall/simplegraphics/mouse/MouseEvent.java index 67e49e2..241e6e3 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEvent.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/MouseEvent.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.mouse; +package com.codeforall.simplegraphics.mouse; /** * Mouse event containing x and y coordinates and the mouse event type diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java b/src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java similarity index 79% rename from src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java rename to src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java index e7e46fd..be0a97d 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseEventType.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.mouse; +package com.codeforall.simplegraphics.mouse; public enum MouseEventType { diff --git a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java b/src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java similarity index 96% rename from src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java rename to src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java index 957a88e..18f6b71 100644 --- a/src/main/java/com/codeforall/online/simplegraphics/mouse/MouseHandler.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.mouse; +package com.codeforall.simplegraphics.mouse; /** * Interface to be implemented by all classes that want to receive mouse events diff --git a/src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java b/src/main/java/com/codeforall/simplegraphics/pictures/Color.java similarity index 97% rename from src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java rename to src/main/java/com/codeforall/simplegraphics/pictures/Color.java index fc67e28..0d8b00b 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/pictures/Color.java +++ b/src/main/java/com/codeforall/simplegraphics/pictures/Color.java @@ -1,4 +1,4 @@ -package com.codeforall.online.simplegraphics.pictures; +package com.codeforall.simplegraphics.pictures; /** * A simple representation of an RGB color for pictures. diff --git a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java b/src/main/java/com/codeforall/simplegraphics/pictures/Picture.java similarity index 89% rename from src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java rename to src/main/java/com/codeforall/simplegraphics/pictures/Picture.java index 3ec1358..10cbb00 100755 --- a/src/main/java/com/codeforall/online/simplegraphics/pictures/Picture.java +++ b/src/main/java/com/codeforall/simplegraphics/pictures/Picture.java @@ -1,9 +1,7 @@ -package com.codeforall.online.simplegraphics.pictures; +package com.codeforall.simplegraphics.pictures; -import com.codeforall.online.simplegraphics.graphics.Movable; -import com.codeforall.online.simplegraphics.graphics.Shape; -import com.codeforall.online.simplegraphics.graphics.Color; -import com.codeforall.online.simplegraphics.graphics.Canvas; +import com.codeforall.simplegraphics.graphics.Movable; +import com.codeforall.simplegraphics.graphics.Shape; import javax.imageio.ImageIO; import javax.swing.*; @@ -141,7 +139,7 @@ public void load(String source) { ex.printStackTrace(); } - Canvas.getInstance().repaint(); + com.codeforall.simplegraphics.graphics.Canvas.getInstance().repaint(); } /** @@ -250,7 +248,7 @@ public String toString() { * @param i the pixel index * @return the color at pixel i */ - public Color getColorAt(int i) { + public com.codeforall.simplegraphics.graphics.Color getColorAt(int i) { if (image == null || i < 0 || i >= pixels()) { throw new IndexOutOfBoundsException("" + i); @@ -266,7 +264,7 @@ public Color getColorAt(int i) { * @param i the pixel index * @param color the new color for the pixel */ - public void setColorAt(int i, Color color) { + public void setColorAt(int i, com.codeforall.simplegraphics.graphics.Color color) { if (image == null || i < 0 || i >= pixels()) { throw new IndexOutOfBoundsException("" + i); @@ -283,14 +281,14 @@ public void setColorAt(int i, Color color) { * @param y the y-coordinate (row) of the pixel * @return the color of the pixel */ - public Color getColorAt(int x, int y) { + public com.codeforall.simplegraphics.graphics.Color getColorAt(int x, int y) { if (image == null || x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight()) { throw new IndexOutOfBoundsException("(" + x + "," + y + ")"); } else { int rgb = image.getRGB(x, y) & 0xFFFFFF; - return new Color(rgb / 65536, (rgb / 256) % 256, rgb % 256); + return new com.codeforall.simplegraphics.graphics.Color(rgb / 65536, (rgb / 256) % 256, rgb % 256); } } @@ -301,14 +299,14 @@ public Color getColorAt(int x, int y) { * @param y the y-coordinate (row) of the pixel * @param color the color of the pixel at the given row and column */ - public void setColorAt(int x, int y, Color color) { + public void setColorAt(int x, int y, com.codeforall.simplegraphics.graphics.Color color) { if (image == null || x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight()) { throw new IndexOutOfBoundsException("(" + x + "," + y + ")"); } else { image.setRGB(x, y, ((int) color.getRed()) * 65536 + ((int) color.getGreen()) * 256 + (int) color.getBlue()); - Canvas.getInstance().repaint(); + com.codeforall.simplegraphics.graphics.Canvas.getInstance().repaint(); } } @@ -322,7 +320,7 @@ public void setColorAt(int x, int y, Color color) { public void translate(double dx, double dy) { x += dx; y += dy; - Canvas.getInstance().repaint(); + com.codeforall.simplegraphics.graphics.Canvas.getInstance().repaint(); } /** @@ -335,7 +333,7 @@ public void translate(double dx, double dy) { public void grow(double dw, double dh) { xGrow += dw; yGrow += dh; - Canvas.getInstance().repaint(); + com.codeforall.simplegraphics.graphics.Canvas.getInstance().repaint(); } /** @@ -343,7 +341,7 @@ public void grow(double dw, double dh) { */ @Override public void draw() { - Canvas.getInstance().show(this); + com.codeforall.simplegraphics.graphics.Canvas.getInstance().show(this); } /** @@ -351,7 +349,7 @@ public void draw() { */ @Override public void delete() { - Canvas.getInstance().hide(this); + com.codeforall.simplegraphics.graphics.Canvas.getInstance().hide(this); } /** From c1813ed225d5e120ce74b6d90feffd68dc0bb4f7 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 16:01:24 +0100 Subject: [PATCH 22/25] went back on mouse event types --- .../simplegraphics/mouse/Mouse.java | 75 ++----------------- .../simplegraphics/mouse/MouseEventType.java | 7 +- .../simplegraphics/mouse/MouseHandler.java | 30 -------- 3 files changed, 6 insertions(+), 106 deletions(-) diff --git a/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java b/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java index cf6cc98..f5d8546 100644 --- a/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java @@ -88,100 +88,35 @@ public void mouseMoved(MouseEvent e) { * @see MouseListener#mousePressed(MouseEvent) */ @Override - public void mousePressed(MouseEvent e) { - - if (handler != null) { - Iterator iterator = mouseEventTypes.iterator(); - - while (iterator.hasNext()) { - MouseEventType eventType = (MouseEventType) iterator.next(); - - if (eventType == MouseEventType.MOUSE_PRESSED) { - handler.mousePressed(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_PRESSED)); - } - } - } - } + public void mousePressed(MouseEvent e) {} /** * @param e the event to be processed * @see MouseListener#mouseReleased(MouseEvent) */ @Override - public void mouseReleased(MouseEvent e) { - - if (handler != null) { - Iterator iterator = mouseEventTypes.iterator(); - - while (iterator.hasNext()) { - MouseEventType eventType = (MouseEventType) iterator.next(); - - if (eventType == MouseEventType.MOUSE_RELEASED) { - handler.mouseReleased(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_RELEASED)); - } - } - } - } + public void mouseReleased(MouseEvent e) {} /** * @param e the event to be processed * @see MouseListener#mouseEntered(MouseEvent) */ @Override - public void mouseEntered(MouseEvent e) { - - if (handler != null) { - Iterator iterator = mouseEventTypes.iterator(); - - while (iterator.hasNext()) { - MouseEventType eventType = (MouseEventType) iterator.next(); - - if (eventType == MouseEventType.MOUSE_ENTERED) { - handler.mouseEntered(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_ENTERED)); - } - } - } - } + public void mouseEntered(MouseEvent e) {} /** * @param e the event to be processed * @see MouseListener#mouseExited(MouseEvent) */ @Override - public void mouseExited(MouseEvent e) { - - if (handler != null) { - Iterator iterator = mouseEventTypes.iterator(); - - while (iterator.hasNext()) { - MouseEventType eventType = (MouseEventType) iterator.next(); - - if (eventType == MouseEventType.MOUSE_EXITED) { - handler.mouseExited(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_EXITED)); - } - } - } - } + public void mouseExited(MouseEvent e) {} /** * @param e the event to be processed * @see MouseMotionListener#mouseDragged(MouseEvent) */ @Override - public void mouseDragged(MouseEvent e) { - - if (handler != null) { - Iterator iterator = mouseEventTypes.iterator(); - - while (iterator.hasNext()) { - MouseEventType eventType = (MouseEventType) iterator.next(); - - if (eventType == MouseEventType.MOUSE_DRAGGED) { - handler.mouseDragged(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_DRAGGED)); - } - } - } - } + public void mouseDragged(MouseEvent e) {} /** * Returns the current {@link MouseHandler} assigned to handle mouse events. diff --git a/src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java b/src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java index be0a97d..a17b454 100644 --- a/src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/MouseEventType.java @@ -3,12 +3,7 @@ public enum MouseEventType { MOUSE_CLICKED, - MOUSE_MOVED, - MOUSE_PRESSED, - MOUSE_RELEASED, - MOUSE_ENTERED, - MOUSE_EXITED, - MOUSE_DRAGGED; + MOUSE_MOVED; private MouseEventType() { } diff --git a/src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java b/src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java index 18f6b71..c0b19e4 100644 --- a/src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/MouseHandler.java @@ -18,34 +18,4 @@ public interface MouseHandler { * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent) */ void mouseMoved(MouseEvent e); - - /** - * @param e the event - * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) - */ - void mousePressed(MouseEvent e); - - /** - * @param e the event - * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) - */ - void mouseReleased(MouseEvent e); - - /** - * @param e the event - * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) - */ - void mouseEntered(MouseEvent e); - - /** - * @param e the event - * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) - */ - void mouseExited(MouseEvent e); - - /** - * @param e the event - * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent) - */ - void mouseDragged(MouseEvent e); } From dc21f2d58f4609c15e6fb787c83ba03576b73924 Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 16:11:01 +0100 Subject: [PATCH 23/25] add change to canvas class --- .../java/com/codeforall/simplegraphics/graphics/Canvas.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java b/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java index 655f74c..6bde8bf 100755 --- a/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java @@ -27,7 +27,7 @@ public class Canvas { private static int MAX_Y = 0; private static Canvas instance = new Canvas(); - private ArrayList shapes = new ArrayList<>(); + private ArrayList shapes = new ArrayList<>(); private BufferedImage background; private JFrame frame; private CanvasComponent component; @@ -79,7 +79,7 @@ public static Canvas getInstance() { public static void pause() { JFrame frame = getInstance().frame; - if (frame != null) { + if (frame == null) { return; } From 045c7be57d19ed90590a455cfd29d34f259e41ec Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 17:41:01 +0100 Subject: [PATCH 24/25] changes to canvas class --- .../simplegraphics/graphics/Canvas.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java b/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java index 6bde8bf..c19761b 100755 --- a/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java +++ b/src/main/java/com/codeforall/simplegraphics/graphics/Canvas.java @@ -92,7 +92,7 @@ public static void pause() { public static void snapshot() { Dimension dim = getInstance().component.getPreferredSize(); Rectangle rect = new Rectangle(0, 0, dim.width, dim.height); - BufferedImage image = new BufferedImage(rect.width, rect.height, 1); + BufferedImage image = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(Color.WHITE); @@ -114,7 +114,7 @@ public static void snapshot() { * * @param shape the shape to display */ - public void show(com.codeforall.simplegraphics.graphics.Shape shape) { + public void show(Shape shape) { if (!shapes.contains(shape)) { shapes.add(shape); } @@ -127,7 +127,7 @@ public void show(com.codeforall.simplegraphics.graphics.Shape shape) { * * @param shape the shape to hide */ - public void hide(com.codeforall.simplegraphics.graphics.Shape shape) { + public void hide(Shape shape) { if (shapes.contains(shape)) { shapes.remove(shape); } @@ -139,16 +139,18 @@ public void hide(com.codeforall.simplegraphics.graphics.Shape shape) { * Repaints the canvas if needed. */ public void repaint() { - if (frame != null) { + if (frame == null) { return; } Dimension dim = component.getPreferredSize(); - if (dim.getWidth() <= (double) component.getWidth() && dim.getHeight() <= (double) component.getHeight()) { - frame.repaint(); - } else { + if (dim.getWidth() > component.getWidth() + || dim.getHeight() > component.getHeight()) { frame.pack(); + + } else { + frame.repaint(); } } @@ -160,7 +162,7 @@ public void repaint() { public void saveToDisk(String fileName) { Dimension dim = component.getPreferredSize(); Rectangle rect = new Rectangle(0, 0, dim.width, dim.height); - BufferedImage image = new BufferedImage(rect.width, rect.height, 1); + BufferedImage image = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) image.getGraphics(); g.setColor(Color.WHITE); @@ -228,7 +230,7 @@ public void paintComponent(Graphics g) { g.drawImage(background, 0, 0, null); } - for (com.codeforall.simplegraphics.graphics.Shape s : new ArrayList<>(shapes)) { + for (Shape s : new ArrayList<>(shapes)) { Graphics2D g2D = (Graphics2D) g.create(); s.paintShape(g2D); g2D.dispose(); From b052e057f5a4396aa44ba9f0a1433079d29e64dc Mon Sep 17 00:00:00 2001 From: Raquel Gomes Date: Tue, 3 Jun 2025 17:43:54 +0100 Subject: [PATCH 25/25] changes to mouse class --- src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java b/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java index f5d8546..c3feaef 100644 --- a/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java +++ b/src/main/java/com/codeforall/simplegraphics/mouse/Mouse.java @@ -57,7 +57,7 @@ public void mouseClicked(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_CLICKED) { - handler.mouseClicked(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_CLICKED)); + handler.mouseClicked(new com.codeforall.simplegraphics.mouse.MouseEvent(e.getX(), e.getY(), MouseEventType.MOUSE_CLICKED)); } } } @@ -77,7 +77,7 @@ public void mouseMoved(MouseEvent e) { MouseEventType eventType = (MouseEventType) iterator.next(); if (eventType == MouseEventType.MOUSE_MOVED) { - handler.mouseMoved(new com.codeforall.simplegraphics.mouse.MouseEvent((double) e.getX(), (double) e.getY(), MouseEventType.MOUSE_MOVED)); + handler.mouseMoved(new com.codeforall.simplegraphics.mouse.MouseEvent(e.getX(), e.getY(), MouseEventType.MOUSE_MOVED)); } } }