Creato progetto grafica, sistemato shape

This commit is contained in:
2023-05-15 18:38:10 +02:00
parent 7d0cbb0d02
commit 50f648d037
16 changed files with 2074 additions and 120 deletions

View File

@@ -1,55 +0,0 @@
package shape;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
/**
*
* @author radaelli11353
*/
public class Circle extends Shape {
private double r;
public Circle(double xc, double yc, double r) {
super(xc-r, yc-r);
this.r = r;
}
public void setRadius(double r) {
this.r = r;
}
@Override
public double width() {
return r*2;
}
@Override
public double height() {
return r*2;
}
@Override
public int hashCode() {
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;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Circle c = (Circle) o;
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(posX(), posY(), width(), height());
g2.draw(e);
}
}

View File

@@ -1,47 +0,0 @@
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

@@ -1,32 +0,0 @@
package shape;
import java.awt.Graphics2D;
/**
*
* @author radaelli11353
*/
public abstract class Shape implements Comparable<Shape> {
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);
@Override
public int compareTo(Shape o) {
return Double.compare(width() * height(), o.width() * o.height());
}
}

View File

@@ -1,22 +0,0 @@
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);
}
}

View File

@@ -1,64 +0,0 @@
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)posX(), (int)posY(), (int)width(), (int)height());
g2.draw(r);
}
}

View File

@@ -1,33 +0,0 @@
package shape;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
*
* @author radaelli11353
*/
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);
System.out.println(c1.posX() + ", " +c1.posY());
System.out.println(c1.width() + ", " +c1.height());
System.out.println(c1.equals(c2));
((Circle) c2).setRadius(2);
System.out.println(c2.posX() + ", " + c2.posY());
}*/
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Geometria geom = new Geometria();
geom.riempiACaso(10);
geom.draw(g2);
}
}

View File

@@ -1,75 +0,0 @@
package shape;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
/**
*
* @author radaelli11353
*/
public class TriangoloRettangolo extends Shape {
private double b;
private double h;
public TriangoloRettangolo(double b, double h, double x, double y) {
super(x, y);
this.b = b;
this.h = h;
}
@Override
public double width() {
return b;
}
@Override
public double height() {
return h;
}
@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(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);
g2.draw(ipotenusa);
}
}