gpt4 book ai didi

java - JLabels 随机填充

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:33 25 4
gpt4 key购买 nike

我有一个名为 Cell 的扩展 JLabel 的类:Cell 内部有两个方法,kill()revive()。当一个 Cell 被复活时,它会使用 setBackground() 改变它的颜色。我有一个名为 LifePanel 的 JPanel,其中包含这些单元格的矩阵。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class LifeDriver {
public static void main(String[] args) {
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
JFrame frame = new JFrame("Life");
frame.setSize(950, 800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new MainPanel(80, 100));
frame.setVisible(true);
}
} );
}
}

class MainPanel extends JPanel {
private LifePanel p;
private ControlPanel c;
private JScrollPane pane;

public MainPanel(int ro, int co) {
setLayout(new BorderLayout());
p = new LifePanel(ro, co);
p.setPreferredSize(new Dimension(740, 740));
pane = new JScrollPane(p);
pane.setBorder(null);
pane.setAutoscrolls(true);
add(pane, BorderLayout.CENTER);
c = new ControlPanel(p);
add(c, BorderLayout.NORTH);
}
}

class ControlPanel extends JPanel {
private JButton random;
private LifePanel lp;

public ControlPanel(LifePanel p) {
lp = p;
random = new JButton("Random");
random.addActionListener(new Listener());
add(random);
}

private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double prob = Double.parseDouble(JOptionPane
.showInputDialog("Percentage Probability?"));
lp.randomize(prob);
} catch (Exception f) {
}
}
}
}

class LifePanel extends JPanel {
private Cell[][] grid;
private Timer t;
private static int nR, nC;

public LifePanel(int row, int column) {
nR = row;
nC = column;
setLayout(new GridLayout(nR, nC, 1, 1));
grid = new Cell[nR][nC];
for (int r = 0; r < nR; r++)
for (int c = 0; c < nC; c++) {
grid[r][c] = new Cell();
add(grid[r][c]);
}
}

public void randomize(double percent) {
for (int r = 0; r < nR; r++)
for (int c = 0; c < nC; c++)
if (Math.random() < percent / 100)
grid[r][c].revive();
else
grid[r][c].kill(false);
}
}

class Cell extends JLabel {
private Color deadCellColor = new Color(245, 245, 245);

public Cell() {
this.setBackground(deadCellColor);
this.setOpaque(true);
this.setAlive(false);
}

public void kill(boolean removeCell) {
this.setAlive(false);
}

public void revive() {
this.setAlive(true);
}

public void setAlive(boolean arg) {
if (arg) {
this.changeColor();
} else {
this.setBackground(deadCellColor);
}
}

private void changeColor() {
setBackground(Color.RED);
}
}

当我调用 randomize 时,它​​起作用了(随机设置每个 JLabel 的颜色),但不是我期望的那样。每个标签的颜色不是一次全部出现,而是以随机方式出现(类似于 powerpoint 中的溶解效果?)。颜色甚至不会像 for 循环可能建议的那样逐行、逐列显示。它看起来完全是随机的,即使顺序是预先确定的。关于它为什么这样做有什么想法吗?

最佳答案

要避免在更改单元格状态时进行更新,您可以只更改父面板 LifePanel 的可见性。像这样:

public void randomize(double percent) {
// No updates while we change state
setVisible(false);
for (int r = 0; r < nR; r++) {
for (int c = 0; c < nC; c++) {
if (Math.random() < percent / 100)
grid[r][c].revive();
else
grid[r][c].kill(false);
}
}
// Update now
setVisible(true);
}

我怀疑任何跟踪脏矩形(即需要在屏幕上重新绘制的东西)的东西都没有按照预期的方式对实际的重新绘制更新进行排序。情况似乎是这样,因为在更新所有单元格后要求重新绘制 LifePanel 似乎也如预期的那样工作:

public void randomize(double percent) {
for (int r = 0; r < nR; r++) {
for (int c = 0; c < nC; c++) {
if (Math.random() < percent / 100)
grid[r][c].revive();
else
grid[r][c].kill(false);
}
}
// Force redraw of whole panel
this.repaint();
}

关于java - JLabels 随机填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37381172/

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