gpt4 book ai didi

Java下降矩阵代码(如电影,续)

转载 作者:搜寻专家 更新时间:2023-11-01 01:57:39 26 4
gpt4 key购买 nike

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

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

但是我想在两件事上得到一些帮助,

1st 一次有不止一列下降并且第 2 个字符相互拖尾

这是我目前的代码

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

最佳答案

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

关于Java下降矩阵代码(如电影,续),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com