This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/8/4

Stress with JLabels in a JFrame

Kartoffelbrot Kartoffelbrot

2013/8/4

#
I've made a JFrame subclass, called World. It looks like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;

public class World extends JFrame
{
    private JLabel[][] pixel;
    private int width;
    private int height;

    public World(String title, int width, int height){
        super(title);

        int border = 10;
        this.width = width;
        this.height = height;

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        int frameWidth = width+(2*border); 
        int frameHeight = height+(2*border);
        setSize(frameWidth, frameHeight);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - getSize().width) / 2;
        int y = (d.height - getSize().height) / 2;
        setLocation(x, y);
        setResizable(false);
        Container cp = getContentPane();
        cp.setLayout(null);
        //meins
        pixel = new JLabel[width][height];
        for(int j = 0; j < height; j++){//y
            for(int i = 0; i < width;  i++){//x
                JLabel a = new JLabel();
                a.setBounds(i+border,j+border,1,1);
                cp.add(a);
                pixel[i][j] = a;
                System.out.println("x: "+i+" y: "+j);
            }  
        }

        setVisible(true);

        System.out.println("Done");
    }

    public void randomizeImage(){
        for(int j = 0; j < height; j++)//y
            for(int i = 0; i < width;  i++)//x
                if(pixel[i][j]!=null)
                    pixel[i][j].setBackground(randomColor());
    }

    public Color randomColor(){
        return new Color(help.random(256),help.random(256),
            help.random(256),help.random(256));
    }

    public static void main(String[] args){
        World w = new World("World", 100, 100);
        w.randomizeImage();
    }
}
Help is another class, which has only a method to get random numbers, that works equal to Greenfoot.getRandomNumber. When I use the main method, the JFrame opens and I get this error message: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract! For higher width and higher height it doesn't finishes loading at all. Has anyone knowledge about that?
davmac davmac

2013/8/4

#
Not sure, though if you posted the full stack trace it might help. Also, you need to create your Swing components (i.e. your World) in the AWT Event Dispatch Thread. See https://weblogs.java.net/blog/cayhorstmann/archive/2007/06/the_single_thre.html
Kartoffelbrot Kartoffelbrot

2013/8/4

#
The problem is, that I'm very new to the materia and that I've no idea of all that classes and what a AWT Event Dispatch Thread is. This is the Stack Trace: World.<init>(World.java:44) World.main(World.java:61) __SHELL1.run(__SHELL1.java:6) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:601) bluej.runtime.ExecServer$3.run(ExecServer.java:725)
davmac davmac

2013/8/4

#
The stack trace shows an error in line 44, but the code you pasted above has nothing on that line. I guess you've changed the code. Please post the code together with the stack trace that corresponds to it.
davmac davmac

2013/8/4

#
And I'm sorry to say but until you understand what the Event Dispatch Thread is, you shouldn't use Swing. Read the link I gave you, and the Swing documentation.
Kartoffelbrot Kartoffelbrot

2013/8/4

#
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;
public class World extends JFrame
{
    private JLabel[][] pixel;
    private int width;
    private int height;
    public World(String title, int width, int height){
        super(title);

        int border = 10;
        this.width = width;
        this.height = height;

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        int frameWidth = width+(2*border); 
        int frameHeight = height+(2*border);
        setSize(frameWidth, frameHeight);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - getSize().width) / 2;
        int y = (d.height - getSize().height) / 2;
        setLocation(x, y);
        setResizable(false);
        Container cp = getContentPane();
        cp.setLayout(null);
        //meins
        pixel = new JLabel[width][height];
        for(int j = 0; j < height; j++){//y
            for(int i = 0; i < width;  i++){//x
                JLabel a = new JLabel();
                a.setBounds(i+border,j+border,1,1);
                cp.add(a);
                pixel[i][j] = a;
                System.out.println("x: "+i+" y: "+j);
            }  
        }

        setVisible(true);

        System.out.println("Done");
        
        for ( StackTraceElement trace : new Throwable().getStackTrace() )
            System.out.println( trace );
    }

    public void randomizeImage(){
        for(int j = 0; j < height; j++)//y
            for(int i = 0; i < width;  i++)//x
                if(pixel[i][j]!=null)
                    pixel[i][j].setBackground(randomColor());
    }

    public Color randomColor(){
        return new Color(help.random(256),help.random(256),
            help.random(256),help.random(256));
    }

    public static void main(String[] args){
        World w = new World("World", 100, 100);
        w.randomizeImage();
    }
}
World.<init>(World.java:45) World.main(World.java:62) __SHELL0.run(__SHELL0.java:6) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:601) bluej.runtime.ExecServer$3.run(ExecServer.java:725) I will do.
davmac davmac

2013/8/4

#
You stack trace corresponds to code which specifically prints a stack trace. There is no error.
Kartoffelbrot Kartoffelbrot

2013/8/4

#
I don't know if it helps, but this is the whole error message: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo(TimSort.java:747) at java.util.TimSort.mergeAt(TimSort.java:483) at java.util.TimSort.mergeCollapse(TimSort.java:410) at java.util.TimSort.sort(TimSort.java:214) at java.util.TimSort.sort(TimSort.java:173) at java.util.Arrays.sort(Arrays.java:659) at java.util.Collections.sort(Collections.java:217) at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136) at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110) at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:435) at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166) at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:515) at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:380) at java.awt.Component.dispatchEventImpl(Component.java:4731) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.SequencedEvent.dispatch(SequencedEvent.java:128) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) And no coloured JLabels are in the JFrame.
davmac davmac

2013/8/5

#
Hmm. To be honest, that looks like it might be a Java bug, although I get slightly better results using JPanel instead of JLabel (I still see the exception). Possibly it's a result of having so many components.
You need to login to post a reply.