From f7f59656195b85b772d46cc84d374b3e691e9646 Mon Sep 17 00:00:00 2001 From: Giacomo Radaelli Date: Thu, 4 May 2023 22:10:05 +0200 Subject: [PATCH] Aggiornato Shape Aggiunti Square e TriangoloRettangolo. Aggiunto Viewer di test --- .../shape/nbproject/private/config.properties | 0 .../nbproject/private/private.properties | 8 +- .../shape/nbproject/private/private.xml | 7 +- .../shape/nbproject/project.properties | 8 +- NetBeans Projects/shape/src/shape/Circle.java | 22 +++-- .../shape/src/shape/Rectangle.java | 57 ------------ NetBeans Projects/shape/src/shape/Shape.java | 4 + .../shape/src/shape/ShapesViewer.java | 22 +++++ NetBeans Projects/shape/src/shape/Square.java | 64 ++++++++++++++ NetBeans Projects/shape/src/shape/Tester.java | 24 +++-- .../shape/src/shape/TriangoloRettangolo.java | 88 +++++++++++++++++++ 11 files changed, 229 insertions(+), 75 deletions(-) create mode 100644 NetBeans Projects/shape/nbproject/private/config.properties delete mode 100644 NetBeans Projects/shape/src/shape/Rectangle.java create mode 100644 NetBeans Projects/shape/src/shape/ShapesViewer.java create mode 100644 NetBeans Projects/shape/src/shape/Square.java create mode 100644 NetBeans Projects/shape/src/shape/TriangoloRettangolo.java diff --git a/NetBeans Projects/shape/nbproject/private/config.properties b/NetBeans Projects/shape/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/NetBeans Projects/shape/nbproject/private/private.properties b/NetBeans Projects/shape/nbproject/private/private.properties index a2ad8bd..721cc40 100644 --- a/NetBeans Projects/shape/nbproject/private/private.properties +++ b/NetBeans Projects/shape/nbproject/private/private.properties @@ -1,2 +1,8 @@ compile.on.save=true -user.properties.file=/home/giacomo/.netbeans/17/build.properties +do.depend=false +do.jar=true +do.jlink=false +javac.debug=true +javadoc.preview=true +jlink.strip=false +user.properties.file=/home/gicorada/.netbeans/17/build.properties diff --git a/NetBeans Projects/shape/nbproject/private/private.xml b/NetBeans Projects/shape/nbproject/private/private.xml index 452ba67..e056723 100644 --- a/NetBeans Projects/shape/nbproject/private/private.xml +++ b/NetBeans Projects/shape/nbproject/private/private.xml @@ -3,7 +3,12 @@ - file:/home/giacomo/Scrivania/Scuola/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/Shape.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/Square.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/Tester.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/Shape.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/Circle.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/TriangoloRettangolo.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/shape/src/shape/ShapesViewer.java diff --git a/NetBeans Projects/shape/nbproject/project.properties b/NetBeans Projects/shape/nbproject/project.properties index a0200a4..5233b45 100644 --- a/NetBeans Projects/shape/nbproject/project.properties +++ b/NetBeans Projects/shape/nbproject/project.properties @@ -1,9 +1,10 @@ annotation.processing.enabled=true annotation.processing.enabled.in.editor=false -annotation.processing.processor.options= annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=shape +application.vendor=gicorada build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form # This directory is removed when the project is cleaned: @@ -32,6 +33,7 @@ dist.jar=${dist.dir}/shape.jar dist.javadoc.dir=${dist.dir}/javadoc dist.jlink.dir=${dist.dir}/jlink dist.jlink.output=${dist.jlink.dir}/shape +endorsed.classpath= excludes= includes=** jar.compress=false @@ -71,11 +73,11 @@ jlink.additionalmodules= jlink.additionalparam= jlink.launcher=true jlink.launcher.name=shape -main.class=shape.Tester +main.class=shape.ShapesViewer manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false -platform.active=Oracle_OpenJDK_20_36 +platform.active=Zulu_17.0.6_10 run.classpath=\ ${javac.classpath}:\ ${build.classes.dir} diff --git a/NetBeans Projects/shape/src/shape/Circle.java b/NetBeans Projects/shape/src/shape/Circle.java index 9cc6e77..406619d 100644 --- a/NetBeans Projects/shape/src/shape/Circle.java +++ b/NetBeans Projects/shape/src/shape/Circle.java @@ -1,5 +1,8 @@ package shape; +import java.awt.Graphics2D; +import java.awt.geom.Ellipse2D; + /** * * @author radaelli11353 @@ -38,13 +41,13 @@ public class Circle extends Shape { public double posY() { return yc - r; } - + @Override public int hashCode() { - int hash = 3; - hash = 59 * hash + this.xc; - hash = 59 * hash + this.yc; - hash = 59 * hash + this.r; + int hash = 5; + hash = 83 * hash + (int) (Double.doubleToLongBits(this.xc) ^ (Double.doubleToLongBits(this.xc) >>> 32)); + hash = 83 * hash + (int) (Double.doubleToLongBits(this.yc) ^ (Double.doubleToLongBits(this.yc) >>> 32)); + hash = 83 * hash + (int) (Double.doubleToLongBits(this.r) ^ (Double.doubleToLongBits(this.r) >>> 32)); return hash; } @@ -53,6 +56,13 @@ public class Circle extends Shape { if (o == null) return false; if (getClass() != o.getClass()) return false; Circle c = (Circle) o; - return Double.compare(xc, c.xc) && Double.compare(yc, c.yc) && Double.compare(r, c.r); + return Double.compare(xc, c.xc) == 0 && Double.compare(yc, c.yc) == 0 && Double.compare(r, c.r) == 0; + } + + @Override + public void draw(Graphics2D g2) { + Ellipse2D.Double e = new Ellipse2D.Double(xc-r, yc-r, r*2, r*2); + + g2.draw(e); } } diff --git a/NetBeans Projects/shape/src/shape/Rectangle.java b/NetBeans Projects/shape/src/shape/Rectangle.java deleted file mode 100644 index 90b827e..0000000 --- a/NetBeans Projects/shape/src/shape/Rectangle.java +++ /dev/null @@ -1,57 +0,0 @@ -package shape; - -/** - * - * @author radaelli11353 - */ -public class Rectangle extends Shape { - private double x; - private double y; - private double w; - private double h; - - public Rectangle(double x, double y, double w, double h) { - this.x = x; - this.y = y; - this.w = w; - this.h = h; - } - - @Override - public double width() { - return w; - } - - @Override - public double height() { - return h; - } - - @Override - public double posX() { - return x; - } - - @Override - public double posY() { - return y; - } - - @Override - public int hashCode() { - int hash = 5; - hash = 37 * hash + this.x; - hash = 37 * hash + this.y; - hash = 37 * hash + this.w; - hash = 37 * hash + this.h; - return hash; - } - - @Override - public boolean equals(Object o) { - if (o == null) return false; - if (getClass() != o.getClass()) return false; - Rectangle r = (Rectangle) o; - return Double.compare(r, r.w) && Double.compare(h, r.h) && Double.compare(x, r.x) && Double.compare(y, r.y); - } -} diff --git a/NetBeans Projects/shape/src/shape/Shape.java b/NetBeans Projects/shape/src/shape/Shape.java index b3ca0e4..8d2f3bb 100644 --- a/NetBeans Projects/shape/src/shape/Shape.java +++ b/NetBeans Projects/shape/src/shape/Shape.java @@ -1,5 +1,7 @@ package shape; +import java.awt.Graphics2D; + /** * * @author radaelli11353 @@ -10,6 +12,8 @@ public abstract class Shape implements Comparable { public abstract double posX(); public abstract double posY(); + public abstract void draw(Graphics2D g2); + @Override public int compareTo(Shape o) { return Double.compare(width() * height(), o.width() * o.height()); diff --git a/NetBeans Projects/shape/src/shape/ShapesViewer.java b/NetBeans Projects/shape/src/shape/ShapesViewer.java new file mode 100644 index 0000000..ad9a7ed --- /dev/null +++ b/NetBeans Projects/shape/src/shape/ShapesViewer.java @@ -0,0 +1,22 @@ +package shape; + +import javax.swing.JFrame; + +/** + * + * @author radaelli11353 + */ +public class ShapesViewer { + public static void main(String[] args) { + JFrame frame = new JFrame(); + + frame.setSize(600, 600); + frame.setTitle("Circles"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + Tester component = new Tester(); + frame.add(component); + + frame.setVisible(true); + } +} diff --git a/NetBeans Projects/shape/src/shape/Square.java b/NetBeans Projects/shape/src/shape/Square.java new file mode 100644 index 0000000..2058a3b --- /dev/null +++ b/NetBeans Projects/shape/src/shape/Square.java @@ -0,0 +1,64 @@ +package shape; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +/** + * + * @author radaelli11353 + */ +public class Square extends Shape { + private double x; + private double y; + private double l; + + public Square(double x, double y, double l) { + this.x = x; + this.y = y; + this.l = l; + } + + @Override + public double width() { + return l; + } + + @Override + public double height() { + return l; + } + + @Override + public double posX() { + return x; + } + + @Override + public double posY() { + return y; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 97 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32)); + hash = 97 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32)); + hash = 97 * hash + (int) (Double.doubleToLongBits(this.l) ^ (Double.doubleToLongBits(this.l) >>> 32)); + return hash; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (getClass() != o.getClass()) return false; + Square s = (Square) o; + return Double.compare(l, s.l) == 0 && Double.compare(x, s.x) == 0 && Double.compare(y, s.y) == 0; + } + + @Override + public void draw(Graphics2D g2) { + Rectangle r = new Rectangle((int)x, (int)y, (int)l, (int)l); + + g2.draw(r); + } +} diff --git a/NetBeans Projects/shape/src/shape/Tester.java b/NetBeans Projects/shape/src/shape/Tester.java index a58a227..a1526d1 100644 --- a/NetBeans Projects/shape/src/shape/Tester.java +++ b/NetBeans Projects/shape/src/shape/Tester.java @@ -1,14 +1,14 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ package shape; +import java.awt.Graphics; +import java.awt.Graphics2D; +import javax.swing.JComponent; + /** * - * @author gicorada + * @author radaelli11353 */ -public class Tester { +public class Tester extends JComponent{ public static void main(String[] args) { Shape c1 = new Circle(2, 3, 1); Shape c2 = new Circle(2, 3, 1); @@ -21,5 +21,15 @@ public class Tester { System.out.println(c2.posX() + ", " + c2.posY()); } - + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + + Shape s1 = new Circle(88, 200, 50); + Shape s2 = new Square(200, 100, 100); + Shape s3 = new TriangoloRettangolo(200, 100, 100, 100); + + s1.draw(g2); + s2.draw(g2); + s3.draw(g2); + } } diff --git a/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java b/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java new file mode 100644 index 0000000..ae6f8c3 --- /dev/null +++ b/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java @@ -0,0 +1,88 @@ +package shape; + +import java.awt.Graphics2D; +import java.awt.geom.Line2D; + +/** + * + * @author radaelli11353 + */ +public class TriangoloRettangolo extends Shape { + private double b; + private double h; + private double x; + private double y; + + public TriangoloRettangolo(double b, double h, double x, double y) { + this.b = b; + this.h = h; + this.x = x; + this.y = y; + } + + @Override + public double width() { + return b; + } + + @Override + public double height() { + return h; + } + + @Override + public double posX() { + return x; + } + + @Override + public double posY() { + return y; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 53 * hash + (int) (Double.doubleToLongBits(this.b) ^ (Double.doubleToLongBits(this.b) >>> 32)); + hash = 53 * hash + (int) (Double.doubleToLongBits(this.h) ^ (Double.doubleToLongBits(this.h) >>> 32)); + hash = 53 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32)); + hash = 53 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32)); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TriangoloRettangolo other = (TriangoloRettangolo) obj; + if (Double.doubleToLongBits(this.b) != Double.doubleToLongBits(other.b)) { + return false; + } + if (Double.doubleToLongBits(this.h) != Double.doubleToLongBits(other.h)) { + return false; + } + if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) { + return false; + } + return Double.doubleToLongBits(this.y) == Double.doubleToLongBits(other.y); + } + + @Override + public void draw(Graphics2D g2) { + Line2D.Double altezza = new Line2D.Double(x, y, x, y+h); + Line2D.Double base = new Line2D.Double(x, y+h, x+b, y+h); + Line2D.Double ipotenusa = new Line2D.Double(x, y, x+b, y+h); + + g2.draw(altezza); + g2.draw(base); + g2.draw(ipotenusa); + } + +}