java Java下降矩阵代码(如电影,续)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4710693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java falling matrix code (like the movie, continued)
提问by java
ok, so yesterday i posted a question in regards to creating a java jframe that simulates the matrix rain from the movies which i want to be just like this php example
好的,所以昨天我发布了一个关于创建一个 java jframe 的问题,它模拟电影中的矩阵雨,我想像这个 php 示例一样
http://mgccl.com/2007/03/30/simple-version-matrix-like-animated-dropping-character-effect-in-php
http://mgccl.com/2007/03/30/simple-version-matrix-like-animated-dropping-character-effect-in-php
but i would like some help with two things,
但我想在两件事上得到帮助,
1st have more than one column falling at a time and 2nd having the characters trailing behind each other
第 1 次有多个列同时下降,第 2 次有字符相互拖后
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();
}
}
回答by javaDude
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();
}
}
回答by ran632
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:
绘图.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;
}
}
}
回答by Landei
I think you should spawn an own thread for every column drop animation (of course you need to take care to use invokeLater in order to synch with the EDT). Just start these threads in random intervals (but be careful that you don't have two active threads for one column)
我认为您应该为每个列放置动画生成一个自己的线程(当然您需要注意使用 invokeLater 以便与 EDT 同步)。只需以随机间隔启动这些线程(但请注意,一列没有两个活动线程)
回答by dogbane
The reason why you weren't seeing all characters on screen at once was because you were creating one character at a time, moving it all the way down, and then creating the next character. Instead, you need to initialise all your characters first, then move then down together. I have adjusted your code to:
您没有一次在屏幕上看到所有角色的原因是因为您一次创建一个角色,将其一直向下移动,然后创建下一个角色。相反,您需要先初始化所有角色,然后一起向下移动。我已将您的代码调整为:
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) {
}
}
}
}