Merge branch 'master' of gitea.it:gicorada/java-scuola
This commit is contained in:
commit
dcfe69aefa
|
@ -0,0 +1,42 @@
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.geom.Ellipse2D;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che rappresenta una nuvola composta da punti.
|
||||||
|
* La nuvola può contenere al masssimo 100 punti
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class Cloud {
|
||||||
|
public static final int DIAMETRO = 5;
|
||||||
|
private Point2D.Double[] punti;
|
||||||
|
private int nPunti;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Costruttore della classe Cloud
|
||||||
|
*/
|
||||||
|
public Cloud() {
|
||||||
|
punti = new Point2D.Double[100];
|
||||||
|
nPunti = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo per aggiungere un punto alla nuvola
|
||||||
|
* @param aPoint Punto da aggiungere alla nuvola
|
||||||
|
*/
|
||||||
|
public void add(Point2D.Double aPoint) {
|
||||||
|
if(nPunti >= punti.length) throw new IllegalArgumentException();
|
||||||
|
punti[nPunti] = aPoint;
|
||||||
|
nPunti++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che disegna la nuvola in un contesto grafico
|
||||||
|
* @param g2 Contesto grafico
|
||||||
|
*/
|
||||||
|
public void draw(Graphics2D g2) {
|
||||||
|
for(int i = 0; i < nPunti; i++) {
|
||||||
|
g2.fill(new Ellipse2D.Double(punti[i].getX(), punti[i].getY(), DIAMETRO, DIAMETRO));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
||||||
|
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 Cloud
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class CloudComponent extends JComponent {
|
||||||
|
/**
|
||||||
|
* Metodo paintComponent che si occupa di disegnare una nuvola
|
||||||
|
* @param g Contesto grafico
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,20 @@
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che visualizza una nuvola tramite un JFrame e CloudComponent
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class CloudViewer {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,42 @@
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
import java.awt.geom.Line2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che rappresenta un poligono composto da un numero variabile di punti
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class Polygon {
|
||||||
|
private Point2D.Double[] punti;
|
||||||
|
private int nPunti;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Costruttore della classe Polygon
|
||||||
|
* @param maxPunti Numero massimo di punti per il poligono
|
||||||
|
*/
|
||||||
|
public Polygon(int maxPunti) {
|
||||||
|
punti = new Point2D.Double[maxPunti];
|
||||||
|
nPunti = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo per aggiungere un punto al poligono
|
||||||
|
* @param aPoint Punto da aggiungere al poligono
|
||||||
|
*/
|
||||||
|
public void add(Point2D.Double aPoint) {
|
||||||
|
if(nPunti >= punti.length) throw new IllegalArgumentException();
|
||||||
|
punti[nPunti] = aPoint;
|
||||||
|
nPunti++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che disegna il poligono in un contesto grafico
|
||||||
|
* @param g2 Contesto grafico
|
||||||
|
*/
|
||||||
|
public void draw(Graphics2D g2) {
|
||||||
|
for(int i = 0; i < nPunti - 1; i++) {
|
||||||
|
g2.draw(new Line2D.Double(punti[i].getX(), punti[i].getY(), punti[i+1].getX(), punti[i+1].getY()));
|
||||||
|
}
|
||||||
|
g2.draw(new Line2D.Double(punti[nPunti - 1].getX(), punti[nPunti - 1].getY(), punti[0].getX(), punti[0].getY()));
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,35 @@
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che estende JComponent per creare un componente Cloud
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class PolygonComponent extends JComponent {
|
||||||
|
/**
|
||||||
|
* Metodo paintComponent che si occupa di disegnare un poligono
|
||||||
|
* @param g Contesto grafico
|
||||||
|
*/
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
Graphics2D g2 = (Graphics2D) g;
|
||||||
|
|
||||||
|
Polygon quadrato = new Polygon(4);
|
||||||
|
Polygon pentagono = new Polygon(5);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,20 @@
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che visualizza un poligono tramite un JFrame e PolygonComponent
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class PolygonViewer {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,41 @@
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
import java.awt.geom.Line2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che rappresenta un grafico a barrecomposto da un numero variabile di dati
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class Chart {
|
||||||
|
private int[] valori;
|
||||||
|
private int nValori;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Costruttore della classe Chart
|
||||||
|
* @param maxValori Numero massimo di valori del grafico
|
||||||
|
*/
|
||||||
|
public Chart(int maxValori) {
|
||||||
|
valori = new int[maxValori];
|
||||||
|
nValori = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo per aggiungere un dato al grafico
|
||||||
|
* @param valore Valore da aggiungere al grafico
|
||||||
|
*/
|
||||||
|
public void add(int valore) {
|
||||||
|
if(nValori >= valori.length) throw new IllegalArgumentException();
|
||||||
|
valori[nValori] = valore;
|
||||||
|
nValori++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che disegna il grafico in un contesto grafico
|
||||||
|
* @param g2 Contesto grafico
|
||||||
|
*/
|
||||||
|
public void draw(Graphics2D g2) {
|
||||||
|
for(int i = 0; i < nValori; i++) {
|
||||||
|
g2.draw(new Line2D.Double(10+i*10, 450, 10+i*10, 450-valori[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che estende JComponent per creare un componente Chart
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class ChartComponent extends JComponent {
|
||||||
|
/**
|
||||||
|
* Metodo paintComponent che si occupa di disegnare un grafico a barre
|
||||||
|
* @param g Contesto grafico
|
||||||
|
*/
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
Graphics2D g2 = (Graphics2D) g;
|
||||||
|
|
||||||
|
Chart grafico = new Chart(15);
|
||||||
|
|
||||||
|
grafico.add(150);
|
||||||
|
grafico.add(300);
|
||||||
|
grafico.add(50);
|
||||||
|
grafico.add(150);
|
||||||
|
grafico.add(250);
|
||||||
|
|
||||||
|
grafico.draw(g2);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,20 @@
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che visualizza un grafico a barre tramite un JFrame e ChartComponent
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class ChartViewer {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame frame = new JFrame();
|
||||||
|
|
||||||
|
frame.setSize(500, 500);
|
||||||
|
frame.setTitle("Visualizzatore grafico a barre");
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
ChartComponent component = new ChartComponent();
|
||||||
|
frame.add(component);
|
||||||
|
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue