diff --git a/src/matrix/ColonnaLettere.java b/src/matrix/ColonnaLettere.java index a73ad73..690417e 100644 --- a/src/matrix/ColonnaLettere.java +++ b/src/matrix/ColonnaLettere.java @@ -1,6 +1,5 @@ package matrix; - import java.awt.Color; import java.awt.Font; import java.awt.Graphics; @@ -22,19 +21,20 @@ public class ColonnaLettere extends JPanel { private final float dissolveSpeed; private final LogoMask logo; - private final int logoX = 300; - private final int logoY = 300; + 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; 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.4f, 0.9f); + dissolveSpeed = ThreadLocalRandom.current().nextFloat(0.2f, 0.3f); spawnLettera(); } @@ -51,7 +51,9 @@ public class ColonnaLettere extends JPanel { for (Iterator it = lettere.iterator(); it.hasNext();) { Lettera l = it.next(); l.dissolve(dissolveSpeed * deltaTime); - if (!l.isViva()) it.remove(); + if (!l.isViva()) { + it.remove(); + } } repaint(); @@ -63,11 +65,11 @@ public class ColonnaLettere extends JPanel { ? 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) - ]; + char c = Matrix.MATRIX_CHARS[ThreadLocalRandom.current().nextInt(Matrix.MATRIX_CHARS.length)]; Lettera l = new Lettera(0, y, c, larghezza); l.setAlpha(1f); @@ -105,10 +107,100 @@ public class ColonnaLettere extends JPanel { g2.setColor(colore); g2.setFont(new Font("Monospaced", Font.PLAIN, l.getDimensione())); 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); + } + } + } + } diff --git a/src/matrix/LogoMask.java b/src/matrix/LogoMask.java index 8e37e5c..b42b527 100644 --- a/src/matrix/LogoMask.java +++ b/src/matrix/LogoMask.java @@ -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; + } + } diff --git a/src/matrix/MatrixForm.java b/src/matrix/MatrixForm.java index c066bf6..0ee030e 100644 --- a/src/matrix/MatrixForm.java +++ b/src/matrix/MatrixForm.java @@ -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. diff --git a/src/matrix/logo.png b/src/matrix/logo.png new file mode 100644 index 0000000..73147f7 Binary files /dev/null and b/src/matrix/logo.png differ