diff --git a/NetBeans Projects/shape/src/shape/Circle.java b/NetBeans Projects/shape/src/shape/Circle.java index 406619d..3b00b67 100644 --- a/NetBeans Projects/shape/src/shape/Circle.java +++ b/NetBeans Projects/shape/src/shape/Circle.java @@ -8,13 +8,10 @@ import java.awt.geom.Ellipse2D; * @author radaelli11353 */ public class Circle extends Shape { - private double xc; - private double yc; private double r; public Circle(double xc, double yc, double r) { - this.xc = xc; - this.yc = yc; + super(xc-r, yc-r); this.r = r; } @@ -32,16 +29,6 @@ public class Circle extends Shape { return r*2; } - @Override - public double posX() { - return xc - r; - } - - @Override - public double posY() { - return yc - r; - } - @Override public int hashCode() { int hash = 5; @@ -61,7 +48,7 @@ public class Circle extends Shape { @Override public void draw(Graphics2D g2) { - Ellipse2D.Double e = new Ellipse2D.Double(xc-r, yc-r, r*2, r*2); + Ellipse2D.Double e = new Ellipse2D.Double(posX(), posY(), width(), height()); g2.draw(e); } diff --git a/NetBeans Projects/shape/src/shape/Geometria.java b/NetBeans Projects/shape/src/shape/Geometria.java new file mode 100644 index 0000000..3bc8b36 --- /dev/null +++ b/NetBeans Projects/shape/src/shape/Geometria.java @@ -0,0 +1,47 @@ +package shape; + +import java.util.ArrayList; +import java.util.Random; +import java.awt.Graphics2D; + +public class Geometria { + private ArrayList elementi; + + public Geometria() { + elementi = new ArrayList<>(); + } + + public void riempiACaso(int size) { + Random r = new Random(); + + for(int i = 0; i < size; i++) { + int s = r.nextInt(3); + + double x = r.nextDouble()*300; + double y = r.nextDouble()*300; + + switch(s) { + case 0: + //circle + double raggio = r.nextDouble()*30; + elementi.add(new Circle(x, y, raggio)); + break; + case 1: + //square + double lato = r.nextDouble()*40; + elementi.add(new Square(lato, x, y)); + break; + case 2: + //triangolo + double base = r.nextDouble()*30; + double altezza = r.nextDouble()*30; + elementi.add(new TriangoloRettangolo(base, altezza, x, y)); + break; + } + } + } + + public void draw(Graphics2D g2) { + for(Shape s : elementi) s.draw(g2); + } +} diff --git a/NetBeans Projects/shape/src/shape/Shape.java b/NetBeans Projects/shape/src/shape/Shape.java index 8d2f3bb..87b7c2f 100644 --- a/NetBeans Projects/shape/src/shape/Shape.java +++ b/NetBeans Projects/shape/src/shape/Shape.java @@ -7,15 +7,26 @@ import java.awt.Graphics2D; * @author radaelli11353 */ public abstract class Shape implements Comparable { - public abstract double width(); - public abstract double height(); - public abstract double posX(); - public abstract double posY(); + private double x, y; + + public Shape(double x, double y) { + this.x = x; + this.y = y; + } + + public abstract double width(); + public abstract double height(); + public double posX() { + return x; + } + public double posY() { + return y; + } - public abstract void draw(Graphics2D g2); + public abstract void draw(Graphics2D g2); - @Override - public int compareTo(Shape o) { - return Double.compare(width() * height(), o.width() * o.height()); - } + @Override + public int compareTo(Shape o) { + return Double.compare(width() * height(), o.width() * o.height()); + } } diff --git a/NetBeans Projects/shape/src/shape/Square.java b/NetBeans Projects/shape/src/shape/Square.java index 2058a3b..4e6311e 100644 --- a/NetBeans Projects/shape/src/shape/Square.java +++ b/NetBeans Projects/shape/src/shape/Square.java @@ -57,7 +57,7 @@ public class Square extends Shape { @Override public void draw(Graphics2D g2) { - Rectangle r = new Rectangle((int)x, (int)y, (int)l, (int)l); + Rectangle r = new Rectangle((int)posX(), (int)posY(), (int)width(), (int)height()); g2.draw(r); } diff --git a/NetBeans Projects/shape/src/shape/Tester.java b/NetBeans Projects/shape/src/shape/Tester.java index a1526d1..d2a8c1e 100644 --- a/NetBeans Projects/shape/src/shape/Tester.java +++ b/NetBeans Projects/shape/src/shape/Tester.java @@ -9,7 +9,7 @@ import javax.swing.JComponent; * @author radaelli11353 */ public class Tester extends JComponent{ - public static void main(String[] args) { + /*public static void main(String[] args) { Shape c1 = new Circle(2, 3, 1); Shape c2 = new Circle(2, 3, 1); @@ -19,17 +19,15 @@ public class Tester extends JComponent{ ((Circle) c2).setRadius(2); 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); + Geometria geom = new Geometria(); - s1.draw(g2); - s2.draw(g2); - s3.draw(g2); + geom.riempiACaso(10); + + geom.draw(g2); } } diff --git a/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java b/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java index ae6f8c3..c8ec99a 100644 --- a/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java +++ b/NetBeans Projects/shape/src/shape/TriangoloRettangolo.java @@ -10,14 +10,11 @@ import java.awt.geom.Line2D; 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; + super(x, y); + this.b = b; this.h = h; - this.x = x; - this.y = y; } @Override @@ -30,16 +27,6 @@ public class TriangoloRettangolo extends Shape { return h; } - @Override - public double posX() { - return x; - } - - @Override - public double posY() { - return y; - } - @Override public int hashCode() { int hash = 3; @@ -76,9 +63,9 @@ public class TriangoloRettangolo extends Shape { @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); + Line2D.Double altezza = new Line2D.Double(posX(), posY(), posX(), posY()+height()); + Line2D.Double base = new Line2D.Double(posX(), posY()+height(), posX()+width(), posY()+height()); + Line2D.Double ipotenusa = new Line2D.Double(posX(), posY(), posX()+width(), posY()+height()); g2.draw(altezza); g2.draw(base);