3

I'm doing a simulation of life, I have wolf who eat sheep, sheep eat grass. It's simple but i'm not an expert in JFrame and JPanel stuff. This is my JFrame code:

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 the 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 to find my problem i don't know where is my ClassCastException.

4
  • I believe with BorderLayout, you need to use add(component, position) rather than just add(component). E.g. add(component, BorderLayout.CENTER). Not sure if that has anything to do with it though. Commented Jan 6, 2015 at 21:07
  • When I do add(ca) or add(image) ? Commented Jan 6, 2015 at 21:27
  • i changed in both, there's still CasterException. Commented Jan 6, 2015 at 21:59
  • sorry yes that has nothing to do with it. see my answer Commented Jan 6, 2015 at 22:07

1 Answer 1

5

OK so the problem is rather obscure but is a typical problem with Swing applications: you can do GUI operations only on the Event Dispatch Thread.

This means that you cannot alter your frame (e.g. add/remove panels) in your "main thread" and have to go via the SwingUtilities.invokeAndWait method. If you don't do this then all sorts of strange things can happen such as NullPointerException, ConcurrentModificationException and apparently also the thing you had.

If you change your main() method like this, it will work without the 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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

import java.lang.reflect.InvocationTargetException;
Thank you there's no exception anymore but my program dont work really well. But i will watch this. It's say there's no animals in my simulation but there's sheep and wolf.
You should accept this answer as it addresses the question you asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.