Nuovi progetti

This commit is contained in:
theitaliandeveloper
2025-11-13 09:24:57 +01:00
parent 4ca42b835c
commit 3054032532
14 changed files with 451 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class CerchioTrascinabile extends JFrame {
public CerchioTrascinabile() {
setTitle("Cerchio Trascinabile");
setSize(600, 500);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
PannelloCerchio pannello = new PannelloCerchio();
JButton btnCancella = new JButton("Cancella");
btnCancella.addActionListener(e -> pannello.cancellaCerchio());
JPanel bottomPanel = new JPanel();
bottomPanel.add(btnCancella);
setLayout(new BorderLayout());
add(pannello, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new CerchioTrascinabile().setVisible(true);
});
}
}
class PannelloCerchio extends JPanel {
private Cerchio cerchio;
private boolean dragging = false;
public PannelloCerchio() {
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (cerchio == null) {
cerchio = new Cerchio(e.getX(), e.getY());
repaint();
} else if (cerchio.contiene(e.getX(), e.getY())) {
dragging = true;
}
}
@Override
public void mouseReleased(MouseEvent e) {
dragging = false;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (dragging && cerchio != null) {
cerchio.muovi(e.getX(), e.getY());
repaint();
}
}
});
}
public void cancellaCerchio() {
cerchio = null;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (cerchio != null) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
cerchio.disegna(g2d);
}
}
}
class Cerchio {
private int x, y;
private static final int RAGGIO = 40;
public Cerchio(int x, int y) {
this.x = x;
this.y = y;
}
public void muovi(int nuovoX, int nuovoY) {
this.x = nuovoX;
this.y = nuovoY;
}
public boolean contiene(int px, int py) {
int dx = px - x;
int dy = py - y;
return dx * dx + dy * dy <= RAGGIO * RAGGIO;
}
public void disegna(Graphics2D g2d) {
g2d.setColor(new Color(70, 130, 180));
g2d.fillOval(x - RAGGIO, y - RAGGIO, RAGGIO * 2, RAGGIO * 2);
g2d.setColor(new Color(30, 90, 140));
g2d.setStroke(new BasicStroke(2));
g2d.drawOval(x - RAGGIO, y - RAGGIO, RAGGIO * 2, RAGGIO * 2);
}
}