Matrix effect funzionante, scie fluide, visualizza immagine colorando le lettere pixel per pixel

This commit is contained in:
2026-02-02 01:03:45 +01:00
commit 298705a685
12 changed files with 2520 additions and 0 deletions

73
build.xml Normal file
View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="Matrix2" default="default" basedir=".">
<description>Builds, tests, and runs the project Matrix2.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="Matrix2-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

3
manifest.mf Normal file
View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

1771
nbproject/build-impl.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=64142c2c
build.xml.script.CRC32=ef325a85
build.xml.stylesheet.CRC32=f85dc8f2@1.117.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=64142c2c
nbproject/build-impl.xml.script.CRC32=9c908c72
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.117.0.48

View File

@@ -0,0 +1,95 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/Matrix2.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/Matrix2
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=21
javac.target=21
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=Matrix2
main.class=matrix2.Matrix2
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

15
nbproject/project.xml Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>Matrix2</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

121
src/matrix2/Colonna.java Normal file
View File

@@ -0,0 +1,121 @@
package matrix2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import javax.swing.JPanel;
public class Colonna extends JPanel {
public static final char[] MATRIX_CHARS = (
"ヲァィゥェォャュョッ" +
"アイウエオカキクケコ" +
"サシスセソタチツテト" +
"ナニヌネノハヒフヘホ" +
"マミムメモヤユヨ" +
"ラリルレロワン" +
"01010101010101010101010101010"
).toCharArray();
private final List<Lettera> lettere = new ArrayList<>();
private final int width;
private int nextY = 0; // ← posizione verticale corrente
private final float dissolvenza;
private int maxLetters=0;
private int letteraAggiunte=0;
private int height=0;
private int velocita=5;
private int timeset=2;
private Point startPoint;
private Point endPoint;
private int grandezza_minima_lettera=15;
private int grandezza_massima_lettera=25;
private float dissolve_max= 0.01f;
private float dissolve_min=0.001f;
public Colonna(int pointX, int height) {
//setOpaque(true); // importante per Matrix
//this.setBackground(Color.red);
setOpaque(false);
this.dissolvenza=randomDissolve();
this.width=randomWidth();
this.height=height;
this.maxLetters = height / this.width;
// altezza proporzionata alla larghezza
this.setBounds(pointX, 0, width, height);
// numero massimo di lettere contenibili
this.startPoint = new Point(pointX, 0);
this.endPoint = new Point(pointX + width, height);
addRandomChar();
}
public void addChar(char c) {
//int size = 20; // oppure random
Point p = new Point(0, nextY);
Lettera l = new Lettera(c, this.width, p);
lettere.add(l);
nextY += this.width; // ← distanza coerente con la dimensione
letteraAggiunte++;
repaint();
}
private int randomWidth(){
return (int) (grandezza_minima_lettera + Math.random() * (grandezza_massima_lettera - grandezza_minima_lettera));
}
private float randomDissolve() {
return dissolve_min +
(float) Math.random() * (dissolve_max - dissolve_min);
}
public void addRandomChar(){
char c = MATRIX_CHARS[ThreadLocalRandom.current().nextInt(MATRIX_CHARS.length)];
addChar(c);
}
public int count(){
return lettere.size();
}
public boolean isDead(){
return lettere.isEmpty();
}
public Rectangle getBoundsRect() {
return new Rectangle(startPoint.x, startPoint.y, width, height);
}
public void setHeight(int h){
this.height=h;
this.maxLetters = height / this.width;
}
public List <Lettera> getLettere(){
return lettere;
}
public Lettera getLettera(int p){
return lettere.get(p);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // FONDAMENTALE
for (int i = lettere.size() - 1; i >= 0; i--) {
Lettera l = lettere.get(i);
if (l.isDead()){
lettere.remove(i);
}else{
l.dissolve(dissolvenza);
l.draw((Graphics2D) g);
}
}
if (letteraAggiunte<maxLetters){
timeset--;
if (timeset==0){
addRandomChar();
// addChar((char) this.maxLetters);
timeset=velocita;
}
}
}
}

90
src/matrix2/Lettera.java Normal file
View File

@@ -0,0 +1,90 @@
package matrix2;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Lettera {
private final BufferedImage image;
private final Point position;
private float alpha = 1.0f;
public Lettera(char c, int size, Point position) {
this.position = position;
this.image = creaLetteraImage(c, size, Color.GREEN);
}
private BufferedImage creaLetteraImage(char c, int size, Color color) {
BufferedImage img = new BufferedImage(
size, size,
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON
);
Font font = new Font("Monospaced", Font.BOLD, size);
g2.setFont(font);
g2.setColor(color);
FontMetrics fm = g2.getFontMetrics();
int x = (size - fm.charWidth(c)) / 2;
int y = ((size - fm.getHeight()) / 2) + fm.getAscent();
g2.drawString(String.valueOf(c), x, y);
g2.dispose();
return img;
}
public void draw(Graphics2D g2) {
Composite old = g2.getComposite();
g2.setComposite(
AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha
)
);
g2.drawImage(
image,
(int) position.x,
(int) position.y,
null
);
g2.setComposite(old);
}
// ===== LOGICA =====
public void dissolve(float amount) {
alpha -= amount;
if (alpha < 0f) alpha = 0f;
}
public boolean isDead() {
return alpha <= 0f;
}
public BufferedImage getImage() {
return image;
}
public Point getPosition() {
return position;
}
public Point getPositionGlobal(Point colonnaPos) {
// colonnaPos = posizione (x,y) della colonna sullo schermo
return new Point(colonnaPos.x + this.position.x,
colonnaPos.y + this.position.y);
}
public int getSize() {
return image.getWidth();
}
}

