java - 重绘线程在触发业务功能时工作正常,但在引用触发时卡住

标签 java multithreading paint

解释起来有点困难,但情况是这样的:

我正在编写一个细胞自动化程序,并且有一个扩展 JFrame 的 mainScreen 类,并且该 JFrame 包含自定义 Jpanel,它也是一个继续重新绘制自身的线程。主类(App)创建主屏幕。

现在奇怪的是,当我从 App 类调用业务函数(即在 while 循环中涉及单元格)时,它正在工作(各代的实时 View ),但是当我调用相同的函数时从 MainScreen 类(通过键盘输入)然后重新绘制不会发生,但我看到几代人正在参与的控制台输出..并且程序没有响应窗口的关闭十字,在另一种情况下它运行算法时像平常一样关闭。

那么,为什么我的 Jpanel 没有重新绘制? 希望各位(女孩)能够帮忙。

类关系:App<-MainScreen<-MapPanel

应用程序

package Business;

import GUI.MainScreen;
import java.util.Random;

public class App {
    public static final int _CELL_SIZE = 5;
    public static final int _WIN_WIDTH = 800;
    public static final int _WIN_HEIGTH = 600;
    public static final int _HELP_HEIGTH = 72;
    public static final double _LIFE_START = 0.1F;

    private MainScreen _mainScreen;
    private Cell[][] _map;
    private int _generation;
    private Random _random;
    private boolean _running;

public App() {
    _generation = 0;
    _running = false;
    _random = new Random(System.currentTimeMillis());
    newMap();
    _mainScreen = new MainScreen(this);
    //envolveCells();
    //cout();
}

public void envolveCells() {
    _generation = 0;
    _running = true;
    Cell[][] newMap = new Cell[getNumOfRows()][getNumOfCells()];

    while(_running) {
        newMap = new Cell[getNumOfRows()][getNumOfCells()];

        //envolve cells
        for(int row = 0; row < getNumOfRows(); row++) {
            for(int cell = 0; cell < getNumOfCells(); cell++) {
                System.out.println(_map[row][cell].envolve());
                newMap[row][cell] = new Cell(_map[row][cell].envolve());
                newMap[row][cell].setNeighbors(_map[row][cell].getNeighbors());
            }
        }

        _map = newMap;
        _generation++;

        try {
            Thread.sleep(100);
        } catch(Exception ex) { }
    }
}

封装 GUI;

导入 Business.App; 导入 java.awt.event.KeyEvent; 导入 java.awt.event.KeyListener; 导入 javax.swing.JFrame;

公共(public)类MainScreen扩展JFrame实现KeyListener { 私有(private)应用程序_app; 私有(private) map 面板_mapPanel;

public MainScreen(App app){
    _app = app;
    _mapPanel = new MapPanel(app);

    setTitle("Cellular Automaton sandbox - Sven van Zoelen");
    setSize(App._WIN_WIDTH, App._WIN_HEIGTH + App._HELP_HEIGTH);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    add(_mapPanel);
    addKeyListener(this);

    setVisible(true);
}

public void keyTyped(KeyEvent e) {
    if(e.getKeyChar() == 'r') {
        System.out.println("Run mode");
        _app.envolveCells();
    }
}

map 面板

package GUI;

import Business.App;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class MapPanel extends JPanel implements Runnable {
    private App _app;
    private Font _font = new Font("Arial", Font.PLAIN, 10);

public MapPanel(App app) {
    _font = new Font("Arial", Font.PLAIN, 10);
    _app = app;

    new Thread(this).start();
}
....
public void run() {
    while(true) {
        repaint();

        try {
            Thread.sleep(50);
        } catch(Exception ex) { }
    }
  }
}

最佳答案

您的“业务逻辑”看起来应该在与 GUI 不同的线程上运行。因此,不要直接从 GUI 线程(在操作监听器中)调用它,而是将其放在一个新线程中。

只要你的 Action 监听器(或者本例中的键监听器)没有返回,你的 GUI 就不会被绘制,因为绘制与输入处理发生在同一个线程中。

public void keyTyped(KeyEvent e) {
    if(e.getKeyChar() == 'r') {
        new Thread("envolver") { public void run() {
           System.out.println("Run mode");
           _app.envolveCells();
        }).start();
    }
}

关于java - 重绘线程在触发业务功能时工作正常,但在引用触发时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380985/

相关文章:

java - 如何在 spring 中配置 Autowiring

java - 使用 Mockito 检查多个参数的一致性

c++ - wxWidgets wxThread 删除可连接线程

java - 什么会导致 Java ScheduleService 无法运行?

java - JPanel repaint()方法及调试

java - 使用B类对象创建B类对象

java - 计算 MPG 结果出现 java.lang.ArithmeticException : Non terminating decimal expansion; no exact representable decimal result

C++ OpenMP 每个线程从私有(private)结构对象打印

c# - 使用蜡笔着色器在 WPF 表单中绘制线条?

javascript - jquery - 用颜色选择器中的颜色填充 div