Java falling matrix code (e.g. movie, continued)

ok, so yesterday I posted a question about creating a java-jframe that mimics matrix rain from movies that I want to be like this php example

http://mgccl.com/2007/03/30/simple-version-matrix-like-animated-dropping-character-effect-in-php

but I would like to help with two things,

First of all, more than one column of the 2nd falls out, having characters coming one after another.

this is my code so far

import java.awt.*; import java.util.*; import javax.swing.*; @SuppressWarnings("serial") public class MainFrame extends JFrame { private static final int FONT_SIZE = 20; private static final int NUMBER_OF_REPEATS = 5; private static final String TEXT = new String("0123456789/*-+/<>?;:[] ~!@ #$%^&*()+=abcdefghijklmnopqrstuvwxyz"); private static JPanel panel = new JPanel(null); private static Random random = new Random(); private static JLabel label[] = new JLabel[NUMBER_OF_REPEATS]; public MainFrame() { this.add(panel); panel.setBackground(Color.BLACK); } public void scroll() { for (int i = 0; i < NUMBER_OF_REPEATS; i++) { int character_initial = random.nextInt(TEXT.length()); int random_x = random.nextInt(panel.getWidth() / FONT_SIZE) - 1; int colour = 255; label[i] = new JLabel(""+TEXT.charAt(character_initial)); panel.add(label[i]); label[i].setFont(new Font("monospaced", Font.PLAIN, FONT_SIZE)); label[i].setForeground(new Color(0, 255, 0)); //change the text of the labels and their position for (int j = 0; j < (panel.getHeight() / FONT_SIZE)*2; j++) { int character = random.nextInt(TEXT.length()); label[i].setBounds(random_x*FONT_SIZE, j*(FONT_SIZE / 2), FONT_SIZE, FONT_SIZE); label[i].setText(""+TEXT.charAt(character)); //if foreground colour < 255 catch exception try { //when text reaches a certain colour remove it label[i].setForeground(new Color(0, 255-(j*5), 0)); colour = 255-(j*5); if (colour <= 80) { panel.remove(label[i]); repaint(); colour = 255; j = (panel.getHeight() / FONT_SIZE)*2; } } catch (Exception e) {} //pause between each character try { Thread.sleep(75); } catch (Exception e) {} } //create an infinite loop if (i == NUMBER_OF_REPEATS - 1) { i = 0; } } } public static void main(String[] args) { MainFrame frame = new MainFrame(); frame.setVisible(true); frame.setSize(600, 400); frame.setResizable(false); frame.setMinimumSize(new Dimension(300, 200)); frame.setLocationRelativeTo(null); frame.setTitle("Matrix Code Emulator by Ricco"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.scroll(); } } 
+4
source share
4 answers
 import java.awt.*; import java.util.*; import javax.swing.*; @SuppressWarnings("serial") public class matrixRain extends JFrame { private static final int FONT_SIZE = 20; private static final int NUMBER_OF_REPEATS = 5; private static final String TEXT = new String("あ γŸγ‚’ γ‚« γ‚΅ γ‚Ά γ‚Έγ‚Ί γ‚Ό γ‚Ύ γ‚· γ‚Ή γ‚» γ‚½ γ‚­ γ‚― γ‚± γ‚³ γ‚€ ウ エ γ‚ͺ ジャ γͺ"); private static JPanel panel = new JPanel(null); private static Random random = new Random(); private static JLabel label[] = new JLabel[NUMBER_OF_REPEATS]; public matrixRain() { this.add(panel); panel.setBackground(Color.BLACK); } public void scroll() { //array to hold x coordinates for the labels int[] random_x = new int[NUMBER_OF_REPEATS]; //create an infinite loop while (true) { //initialise all the labels to random characters for (int i = 0; i < NUMBER_OF_REPEATS; i++) { int character_initial = random.nextInt(TEXT.length()); random_x[i] = random.nextInt(panel.getWidth() / FONT_SIZE) - 1; label[i] = new JLabel("" + TEXT.charAt(character_initial)); panel.add(label[i]); label[i].setFont(new Font("monospaced", Font.PLAIN, FONT_SIZE)); label[i].setForeground(new Color(0, 255, 0)); } // change the text of the labels and their position for (int j = 0; j < (panel.getHeight() / FONT_SIZE) * 2; j++) { int character = random.nextInt(TEXT.length()); //move each character for (int i = 0; i < NUMBER_OF_REPEATS; i++) { label[i].setBounds(random_x[i] * FONT_SIZE, j * (FONT_SIZE / 2), FONT_SIZE, FONT_SIZE); label[i].setText("" + TEXT.charAt(character)); label[i].setForeground(new Color(0, 255 - (j * 5), 0)); for (int k = 0; k < NUMBER_OF_REPEATS; k++) { int character_initial = random.nextInt(TEXT.length()); random_x[k] = random.nextInt(panel.getWidth() / FONT_SIZE) - 1; label[k] = new JLabel("" + TEXT.charAt(character_initial)); panel.add(label[k]); label[k].setFont(new Font("monospaced", Font.PLAIN, FONT_SIZE)); label[k].setForeground(new Color(0, 255, 0)); Color colour = label[k].getForeground(); if (colour.getGreen() <= 80) { panel.remove(label[k]); k = (panel.getHeight() / FONT_SIZE) * 2; } } } // pause between each character try { Thread.sleep(15); } catch (Exception e) { } } } } public static void main(String[] args) { matrixRain frame = new matrixRain(); frame.setVisible(true); frame.setSize(600, 400); frame.setResizable(false); frame.setMinimumSize(new Dimension(300, 200)); frame.setLocationRelativeTo(null); frame.setTitle("Matrix Code Emulator by Ricco"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.scroll(); } } 
+5
source

Main.java

 import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class Main extends JPanel{ public static void main(String[] args) { JFrame jf = new JFrame("Matrix raining code - by Ran Galili"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(700,700); jf.setResizable(false); jf.add(new Drawing()); jf.setVisible(true); } } 

Drawing.java:

 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import javax.swing.JPanel; @SuppressWarnings("serial") public class Drawing extends JPanel{ int FONTSIZE = 32, SCREENSIZE = 700; thread1[] thArr = new thread1[SCREENSIZE/FONTSIZE]; Drawing(){ for (int i = 0; i < thArr.length; i++) { thArr[i] = new thread1(i*FONTSIZE); } } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D)g; g.fillRect(0, 0, SCREENSIZE, SCREENSIZE); g.setColor(Color.BLACK); Font font = new Font("Monospaced", Font.BOLD, FONTSIZE); g2.setFont(font); for (int i = 0; i < thArr.length; i++) { if(thArr[i].y > 700){ thArr[i] = new thread1(i*FONTSIZE); } drawThread(g2,thArr[i]); } try{Thread.sleep(30);}catch(Exception ex){} repaint(); } public void drawThread(Graphics2D g2, thread1 th){ int fontsize = g2.getFont().getSize(); for (int i = 0; i < th.len; i++) { if(th.randInt(0, th.len) == i) th.chArr[i][0] = th.randChar(); if(i == th.len-1) g2.setColor(Color.WHITE); else g2.setColor(Color.GREEN); g2.drawChars(th.chArr[i] ,0 ,1 ,th.x , th.y + (i*fontsize)); } th.y+=th.vel; } public class thread1{ int vel, len, x, y, yBottom; char[][] chArr; thread1(int x){ this.x = x; len = randInt(5,30); chArr = new char[len][1]; chArr = populateArrWithChars(chArr); vel = randInt(1,5); this.y = (-1)*len*FONTSIZE; } public char[][] populateArrWithChars(char[][] arr){ for (int i = 0; i < arr.length; i++) { arr[i][0] = randChar(); } return arr; } public char randChar(){ final String alphabet = "0123456789QWERTYUIOPASDFGHJKLZXCVBNM"; final int N = alphabet.length(); Random r = new Random(); return alphabet.charAt(r.nextInt(N)); } public int randInt(int min, int max) { Random rand = new Random(); int randomNum = rand.nextInt((max - min) + 1) + min; return randomNum; } } } 
+2
source

I think you should create your own stream for each flush animation (of course, you need to take care to use invokeLater to synchronize with EDT). Just run these threads at random intervals (but be careful that you don't have two active threads for the same column)

0
source

The reason you didn't see all the characters on the screen at once was because you created one character at a time, moving it all the way down, and then creating the next character. Instead, you need to initialize all your characters first, and then move them down. I adjusted your code:

 public void scroll() { //array to hold x coordinates for the labels int[] random_x = new int[NUMBER_OF_REPEATS]; //create an infinite loop while (true) { //initialise all the labels to random characters for (int i = 0; i < NUMBER_OF_REPEATS; i++) { int character_initial = random.nextInt(TEXT.length()); random_x[i] = random.nextInt(panel.getWidth() / FONT_SIZE) - 1; label[i] = new JLabel("" + TEXT.charAt(character_initial)); panel.add(label[i]); label[i].setFont(new Font("monospaced", Font.PLAIN, FONT_SIZE)); label[i].setForeground(new Color(0, 255, 0)); } // change the text of the labels and their position for (int j = 0; j < (panel.getHeight() / FONT_SIZE) * 2; j++) { int character = random.nextInt(TEXT.length()); //move each character for (int i = 0; i < NUMBER_OF_REPEATS; i++) { label[i].setBounds(random_x[i] * FONT_SIZE, j * (FONT_SIZE / 2), FONT_SIZE, FONT_SIZE); label[i].setText("" + TEXT.charAt(character)); label[i].setForeground(new Color(0, 255 - (j * 5), 0)); Color colour = label[i].getForeground(); if (colour.getGreen() <= 80) { panel.remove(label[i]); j = (panel.getHeight() / FONT_SIZE) * 2; } } // pause between each character try { Thread.sleep(75); } catch (Exception e) { } } } } 
0
source

Source: https://habr.com/ru/post/1335802/


All Articles