简体   繁体   中英

Java JFrame window is not updating

I've tried a lot of things but I still cannot figure it out. I have a List that contains "entities". I'm going trought all of these and rendering them. This works perfectly fine. The problem is that when I remove one object (entity) in this List from different thread. In this case the square (entity) won't disappear until I resize the window.

private List<Entity> entities = new ArrayList<Entity>();

public void render(Graphics g, ImageObserver obs) { //This function is called from my game loop (few hundred times per second..)
    for (int i = 0; i < entities.size(); i++) {
        entities.get(i).render(); //Calling the render function in the entity class
    }
}

public void removeEntity(int index) { //This function is called from different thread
    entities.remove(index); //Removing entity from the list
}

Your render() function is not being called. You need to call repaint() to do this. This will call render() and update the JFrame. Resizing the window repaints the window automatically, which is why it worked only after you resized it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM