Exception on thread "AWT-EventQueue-0" java.lang.ClassCastException in my JFrame class

I am doing a simulation of life, I have a wolf that eats sheep, sheep eat grass. It's simple, but I'm not an expert in materials JFrame

and JPanel

. This is my code JFrame

:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class InterfaceGraphique extends JFrame{
private Case[][] caseMemoire = new Case[16][16];

public InterfaceGraphique(){
    setSize(800, 825);
    setTitle("Evolution");
    setLocationRelativeTo(null);
    Container c = getContentPane();
    c.setLayout(null);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    for(int i = 0; i<16; i++){
        for(int j = 0; j<16; j++){
            Case ca = new Case(i*50, j*50); 
            caseMemoire[i][j] = ca;
            add(ca);
        }
    }
    setVisible(true);
}

public Case getCase(int i, int j){
    return caseMemoire[i][j];
}

public class Case extends JPanel{
    private int coordX;
    private int coordY;
    private JLabel image;

    public Case(int x, int y){
        coordX = x;
        coordY = y;
        setBounds(coordX, coordY, 55, 55);
        image = new JLabel(new ImageIcon("../images/Grass.png"));
        this.setLayout(new BorderLayout());
        this.add(image);
    }

    public void setImg(int i){
        switch(i){
            case 0:
                this.remove(image);
                image = new JLabel( new ImageIcon("../images/Grass.png"));
                this.setLayout(new BorderLayout());
                this.add(image);
                break;
            case 1:
                this.remove(image);
                break;
            case 2:
                this.remove(image);
                image = new JLabel( new ImageIcon("../images/Wolf.png"));
                this.setLayout(new BorderLayout());
                this.add(image);
                break;
            case 3:
                this.remove(image);
                image = new JLabel( new ImageIcon("../images/Sheep.png"));
                this.setLayout(new BorderLayout());
                this.add(image);
                break;
        }
    }

    public JLabel getImage(){
        return image;
    }
}

public void afficheUniv(Univers u){
    for (int i=0; i < u.getLignes(); i++){
        for (int j=0; j < u.getColones(); j++) {
            if(u.getCellule(i, j).isLoup())
                caseMemoire[i][j].setImg(2);
            else if(u.getCellule(i, j).isMouton())
                caseMemoire[i][j].setImg(3);
            else if(u.getCellule(i, j).isHerbe())
                caseMemoire[i][j].setImg(0);
            else if(u.getCellule(i, j).isSels())
                caseMemoire[i][j].setImg(1);
            else
                caseMemoire[i][j].setImg(1);
        }
    }

}

public static void main(String[] args){
    Univers u = new Univers (16, 16, 5, 5);
    InterfaceGraphique evolution = new InterfaceGraphique();
    while(u.getNbMoutons()+u.getNbLoups() > 0){
        u.simulation();
        evolution.afficheUniv(u);
        evolution.setVisible(true);
    }
    JOptionPane.showMessageDialog(null,
                    "Il n'y a plus d'animaux en vie !",
                    "Fin",
                    JOptionPane.PLAIN_MESSAGE);
}

      

}

Compilation javac Interfacegraphique.java

works fine. But when I run my program ( java InterfaceGraphique

) I have an error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(LayoutComparator.java:75)
at javax.swing.LayoutComparator.compare(LayoutComparator.java:42)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356)
at java.util.TimSort.sort(TimSort.java:230)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.ArrayList.sort(ArrayList.java:1454)
at java.util.Collections.sort(Collections.java:175)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:445)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:535)
at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:420)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

      

Can you help me find my problem, I don't know where mine is ClassCastException

.

+3


source to share


1 answer


OK, so the problem is rather obscure, but a typical problem in Swing applications: you can only perform GUI operations on the Dispatch Event thread.

This means that you cannot change your frame (like add / remove panels) to your "main thread" and go through the method SwingUtilities.invokeAndWait

. If you don't, all sorts of weird things can happen, such as NullPointerException

, ConcurrentModificationException

and presumably what you had.



If you change your method main()

like this, it works without exception:

public static void main(String[] args) throws InvocationTargetException, InterruptedException {
    final Univers u = new Univers(16, 16, 5, 5);
    final InterfaceGraphique evolution = new InterfaceGraphique();
    while (u.getNbMoutons() + u.getNbLoups() > 0) {
        u.simulation();
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                evolution.afficheUniv(u);
                evolution.setVisible(true);
            }
        });
    }
    JOptionPane.showMessageDialog(null, "Il n'y a plus d'animaux en vie !", "Fin",
            JOptionPane.PLAIN_MESSAGE);
}

      

+4


source







All Articles