There has been a bug bothering me for some time now, I believed I managed to code around it, but in a very clumsy way. I have no idea why this bug appears, but it's there, and appear at what seems to be random. I need to get rid of it since it could clog up the entire program.

Why does this illegal state occur? I have tried to code around it by making the frame visible again before trying to recreate the buffers, yet it dosen't really work in this case. This bug is also quite hard to recreate, as I said, it occurs at what seems to be random (yet I know it hardly is random). Please help, I have been trying to get rid of this shit for about 2 months and when I finally thought I succeeded, this things pops up again.

EDIT: The program didn't crash when i encountered this, but in the unlikely event of both of those frame.createBufferStrategy() giving an illegal state this program will crash and the user won't be able to shut it down normally. That's why I need this bug gone.

I think those lines have somthing to do whit it. You try tu create buffer and then it fails you try again but this time in catch block (no try) and there is none to catch new expection. There could be same reason but you are noticed only if second attempt fails.I also suggest to try show frame only all work has beed finished. (after createBufferStrategy but not before)

If the first attempt fails i will get notified becuase i tell it to print a stack trace, then try to recreate the buffer. If i put the other buffer creation command in a try catch block i will have to create an infinite try catch block to solve all this once and for all. What happened in that stack trace was the first buffer creation failed due to an illegalStateException, the code went into the catch section and managed to recreate the buffer without it failing, but since this fail occurs random both of those could have failed and that would leave me with a crashed program that can't be shut down normally.

I've looked around a bit and most sources suggest the command you suggested as well, i'll try it and see if the bug pops up again, if not then you have my thanks ^_^ You have my thanks anyway though since you took the time to help me with this ^_^

Also, I'm a bit ashamed I didn't google the exception first, I guess I learned something new xD

I use a bufferstrategy since it is the technique I usually use, like this basically.

SwingUtilities.invokeLater() runs code on the Event Dispatch Thread. You should do all AWT/Swing widget manipulation on the EDT as a general rule, or you may get strange threading errors.

In your case, the call to createBufferStrategy() is happening before the frame is made "displayable," which is an AWT term meaning "there's a native component ready to display this Java component." A call to pack() will make a window and its children "displayable," for example. In any case, a Window must have a native resource attached to it or createBufferStrategy will throw this exception.

publicstaticvoidmain(String[] args){ //TODO Remember to use the -Dsun.java2d.d3d=false command when executing!!!Mathematicsmath = newMathematics();if(args.length < 1){System.err.println("Please execute with the -Dsun.java2d.d3d=false command");System.err.println("This can be done by typing java -Dsun.java2d.d3d=false -jar \"Filepath here\" in the start.bat script i have provided");System.err.println("For devs: just add the line -Dsun.java2d.d3d=false to the program arguments section in run configuration");try{Thread.sleep(5000); }catch(InterruptedExceptione){e.printStackTrace(); } }else{System.out.println(args[0]);math.init(); }

@ra4king: Ooooh, now i understood how the method works! Or well, i think i have understood it, it tells the EDT to execute the run method after it is done with all it's current tasks, when it ahve told the EDT to executet it later, it moves on and what happened was that my other threads tried to use the bufferstrat before the EDT had created it, is that correct? Your solution worked btw, the program executes normally now Once again, thanks for the help

@Cylab: thanks for the explanation, i'll play around a bit with the code you provided, it is a helper method for scheduling tasks for the EDT, or have i misunderstood it?

As an aside, even though you sound like you're set... If you're not a composition-over-inheritance-at-all-costs zealot*, one easy way to not worry about this stuff is to extend Canvas to create your own drawable component, and override addNotify() to create your BufferStrategy, spawn other threads, etc. addNotify() is called on the EDT when a component is made displayable, so it's a good time to add hooks for stuff like that. Just be sure to call super.addNotify() first-thing in your method override, or you won't be happy.

Why set D3D to false? You WANT d3d on Windows, and OpenGL on everything else

I have used that before, if your not using any hardware acceleration (just software, straight up awt / swing) then setting d3d to false increases performance, of course you wont be happy if you ARE using hardware acceleration (Graphics2D)

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org