gpt4 book ai didi

java - 为什么即使调用了 repaint(),我的 GUI 也不会更新?

转载 作者:行者123 更新时间:2023-12-01 22:42:31 27 4
gpt4 key购买 nike

我在使用 Swing worker 、计时器时遇到了一些困难,而且我实际上有点困惑。据我了解,我必须设置一个计时器来设置 EDT 必须调用的重复任务。

我正在尝试制作一个以图形方式显示排序算法的程序(如下所示:https://www.youtube.com/watch?v=kPRA0W1kECg)

我只是不明白为什么 GUI 不会刷新。我非常确定重绘方法正在被调用,因为我放置了一个向我显示有序值的系统输出,并且它似乎可以工作,但 GUI 只是......没有改变。

这是我的代码:

public class MainWindow {
private JFrame frame;
JPanel panel;
public final static int JFRAME_WIDTH = 800;
public final static int JFRAME_HEIGHT = 600;
public final static int NELEM = 40;

ArrayList<Double> numbers;
ArrayList<myRectangle> drawables = new ArrayList<myRectangle>();
Lock lock = new ReentrantLock();
Condition waitme = lock.newCondition();


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public MainWindow() {
initialize();
}

private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, JFRAME_WIDTH + 20, JFRAME_HEIGHT + 40);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new myPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);

Timer timer = new Timer(500, new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
lock.lock();
try{
//Updating the gui
panel.repaint();
panel.revalidate();
//Giving the OK to the sorting alghoritm to proceed.
waitme.signal();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
});
timer.start();

SwingWorker<Integer, String> sw = new SwingWorker<Integer, String>(){

@Override
protected Integer doInBackground() throws Exception {
mapAndCreate();
bubbleSort();
return null;
}

};
sw.execute();
}

private void bubbleSort() throws InterruptedException{
for(int i=0; i < NELEM; i++){
for(int j=1; j < (NELEM-i); j++){
if(drawables.get(j-1).wid > drawables.get(j).wid){
//swap the elements!
myRectangle temp = drawables.get(j-1);
drawables.set(j-1, drawables.get(j));
drawables.set(j, temp);
lock.lock();
try{
//Wait for the GUI to update.
waitme.await();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
}

}

/***
* Function that maps values from 0 to 1 into the rectangle width.
*/
private void mapAndCreate() {
double max = 0;
numbers = new ArrayList<Double>(NELEM);
//Finding maximum.
for(int i = 0; i < NELEM; i++){
Double currElem = Math.random();
if(currElem > max) max = currElem;
numbers.add(currElem);
}
//Mapping process
int offset = 0;
for(int j = 0; j < NELEM; j++){
Integer mapped = (int) (( JFRAME_WIDTH * numbers.get(j) ) / max);
myRectangle rect = new myRectangle(offset , mapped);
drawables.add(rect);
offset += JFRAME_HEIGHT / NELEM;
}
}

private class myRectangle{

int myy , wid , colorR,colorG,colorB;

public myRectangle(int y , int wid){
this.myy = y;
this.wid = wid;
Random r = new Random();
colorR = r.nextInt(255);
colorG = r.nextInt(255);
colorB = r.nextInt(255);

}
}

private class myPanel extends JPanel{

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(myRectangle rectan : drawables){
Graphics2D graphics2D = (Graphics2D)g;
System.out.println(rectan.wid);
Rectangle2D.Double rect = new Rectangle2D.Double(0,rectan.myy,rectan.wid,JFRAME_HEIGHT / NELEM);
graphics2D.setColor(new Color(rectan.colorR,rectan.colorG,rectan.colorB));
graphics2D.fill(rect);
}
System.out.println("====================================================================================================");
}
}


}

最佳答案

大多数操作系统(或者更确切地说,它们使用的 UI 框架)不支持并发访问。简而言之,您无法同时渲染两个文本字符串。

这就是 Swing 在 UI 线程中运行所有渲染操作的原因。在 UI 线程之外调用渲染函数(例如 paint())可能会导致各种问题。因此,当您执行此操作时,Swing 只会记住“我应该重新绘制”并返回(而不是执行任何实际工作)。这样,Swing 就能保护您,但大多数人更愿意看到带有有用消息的错误。

计时器也总是意味着某处有一个线程在计时器耗尽时执行。这不是Swing 的UI 线程。因此,任何绘画操作都必须用 EventQueue.invokeLater() 或类似的方法包装。

另一个常见的错误是占用 UI 线程(因此不会发生渲染,因为您在那里进行复杂的计算)。这就是 SwingWorker 的用途。同样,在 SwingWorker 的大多数方法中,禁止调用会渲染某些内容的方法(-> 使用 invokeLater())。

所以我的猜测是 UI 线程等待锁,而锁根本没有尽早或足够频繁地解锁。 See this demo如何在 Swing 中制作简单的动画。

public class TimerBasedAnimation extends JPanel implements ActionListener {
public void paint(Graphics g) {
// setup
// do some first-run init stuff
// calculate the next frame
// render frame
}

public void actionPerformed(ActionEvent e) {
repaint();
}

public static void main(String[] args) {
JFrame frame = new JFrame("TimerBasedAnimation");
frame.add(new TimerBasedAnimation());
...
}
}

正如您在代码中看到的那样,没有锁定。相反,您只需将“立即渲染”事件从 actionPerformed 发送到 Swing。一段时间后,Swing 将调用 paint()。无法得知(也无法确保或强制 Swing)这种情况何时发生。

所以好的动画代码会获取当前时间,计算出当时的动画状态,然后进行渲染。所以它不会在M秒内盲目地执行N个阶段。相反,它会针对每一帧进行调整,以产生动画很流畅的错觉,但实际上并非如此。

相关:

关于java - 为什么即使调用了 repaint(),我的 GUI 也不会更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25933331/

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