gpt4 book ai didi

java - 当我的程序繁忙时无法重新绘制 JPanel

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:29 25 4
gpt4 key购买 nike

我正在尝试将算法可视化。我有一个工作程序,但它真的很脏,我决定在继续之前重新组织它。问题是在算法完成之前它不会再重新绘制。 (我尝试使用重新验证代替/与重新绘制结合使用)

选项菜单:

public class BootScreen extends JPanel implements ActionListener {

GridBagConstraints c = new GridBagConstraints();

JFrame frame = new JFrame();

SpinnerNumberModel arraySizeModel = new SpinnerNumberModel(50, 0, 100000, 1);
SpinnerNumberModel speedModel = new SpinnerNumberModel(20, 0, 10000, 1);
SpinnerNumberModel algSelectModel = new SpinnerNumberModel(1, 1, 5, 1);

JSpinner arraySizeSpinner = new JSpinner(arraySizeModel);
JSpinner speedSpinner = new JSpinner(speedModel);
JSpinner algSelectSpinner = new JSpinner(algSelectModel);

JButton start = new JButton("Start");

BootScreen() {
frame.setTitle("Settings");
frame.setSize(500, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(this);

//just creating the options menu, nothing special here (deleted for simplicity)
//...
}

@Override
public void actionPerformed( ActionEvent e ) {
Algorithm alg = new Algorithm(Integer.parseInt(arraySizeModel.getValue().toString()), Integer.parseInt
(speedModel.getValue().toString()));

switch(Integer.parseInt(algSelectModel.getValue().toString())) {
case 1:
alg.alg1();
break;

case 2:
alg.alg2();
break;

case 3:
alg.alg3();
break;

case 4:
alg.alg4();
break;

case 5:
alg.alg5();
break;
}
}

public static void main(String[] Args) {new BootScreen();}
}

主要算法内容:

public class Algorithm {
int[] A;
GUI gui;
int type;
int[] pointers;
int delay;
int max;

Random r = new Random();

Algorithm( int arraySize, int delaySet) {
A = new int[arraySize];
delay = delaySet;
gui = new GUI(this);
}

void generate(int maxIntSize, int pointersAmount, int typeSet) {
max = maxIntSize;
for( int i = 0; i < A.length; i++ ) {
A[i] = r.nextInt(max);
}

pointers = new int[pointersAmount];
for( int i = 0; i < pointers.length; i++ ) {
pointers[i] = -1;
}
type = typeSet;
}

void step(boolean sleep, int updatePointer, int updatePointerVal) {
pointers[updatePointer] = updatePointerVal;
gui.revalidate();
gui.repaint();
if( sleep ) {
try {
Thread.sleep(delay);
}catch( InterruptedException e ) {
}
}
}

//alg1(), alg2(), .... would be here. They first call generate() and call step() a couple of times. (deleted for simplicity)
}

图形用户界面:

public class GUI extends JPanel {
JFrame frame = new JFrame();
Algorithm alg;

GUI( Algorithm algIn ) {
alg = algIn;

frame.setTitle("Algorithmizer");
frame.setSize(1080, 720);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(this);
}

@Override
protected void paintComponent( Graphics g ) {
super.paintComponent(g);
//drawing alg.A as a bar-graph by looping through it (deleted for simplicity)
}
}

当我将 System.out.println("1") 放在 repaint() 调用之前、将 System.out.println("2") 放在 PaintComponent() 函数中、将 System.out.println("3") 放在 repaint() 调用之后时,它只会打印:131313...我还尝试打印堆栈,但也没有从中得到任何有用的东西。算法完成后,程序会重新绘制,但这对我来说没有用。

最佳答案

也许可以采取这样的方法。这将在事件分派(dispatch)线程之外的工作线程中完成所有计算和工作,然后调用 done() ,您可以在事件分派(dispatch)线程上进行 GUI 更新。

@Override
public void actionPerformed( ActionEvent e ) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
runAlg();
return null
}

@Override
protected void done() {
//any of you gui stuff here
}
}.execute();
}
}

public void runAlg(){
Algorithm alg = new Algorithm(Integer.parseInt(arraySizeModel.getValue().toString()), Integer.parseInt
(speedModel.getValue().toString()));

switch(Integer.parseInt(algSelectModel.getValue().toString())) {
case 1:
alg.alg1();
break;

case 2:
alg.alg2();
break;

case 3:
alg.alg3();
break;

case 4:
alg.alg4();
break;

case 5:
alg.alg5();
break;
}
}

关于java - 当我的程序繁忙时无法重新绘制 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814797/

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