Aggiunti esercizi P7.17 e 18
Aggiunti esercizi P7.17 e 18 (Cloud e Polygon), grafica
This commit is contained in:
parent
0b01a6382d
commit
c8f9eda3a8
34
7.x/P7.17 Purse/Cloud.java
Normal file
34
7.x/P7.17 Purse/Cloud.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
/**
|
||||
Classe che rappresenta lo stato di una lampadina
|
||||
@author radaelli11353
|
||||
*/
|
||||
public class Cloud {
|
||||
public static final int DIAMETRO = 5;
|
||||
private Point2D.Double[] punti;
|
||||
private int nPunti;
|
||||
|
||||
public Cloud() {
|
||||
punti = new Point2D.Double[100];
|
||||
nPunti = 0;
|
||||
}
|
||||
|
||||
public void add(Point2D.Double aPoint) {
|
||||
if(nPunti >= punti.length) throw new IllegalArgumentException();
|
||||
punti[nPunti] = aPoint;
|
||||
nPunti++;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2) {
|
||||
for(int i = 0; i < nPunti; i++) {
|
||||
Ellipse2D.Double punto = new Ellipse2D.Double(punti[i].getX(), punti[i].getY(), DIAMETRO, DIAMETRO);
|
||||
g2.setColor(Color.RED);
|
||||
g2.draw(punto);
|
||||
g2.fill(punto);
|
||||
}
|
||||
}
|
||||
}
|
28
7.x/P7.17 Purse/CloudComponent.java
Normal file
28
7.x/P7.17 Purse/CloudComponent.java
Normal file
@ -0,0 +1,28 @@
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import javax.swing.JComponent;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
Classe che estende JComponent per creare un componente Lampadina
|
||||
@author radaelli11353
|
||||
*/
|
||||
public class CloudComponent extends JComponent {
|
||||
/**
|
||||
Metodo paintComponent che si occupa di disegnare una lampadina
|
||||
@param g Contesto grafico di Graphics
|
||||
*/
|
||||
public void paintComponent(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
Random generator = new Random();
|
||||
|
||||
Cloud nuvola = new Cloud();
|
||||
|
||||
for(int i = 0; i < 100; i++) {
|
||||
nuvola.add(new Point2D.Double(generator.nextDouble()*400, generator.nextDouble()*400));
|
||||
}
|
||||
|
||||
nuvola.draw(g2);
|
||||
}
|
||||
}
|
24
7.x/P7.17 Purse/CloudViewer.java
Normal file
24
7.x/P7.17 Purse/CloudViewer.java
Normal file
@ -0,0 +1,24 @@
|
||||
import javax.swing.JFrame;
|
||||
|
||||
/**
|
||||
Classe che visualizza una lampadina tramite un JFrame e LampadinaComponent
|
||||
@author radaelli11353
|
||||
*/
|
||||
public class CloudViewer {
|
||||
/**
|
||||
Metodo main di LampadinaViewer
|
||||
@param args Argomenti passati all'esecuzione del programma
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame();
|
||||
|
||||
frame.setSize(500, 500);
|
||||
frame.setTitle("Visualizzatore nuvola");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
CloudComponent component = new CloudComponent();
|
||||
frame.add(component);
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
32
7.x/P7.18 Polygon/Polygon.java
Normal file
32
7.x/P7.18 Polygon/Polygon.java
Normal file
@ -0,0 +1,32 @@
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.geom.Line2D;
|
||||
|
||||
/**
|
||||
Classe che rappresenta lo stato di una lampadina
|
||||
@author radaelli11353
|
||||
*/
|
||||
public class Polygon {
|
||||
public static final int DIAMETRO = 5;
|
||||
private Point2D.Double[] punti;
|
||||
private int nPunti;
|
||||
|
||||
public Polygon() {
|
||||
punti = new Point2D.Double[100];
|
||||
nPunti = 0;
|
||||
}
|
||||
|
||||
public void add(Point2D.Double aPoint) {
|
||||
if(nPunti >= punti.length) throw new IllegalArgumentException();
|
||||
punti[nPunti] = aPoint;
|
||||
nPunti++;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2) {
|
||||
for(int i = 0; i < nPunti - 1; i++) {
|
||||
Line2D.Double linea = new Line2D.Double(punti[i].getX(), punti[i].getY(), punti[i+1].getX(), punti[i+1].getY());
|
||||
g2.draw(linea);
|
||||
}
|
||||
g2.draw(new Line2D.Double(punti[nPunti - 1].getX(), punti[nPunti - 1].getY(), punti[0].getX(), punti[0].getY()));
|
||||
}
|
||||
}
|
36
7.x/P7.18 Polygon/PolygonComponent.java
Normal file
36
7.x/P7.18 Polygon/PolygonComponent.java
Normal file
@ -0,0 +1,36 @@
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import javax.swing.JComponent;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
Classe che estende JComponent per creare un componente Lampadina
|
||||
@author radaelli11353
|
||||
*/
|
||||
public class PolygonComponent extends JComponent {
|
||||
/**
|
||||
Metodo paintComponent che si occupa di disegnare una lampadina
|
||||
@param g Contesto grafico di Graphics
|
||||
*/
|
||||
public void paintComponent(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
||||
Polygon quadrato = new Polygon();
|
||||
Polygon pentagono = new Polygon();
|
||||
|
||||
quadrato.add(new Point2D.Double(0, 0));
|
||||
quadrato.add(new Point2D.Double(50, 0));
|
||||
quadrato.add(new Point2D.Double(50, 50));
|
||||
quadrato.add(new Point2D.Double(0, 50));
|
||||
|
||||
pentagono.add(new Point2D.Double(100, 100));
|
||||
pentagono.add(new Point2D.Double(70, 80));
|
||||
pentagono.add(new Point2D.Double(50, 50));
|
||||
pentagono.add(new Point2D.Double(150, 70));
|
||||
pentagono.add(new Point2D.Double(120, 100));
|
||||
|
||||
quadrato.draw(g2);
|
||||
pentagono.draw(g2);
|
||||
}
|
||||
}
|
24
7.x/P7.18 Polygon/PolygonViewer.java
Normal file
24
7.x/P7.18 Polygon/PolygonViewer.java
Normal file
@ -0,0 +1,24 @@
|
||||
import javax.swing.JFrame;
|
||||
|
||||
/**
|
||||
Classe che visualizza una lampadina tramite un JFrame e LampadinaComponent
|
||||
@author radaelli11353
|
||||
*/
|
||||
public class PolygonViewer {
|
||||
/**
|
||||
Metodo main di LampadinaViewer
|
||||
@param args Argomenti passati all'esecuzione del programma
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame();
|
||||
|
||||
frame.setSize(500, 500);
|
||||
frame.setTitle("Visualizzatore poligono");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
PolygonComponent component = new PolygonComponent();
|
||||
frame.add(component);
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user