Aggiunte classi lezione 5 mag

This commit is contained in:
Giacomo R. 2023-05-05 15:04:05 +02:00
parent f273879242
commit 8dceaf0edb
6 changed files with 81 additions and 51 deletions

View File

@ -8,13 +8,10 @@ import java.awt.geom.Ellipse2D;
* @author radaelli11353 * @author radaelli11353
*/ */
public class Circle extends Shape { public class Circle extends Shape {
private double xc;
private double yc;
private double r; private double r;
public Circle(double xc, double yc, double r) { public Circle(double xc, double yc, double r) {
this.xc = xc; super(xc-r, yc-r);
this.yc = yc;
this.r = r; this.r = r;
} }
@ -32,16 +29,6 @@ public class Circle extends Shape {
return r*2; return r*2;
} }
@Override
public double posX() {
return xc - r;
}
@Override
public double posY() {
return yc - r;
}
@Override @Override
public int hashCode() { public int hashCode() {
int hash = 5; int hash = 5;
@ -61,7 +48,7 @@ public class Circle extends Shape {
@Override @Override
public void draw(Graphics2D g2) { 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); g2.draw(e);
} }

View File

@ -0,0 +1,47 @@
package shape;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Graphics2D;
public class Geometria {
private ArrayList<Shape> 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);
}
}

View File

@ -7,15 +7,26 @@ import java.awt.Graphics2D;
* @author radaelli11353 * @author radaelli11353
*/ */
public abstract class Shape implements Comparable<Shape> { public abstract class Shape implements Comparable<Shape> {
public abstract double width(); private double x, y;
public abstract double height();
public abstract double posX(); public Shape(double x, double y) {
public abstract double posY(); 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 @Override
public int compareTo(Shape o) { public int compareTo(Shape o) {
return Double.compare(width() * height(), o.width() * o.height()); return Double.compare(width() * height(), o.width() * o.height());
} }
} }

View File

@ -57,7 +57,7 @@ public class Square extends Shape {
@Override @Override
public void draw(Graphics2D g2) { 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); g2.draw(r);
} }

View File

@ -9,7 +9,7 @@ import javax.swing.JComponent;
* @author radaelli11353 * @author radaelli11353
*/ */
public class Tester extends JComponent{ 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 c1 = new Circle(2, 3, 1);
Shape c2 = 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); ((Circle) c2).setRadius(2);
System.out.println(c2.posX() + ", " + c2.posY()); System.out.println(c2.posX() + ", " + c2.posY());
} }*/
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
Shape s1 = new Circle(88, 200, 50); Geometria geom = new Geometria();
Shape s2 = new Square(200, 100, 100);
Shape s3 = new TriangoloRettangolo(200, 100, 100, 100);
s1.draw(g2); geom.riempiACaso(10);
s2.draw(g2);
s3.draw(g2); geom.draw(g2);
} }
} }

View File

@ -10,14 +10,11 @@ import java.awt.geom.Line2D;
public class TriangoloRettangolo extends Shape { public class TriangoloRettangolo extends Shape {
private double b; private double b;
private double h; private double h;
private double x;
private double y;
public TriangoloRettangolo(double b, double h, double x, 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.h = h;
this.x = x;
this.y = y;
} }
@Override @Override
@ -30,16 +27,6 @@ public class TriangoloRettangolo extends Shape {
return h; return h;
} }
@Override
public double posX() {
return x;
}
@Override
public double posY() {
return y;
}
@Override @Override
public int hashCode() { public int hashCode() {
int hash = 3; int hash = 3;
@ -76,9 +63,9 @@ public class TriangoloRettangolo extends Shape {
@Override @Override
public void draw(Graphics2D g2) { public void draw(Graphics2D g2) {
Line2D.Double altezza = new Line2D.Double(x, y, x, y+h); Line2D.Double altezza = new Line2D.Double(posX(), posY(), posX(), posY()+height());
Line2D.Double base = new Line2D.Double(x, y+h, x+b, y+h); Line2D.Double base = new Line2D.Double(posX(), posY()+height(), posX()+width(), posY()+height());
Line2D.Double ipotenusa = new Line2D.Double(x, y, x+b, y+h); Line2D.Double ipotenusa = new Line2D.Double(posX(), posY(), posX()+width(), posY()+height());
g2.draw(altezza); g2.draw(altezza);
g2.draw(base); g2.draw(base);