Java下降矩阵码(像电影一样,继续)

时间:2011-01-17 06:51:53

标签: java

好的,所以昨天我发布了一个关于创建一个java jframe的问题,模拟电影中的矩阵雨,我希望就像这个php示例

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

但我希望得到一些帮助,

第一次有一个以上的列落在一起 第二个字符落后于对方

到目前为止这是我的代码

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 个答案:

答案 0 :(得分:5)

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();
  }
}

答案 1 :(得分:2)

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 :(得分:0)

我认为你应该为每个列下拉动画生成一个自己的线程(当然你需要注意使用invokeLater以便与EDT同步)。只需以随机间隔启动这些线程(但要注意一列没有两个活动线程)

答案 3 :(得分:0)

您之前没有在屏幕上看到所有字符的原因是因为您一次创建一个字符,将其一直向下移动,然后创建下一个字符。相反,您需要首先初始化所有角色,然后再一起向下移动。我已将您的代码调整为:

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) {
            }
        }
    }
}