Matrix effect funzionante, scie fluide

This commit is contained in:
2026-01-28 19:35:56 +01:00
parent a263079089
commit 762321f027

View File

@@ -1,65 +1,46 @@
package matrix;
import java.awt.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
public class ColonnaLettere extends JPanel {
private final List<Lettera> lettere = new ArrayList<>();
private final int larghezza;
private final int yEnd;
private BufferedImage buffer;
private Graphics2D bufferG;
private float spawnTimer = 0f;
private final float spawnInterval; // tempo tra una lettera e l'altra
private final float dissolveSpeed; // velocità dissolvenza
private final float spawnInterval;
private final float dissolveSpeed;
public ColonnaLettere(int x, int yStart, int yEnd, int larghezza) {
if (yEnd <= yStart) {
throw new IllegalArgumentException("Altezza colonna non valida: " + yEnd);
}
private final LogoMask logo;
private final int logoX = 300;
private final int logoY = 300;
public ColonnaLettere(int x, int yStart, int yEnd, int larghezza, LogoMask logo) {
this.larghezza = larghezza;
this.yEnd = yEnd;
this.logo = logo;
// buffer ARGB per trasparenza
this.buffer = new BufferedImage(
larghezza,
yEnd - yStart,
BufferedImage.TYPE_INT_ARGB
);
setBounds(x, yStart, larghezza, yEnd - yStart);
setOpaque(false); // 🔥 FONDAMENTALE
setBackground(new Color(0,0,0,0)); // trasparente
bufferG = buffer.createGraphics();
bufferG.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON
);
bufferG.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
setBounds(x, yStart, larghezza, yEnd);
setOpaque(false);
spawnInterval = ThreadLocalRandom.current().nextFloat(0.05f, 0.15f);
dissolveSpeed = ThreadLocalRandom.current().nextFloat(0.1f, 0.6f);
dissolveSpeed = ThreadLocalRandom.current().nextFloat(0.4f, 0.9f);
// prima lettera
spawnLettera();
}
// ================= UPDATE =================
public void aggiorna(float deltaTime) {
// Sfuma leggermente il buffer per creare scia trasparente
Graphics2D g2 = (Graphics2D) buffer.getGraphics();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.15f));
g2.setColor(new Color(0,0,0,255)); // trasparente
g2.fillRect(0, 0, buffer.getWidth(), buffer.getHeight());
g2.dispose();
spawnTimer += deltaTime;
if (spawnTimer >= spawnInterval) {
@@ -67,60 +48,30 @@ public class ColonnaLettere extends JPanel {
spawnLettera();
}
for (Lettera l : lettere) {
bufferG.setFont(new Font("Monospaced", Font.PLAIN, l.getDimensione()));
bufferG.setColor(new Color(
0,
255,
70,
Math.round(l.getAlpha() * 255) // alpha dinamico per dissolvenza
));
bufferG.drawString(
String.valueOf(l.getCarattere()),
l.getPosizione().x,
l.getPosizione().y
);
}
for (Iterator<Lettera> it = lettere.iterator(); it.hasNext();) {
Lettera l = it.next();
l.dissolve(dissolveSpeed * deltaTime); // diminuisce alpha
//l.dissolve(dissolveSpeed * deltaTime); // diminuisce alpha
//l.dissolve(dissolveSpeed * deltaTime); // riduce alpha
//l.muovi(deltaTime); // sposta y
if (!l.isViva()) it.remove(); // rimuove quando alpha = 0
l.dissolve(dissolveSpeed * deltaTime);
if (!l.isViva()) it.remove();
}
repaint();
repaint();
}
// ================= SPAWN =================
private void spawnLettera() {
float y = lettere.isEmpty()
? larghezza
: lettere.get(lettere.size() - 1).getPosizione().y + larghezza;
private void spawnLettera() {
float y = lettere.isEmpty()
? larghezza
: lettere.get(lettere.size() - 1).getPosizione().y + larghezza;
if (y > yEnd) return;
if (y > yEnd) return;
char c = Matrix.MATRIX_CHARS[ThreadLocalRandom.current().nextInt(Matrix.MATRIX_CHARS.length)];
Lettera l = new Lettera(0, y, c, larghezza);
l.setAlpha(1f); // piena luminosità
l.setNuova(true); // flag per lettera appena generata
char c = Matrix.MATRIX_CHARS[
ThreadLocalRandom.current().nextInt(Matrix.MATRIX_CHARS.length)
];
lettere.add(l);
}
// ================= STATO =================
public boolean isVuota() {
return lettere.isEmpty();
}
public List<Lettera> getLettere() {
return lettere;
Lettera l = new Lettera(0, y, c, larghezza);
l.setAlpha(1f);
lettere.add(l);
}
// ================= DRAW =================
@@ -130,18 +81,34 @@ public class ColonnaLettere extends JPanel {
Graphics2D g2 = (Graphics2D) g;
for (Lettera l : lettere) {
Color colore;
if (!l.isNuova()) {
colore = new Color(0, 255, 0, Math.round(l.getAlpha() * 255));
} else {
colore = new Color(0, 255, 0);
l.setNuova(false);
int localX = (int) l.getPosizione().x;
int localY = (int) l.getPosizione().y;
int globalX = getX() + localX;
int globalY = getY() + localY;
Color colore = new Color(0, 255, 70, Math.round(l.getAlpha() * 255));
// collisione logo
if (logo != null && logo.isInside(globalX, globalY, logoX, logoY)) {
Color lc = logo.getColor(globalX, globalY, logoX, logoY);
if (lc != null) {
colore = new Color(
lc.getRed(),
lc.getGreen(),
lc.getBlue(),
Math.round(l.getAlpha() * 255)
);
}
}
//Color colore = new Color(0, 255, 70, Math.round(l.getAlpha() * 255));
g2.setColor(colore);
g2.setFont(new Font("Monospaced", Font.PLAIN, l.getDimensione()));
g2.drawString(String.valueOf(l.getCarattere()), l.getPosizione().x, l.getPosizione().y);
g2.drawString(String.valueOf(l.getCarattere()), localX, localY);
}
}
public boolean isVuota() {
return lettere.isEmpty();
}
}