129
src/matrix2/Logo.java Normal file
View File

@@ -0,0 +1,129 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package matrix2;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author mstc
*/
public class Logo {
private BufferedImage image;
private Point startPoint = null;
private Point endPoint = null;
public Logo(String path, Point posizione, int percentuale) {
try {
BufferedImage original = ImageIO.read(new File(path));
if (original != null) {
// calcolo nuove dimensioni in base alla percentuale
int newWidth = original.getWidth() * percentuale / 100;
int newHeight = original.getHeight() * percentuale / 100;
// creo nuova immagine scalata
image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
// abilitare l’anti-aliasing e la qualità
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(original, 0, 0, newWidth, newHeight, null);
g2.dispose();
// aggiorno punti start e end
startPoint = posizione;
endPoint = new Point(startPoint.x + newWidth, startPoint.y + newHeight);
} else {
startPoint = new Point(0,0);
endPoint = new Point(0,0);
}
} catch (IOException ex) {
ex.printStackTrace();
startPoint = new Point(0,0);
endPoint = new Point(0,0);
}
}
private Point localPoint(Point p){
return new Point(p.x-startPoint.x, p.y-startPoint.y);
}
public Color getPixelColor(Point globalPixel) {
if (image == null) return null;
if (globalPixel.x < startPoint.x || globalPixel.y < startPoint.y ||
globalPixel.x >= endPoint.x || globalPixel.y >= endPoint.y) {
return null;
}
int lx = globalPixel.x - startPoint.x;
int ly = globalPixel.y - startPoint.y;
if (lx < 0 || ly < 0 ||
lx >= image.getWidth() || ly >= image.getHeight()) {
return null;
}
int argb = image.getRGB(lx, ly);
Color c = new Color(argb, true);
return (c.getAlpha() == 0) ? null : c;
}
public boolean isLetterInsideLogo(Point letterPos, int letterSize) {
int lx1 = letterPos.x;
int ly1 = letterPos.y;
int lx2 = lx1 + letterSize;
int ly2 = ly1 + letterSize;
int rx1 = startPoint.x;
int ry1 = startPoint.y;
int rx2 = endPoint.x;
int ry2 = endPoint.y;
return lx1 < rx2 && lx2 > rx1 &&
ly1 < ry2 && ly2 > ry1;
}
public Rectangle getBoundsRect() {
return new Rectangle(
startPoint.x,
startPoint.y,
endPoint.x - startPoint.x,
endPoint.y - startPoint.y
);
}
public void modificaLettera(Lettera l, Point colonnaPos){
BufferedImage img = l.getImage();
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int argb = img.getRGB(x, y);
Color lc = new Color(argb, true);
if (lc.getAlpha() == 0) continue;
int globalX = colonnaPos.x + l.getPosition().x + x;
int globalY = colonnaPos.y + l.getPosition().y + y;
Color logoColor = getPixelColor(new Point(globalX, globalY));
if (logoColor != null) {
img.setRGB(x, y, logoColor.getRGB());
}
}
}
}
}

24
src/matrix2/Matrix2.java Normal file
View File

@@ -0,0 +1,24 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package matrix2;
/**
*
* @author mstc
*/
public class Matrix2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MatrixForm form= new MatrixForm();
//form.setBounds(null);
}
}

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="3"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="400" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="300" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Form>

156
src/matrix2/MatrixForm.java Normal file
View File

@@ -0,0 +1,156 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package matrix2;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.Timer;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
/**
*
* @author mstc
*/
public class MatrixForm extends javax.swing.JFrame {
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(MatrixForm.class.getName());
private int Maxcol=500;
private Logo logo;
private List <Colonna> colonne =new ArrayList<>();
/**
* Creates new form MatrixForm
*/
public MatrixForm() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
getContentPane().setBackground(Color.BLACK);
getContentPane().setLayout(null);
// logo= new Logo("/home/mstc/Scaricati/io.jpg",new Point(400,100),75);
logo= new Logo("/home/mstc/Scaricati/mondo2.png",new Point(400,100),200);
initComponents();
// col = new Colonna(30,this.getHeight());
// this.add(col);
// col.addChar('A');
// col.addRandomChar();
// Colonna c = new Colonna();
// this.add(c);
// colonne.add(c);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (getHeight() <= 0) {
return;
}
resetColonne();
}
});
this.setVisible(true);
// === TIMER (30 FPS circa) ===
Timer timer = new Timer(1, e -> aggiorna());
timer.start();
}
private void aggiorna(){
repaint();
if (colonne.size()<Maxcol){
Colonna c = new Colonna(randomPointX(), this.getHeight());
this.add(c);
colonne.add(c);
}
for (int i = colonne.size() - 1; i >= 0; i--) {
if (colonne.get(i).isDead()){
this.remove(colonne.get(i));
colonne.remove(i);
}else{
if (colonne.get(i).getBoundsRect().intersects(logo.getBoundsRect())) {
// la colonna passa sopra al logo
Colonna c = colonne.get(i);
for(Lettera l:c.getLettere()){
if (logo.isLetterInsideLogo(l.getPositionGlobal(c.getLocation()), l.getSize())){
logo.modificaLettera(l,c.getLocation());
}
}
}
}
}
}
private int randomPointX(){
int min=0;
int max=this.getWidth();
return (int) (min + Math.random() * (max - min));
}
private void resetColonne() {
// colonne.forEach(this::remove);
// colonne.clear();
// calcolaColonne();
for (Colonna col:colonne){
col.setSize(col.getWidth(), this.getHeight());
col.setHeight(this.getHeight());
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ReflectiveOperationException | javax.swing.UnsupportedLookAndFeelException ex) {
logger.log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> new MatrixForm().setVisible(true));
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}