Aggiunte classi lezione 5 mag
This commit is contained in:
parent
f273879242
commit
8dceaf0edb
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -7,15 +7,26 @@ import java.awt.Graphics2D;
|
|||
* @author radaelli11353
|
||||
*/
|
||||
public abstract class Shape implements Comparable<Shape> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue