2 Commits
v0.1 ... master

4 changed files with 275 additions and 174 deletions

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 int logoX = 300;
private int logoY = 300;
public ColonnaLettere(int x, int yStart, int yEnd, int larghezza, LogoMask logo) {
this.larghezza = larghezza;
this.yEnd = yEnd;
// 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));
this.logo = logo;
this.logoX=logo.posizione.x;
this.logoY=logo.posizione.y;
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.2f, 0.3f);
// 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,32 @@ 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,17 +83,123 @@ 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();
}
private void drawGlowFromLetter(
Graphics2D g2,
LogoMask logo,
int centerX,
int centerY,
int radius
) {
int r2 = radius * radius;
for (int dy = -radius; dy <= radius; dy++) {
for (int dx = -radius; dx <= radius; dx++) {
int dist2 = dx * dx + dy * dy;
if (dist2 > r2) {
continue;
}
int x = centerX + dx;
int y = centerY + dy;
Color src = logo.sample(x, y);
if (src == null) {
continue;
}
float dist = (float) Math.sqrt(dist2);
float alpha = 1f - (dist / radius); // centro pieno, bordo zero
int a = Math.min(255, (int) (src.getAlpha() * alpha));
Color glow = new Color(
src.getRed(),
src.getGreen(),
src.getBlue(),
a
);
g2.setColor(glow);
g2.fillRect(x, y, 1, 1);
}
}
}
private void disegnaPixelLettera(
Graphics2D g2,
Lettera l,
int globalX,
int globalY
) {
int r = l.getDimensione() / 2;
int r2 = r * r;
for (int dy = -r; dy <= r; dy++) {
for (int dx = -r; dx <= r; dx++) {
int d2 = dx * dx + dy * dy;
if (d2 > r2) {
continue; // fuori dal cerchio
}
int px = globalX + dx;
int py = globalY + dy;
Color lc = logo.getPixelColor(px, py, logoX, logoY);
if (lc == null) {
continue;
}
float dist = (float) Math.sqrt(d2);
float fattore = 1f - (dist / r); // centro pieno → bordo zero
int alpha = (int) (255 * fattore * l.getAlpha());
if (alpha <= 0) {
continue;
}
Color pixel = new Color(
lc.getRed(),
lc.getGreen(),
lc.getBlue(),
alpha
);
g2.setColor(pixel);
g2.fillRect(px, py, 1, 1);
}
}
}

View File

@@ -1,67 +1,112 @@
package matrix;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class LogoMask {
private final BufferedImage mask;
private final BufferedImage glow;
private final int x, y;
private final BufferedImage image;
public final Point posizione;
//private int width;
//private int height;
public LogoMask(BufferedImage logo, int x, int y) {
this.x = x;
this.y = y;
mask = new BufferedImage(
logo.getWidth(),
logo.getHeight(),
BufferedImage.TYPE_INT_ARGB
);
glow = new BufferedImage(
logo.getWidth(),
logo.getHeight(),
BufferedImage.TYPE_INT_ARGB
);
// copia logo → mask
Graphics2D g = mask.createGraphics();
g.drawImage(logo, 0, 0, null);
g.dispose();
public LogoMask(String resourcePath, int percento, Point posizione) {
try {
BufferedImage original = ImageIO.read(new File(resourcePath));
int width = original.getWidth() * percento / 100;
int height = original.getHeight() * percento / 100;
this.image = resize(original, width, height);
this.posizione=posizione;
// this.width = 1000;
// this.height = 1000;
} catch (Exception e) {
throw new RuntimeException("Errore caricamento logo", e);
}
}
// chiamata quando una lettera passa sopra
public void accendiPixel(int worldX, int worldY, int size, Color c) {
int lx = worldX - x;
int ly = worldY - y;
if (lx < 0 || ly < 0 ||
lx >= mask.getWidth() ||
ly >= mask.getHeight()) return;
int alpha = (mask.getRGB(lx, ly) >> 24) & 0xff;
if (alpha == 0) return; // pixel logo trasparente
Graphics2D g = glow.createGraphics();
g.setComposite(AlphaComposite.SrcOver);
g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 200));
g.fillRect(lx, ly, size, size);
g.dispose();
private BufferedImage resize(BufferedImage src, int w, int h) {
BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = out.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(src, 0, 0, w, h, null);
g2.dispose();
return out;
}
// dissolve del logo
public void sfuma() {
Graphics2D g = glow.createGraphics();
g.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.85f)
);
g.setColor(Color.BLACK);
g.fillRect(0, 0, glow.getWidth(), glow.getHeight());
g.dispose();
public int getWidth() {
return image.getWidth();
}
public void draw(Graphics2D g) {
g.drawImage(glow, x, y, null);
public int getHeight() {
return image.getHeight();
}
public boolean isInside(int x, int y, int logoX, int logoY) {
int lx = x - logoX;
int ly = y - logoY;
if (lx < 0 || ly < 0 || lx >= image.getWidth() || ly >= image.getHeight()) {
return false;
}
int alpha = (image.getRGB(lx, ly) >> 24) & 0xff;
return alpha > 10;
}
public Color getColor(int x, int y, int logoX, int logoY) {
int lx = x - logoX;
int ly = y - logoY;
if (lx < 0 || ly < 0 || lx >= image.getWidth() || ly >= image.getHeight()) {
return null;
}
int argb = image.getRGB(lx, ly);
int alpha = (argb >> 24) & 0xff;
if (alpha <= 10) {
return null;
}
return new Color(argb, true);
}
/**
* Ritorna il colore del pixel del logo, oppure null se trasparente o fuori
*/
public Color getPixelColor(int globalX, int globalY, int logoX, int logoY) {
int localX = globalX - logoX;
int localY = globalY - logoY;
if (localX < 0 || localX >= image.getWidth() || localY < 0 || localY >= image.getHeight()) {
return null;
}
int rgb = image.getRGB(localX, localY);
return new Color(rgb, true); // true = ha alpha
}
// public void draw(Graphics2D g, int x, int y) {
// g.drawImage(image, x, y, null);
// }
public BufferedImage getImage() {
return this.image;
}
public Color sample(int x, int y) {
if (x < 0 || y < 0 || x >= image.getWidth() || y >= image.getHeight()) {
return null;
}
int argb = image.getRGB(x, y);
Color c = new Color(argb, true);
return c.getAlpha() == 0 ? null : c;
}
}

View File

@@ -4,15 +4,17 @@
*/
package matrix;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
*
* @author mstc
@@ -24,32 +26,33 @@ public class MatrixForm extends JFrame {
private final int larghezzaMin = 10;
private final int larghezzaMax = 25;
private int maxColonne;
private LogoMask logo;
private long lastTime = System.nanoTime();
public MatrixForm() {
// === FINESTRA ===
getContentPane().setBackground(Color.BLACK);
setLayout(null);
setLayout(null);
setTitle("Matrix");
setBackground(Color.BLACK);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setLayout(null);
logo = new LogoMask("/home/mstc/Scaricati/mondo2.png", 150,new Point(400,100));
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (getHeight() <= 0) return;
if (getHeight() <= 0) {
return;
}
resetColonne();
}
});
setVisible(true);
setVisible(true);
// === TIMER (30 FPS circa) ===
Timer timer = new Timer(33, e -> aggiorna());
@@ -59,7 +62,6 @@ public class MatrixForm extends JFrame {
// =========================
// LOGICA
// =========================
private void aggiorna() {
long now = System.nanoTime();
float deltaTime = (now - lastTime) / 1_000_000_000f;
@@ -83,8 +85,8 @@ public class MatrixForm extends JFrame {
repaint();
}
private void calcolaColonne() {
maxColonne = Math.max(1, getWidth() / larghezzaMin)+100;
private void calcolaColonne() {
maxColonne = Math.max(1, getWidth() / larghezzaMin) + 200;
for (int i = 0; i < maxColonne; i++) {
aggiungiColonna();
@@ -97,32 +99,27 @@ public class MatrixForm extends JFrame {
calcolaColonne();
}
private void aggiungiColonna() {
int h = getHeight();
if (h <= 0) return; // 🔥 salva la vita
int larghezza = ThreadLocalRandom.current()
.nextInt(larghezzaMin, larghezzaMax + 1);
int x = ThreadLocalRandom.current()
.nextInt(0, Math.max(1, getWidth() - larghezza));
ColonnaLettere col = new ColonnaLettere(
x,
0,
h,
larghezza
);
colonne.add(col);
add(col);
}
private void aggiungiColonna() {
int h = getHeight();
if (h <= 0) {
return; // 🔥 salva la vita
}
int larghezza = ThreadLocalRandom.current()
.nextInt(larghezzaMin, larghezzaMax + 1);
int x = ThreadLocalRandom.current()
.nextInt(0, Math.max(1, getWidth() - larghezza));
ColonnaLettere col = new ColonnaLettere(
x,
0,
h,
larghezza, logo
);
colonne.add(col);
add(col);
}
/**
* This method is called from within the constructor to initialize the form.

BIN
src/matrix/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB