This commit is contained in:
2025-11-14 09:35:30 +01:00
parent 70e46cc65c
commit ce29e454cc
7 changed files with 179 additions and 0 deletions

30
VerificaBlu/.gitignore vendored Normal file
View File

@@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.kotlin
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
VerificaBlu/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
VerificaBlu/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
VerificaBlu/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/VerificaBlu.iml" filepath="$PROJECT_DIR$/VerificaBlu.iml" />
</modules>
</component>
</project>

6
VerificaBlu/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,110 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class DisegnaFigura {
public static void main(String[] args) {
SwingUtilities.invokeLater(DisegnaFigura::creaGUI);
}
private static void creaGUI() {
JFrame f = new JFrame("DisegnoSuperPazzoSgravato");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout()); // layout dei brordi per rendere meno storpia la finestra
JPanel ctrl = new JPanel();
JTextField wtf = new JTextField(5);
JTextField htf = new JTextField(5);
JButton btnSq = new JButton("Quadrato");
JButton btnCirc = new JButton("Cerchio");
ctrl.add(new JLabel("Larghezza:"));
ctrl.add(wtf);
ctrl.add(Box.createHorizontalStrut(10)); // Un pochetto di spaziatura per rendere il tutto più leggibile
ctrl.add(new JLabel("Altezza:"));
ctrl.add(htf);
ctrl.add(Box.createHorizontalStrut(20)); // Un pochetto di spaziatura per rendere il tutto più leggibile
ctrl.add(btnSq);
ctrl.add(btnCirc);
f.add(ctrl, BorderLayout.NORTH);
DrawPanel dp = new DrawPanel(wtf, htf);
f.add(dp, BorderLayout.CENTER);
// la freccetta è una lambda per rendere più snello il codice: e -> indica cosa eseguire in caso che quella azione sia verificata
btnSq.addActionListener(e -> dp.setFigura("quadrato"));
btnCirc.addActionListener(e -> dp.setFigura("cerchio"));
f.pack(); //ridimnezionamento
f.setLocationRelativeTo(null);
f.setVisible(true); // Rendere visibile la finestra
}
private static class DrawPanel extends JPanel {
private String prossima = null;
private final JTextField wtf, htf; // Campi di testo di larghezza e altezza
DrawPanel(JTextField wtf, JTextField htf) {
this.wtf = wtf;
this.htf = htf;
setBackground(Color.WHITE); // Sfondo bianco perchè si
// Evento del click click click click click del mouse
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (prossima == null) return; // Non fae niente se l'utentew non ha selezionato la modalità
int w = parseInt(wtf.getText(), 50); // Traforma la stringa ad intero, se è vuota prende 50 come valore
int h = parseInt(htf.getText(), 50); // Traforma la stringa ad intero, se è vuota prende 50 come valore
int x = e.getX(); // x del mouse
int y = e.getY(); // y del mouse
if (prossima.equals("quadrato")) {
addShape(new Rectangle(x, y, w, h)); // fai un rettangolo/quadrato
} else {
addShape(new Ellipse2D.Double(x, y, w, h)); //altrimenti disegna un ellisse
}
prossima = null;
}
});
}
// Modo più carinino per impostrare òla figura
void setFigura(String fig) {
this.prossima = fig;
}
// Forme nella finestra
private final java.util.List<Shape> shapes = new java.util.ArrayList<>();
private void addShape(Shape s) {
shapes.add(s);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // In teoria con l'antialiasing non dovrebbe essere spigolosa la figura
// Imposta il colore delle figrure per capire meglio se si tratta di un cerchio o di un rettangolo
for (Shape s : shapes) {
if (s instanceof Rectangle)
g2.setColor(Color.BLUE);
else
g2.setColor(Color.RED);
g2.fill(s); // ripempie la figura col colorew selezionato
}
} finally {
g2.dispose(); // liberazioniamo le risorse una volta finizionato il disegno della figura
}
}
// Coinvertire una stringa a intero
private static int parseInt(String txt, int defaultVal) {
try {
return Integer.parseInt(txt.trim());
} catch (NumberFormatException e) {
return defaultVal;
}
}
}
}