Verifica
This commit is contained in:
157
SuperVerificaMeravigliosa/src/Main.java
Normal file
157
SuperVerificaMeravigliosa/src/Main.java
Normal file
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
NB. aggiungere dei parametri ai metodi dove indispensabile
|
||||
|
||||
Drawable (interfaccia)
|
||||
void draw(); stampa le coordinate e le dimensioni del bottone.
|
||||
|
||||
Component (classe astratta)
|
||||
classe base astratta che implementa Drawable e rappresenta un generico componente GUI.
|
||||
Contiene le proprietà comuni a tutti i componenti: posizione (x, y) e dimensioni (width, height).
|
||||
|
||||
Button (classe concreta)
|
||||
Estende Component e rappresenta un pulsante.
|
||||
Aggiunge un'etichetta (label) e implementa i metodi:
|
||||
click() → simula il click del bottone.
|
||||
|
||||
TextField (classe concreta)
|
||||
Estende Component e rappresenta un campo di testo.
|
||||
Aggiunge la gestione del contenuto testuale (text) e implementa i metodi:
|
||||
setText() e getText() → gestione del contenuto.
|
||||
|
||||
Frame
|
||||
Classe che rappresenta una finestra contenente componenti.
|
||||
Gestisce:
|
||||
Il titolo e le dimensioni della finestra.
|
||||
Un array di componenti con numero massimo definito.
|
||||
metodi addComponent()
|
||||
draw() → disegna il frame e tutti i componenti e può lanciare una eccezione
|
||||
Eccezioni da gestire:
|
||||
OutOfFrameException → se un componente esce dai limiti della finestra
|
||||
|
||||
Main
|
||||
Contiene il metodo main() che:
|
||||
1. Crea un oggetto Frame.
|
||||
2. Aggiunge un Button e un TextField.
|
||||
3. Disegna la finestra e i componenti.
|
||||
4. Simula un click sul bottone.
|
||||
5. Cambia il testo nel TextField.
|
||||
6. Ridisegna la finestra per mostrare il nuovo stato.
|
||||
|
||||
|
||||
Drawing Frame: 'La mia finestra' size [800x600]
|
||||
Components:
|
||||
Drawing Button 'OK' at (50,100) size [100x40]
|
||||
Drawing TextField with text 'Inserisci testo...' at (200,100) size [200x40]
|
||||
Button 'OK' clicked!
|
||||
Drawing Frame: 'La mia finestra' size [800x600]
|
||||
Components:
|
||||
Drawing Button 'OK' at (50,100) size [100x40]
|
||||
Drawing TextField with text 'Inserisci testo...' at (200,100) size [200x40]
|
||||
!!! Eccezione catturata !!! Il componente TextField esce dai limiti del frame (La mia finestra)
|
||||
|
||||
|
||||
*/
|
||||
|
||||
class OutOfFrameException extends Exception {
|
||||
OutOfFrameException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
class MaxComponentsException extends Exception {
|
||||
MaxComponentsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
interface Drawable {
|
||||
void draw() throws OutOfFrameException;
|
||||
}
|
||||
|
||||
abstract class Component implements Drawable {
|
||||
protected int x,y,width,height;
|
||||
}
|
||||
class Button extends Component {
|
||||
String label;
|
||||
Button(int x, int y, int width, int height, String label) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.label = label;
|
||||
}
|
||||
public void draw() throws OutOfFrameException {
|
||||
System.out.println("Drawing Button '" + label + "' size [" + width + "x" + height + "] at position (" + x + "," + y + ") status OK.");
|
||||
}
|
||||
public void click() {
|
||||
System.out.println("Clicked button '" + label + "'");
|
||||
}
|
||||
}
|
||||
class TextField extends Component {
|
||||
TextField(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
protected String text;
|
||||
public void draw() {
|
||||
System.out.println("Drawing TextField with text '" + text + "' size [" + width + "x" + height + "] at position (" + x + "," + y + ") status OK.");
|
||||
}
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
class Frame extends Component {
|
||||
protected int maxComponents = 10;
|
||||
protected int componentNumber = 0;
|
||||
protected String title;
|
||||
protected Component[] componentlist = new Component[maxComponents];
|
||||
Frame(int width, int height, String title) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.title = title;
|
||||
}
|
||||
public void draw() throws OutOfFrameException{
|
||||
System.out.println("Drawing Frame '" + title + "' size [" + width + "x" + height + "]");
|
||||
System.out.println("Drawing components...");
|
||||
for (int i = 0; i < componentNumber; i++) {
|
||||
if (componentlist[i].width > this.width || componentlist[i].height > this.height) {
|
||||
throw new OutOfFrameException("ERROR! The component is out of the frame limits!");
|
||||
}
|
||||
else {
|
||||
componentlist[i].draw();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void addComponent(Component c) throws MaxComponentsException{
|
||||
if (componentNumber == maxComponents) {
|
||||
throw new MaxComponentsException("ERROR! Maximum number of components reached!");
|
||||
}
|
||||
else {
|
||||
componentlist[componentNumber] = c;
|
||||
componentNumber++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
Frame frame = new Frame(800,600,"La mia finestra");
|
||||
TextField field = new TextField(60,10,801,11);
|
||||
field.setText("Inserisci Testo...");
|
||||
Button button = new Button(30, 40, 90, 30, "OK");
|
||||
frame.addComponent(button);
|
||||
frame.addComponent(field);
|
||||
frame.draw();
|
||||
button.click();
|
||||
} catch (OutOfFrameException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user