IllegalStateException, I can't get rid of it!

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.

This is what happens

java.lang.IllegalStateException: Buffers have not been created
	at sun.awt.windows.WComponentPeer.getBackBuffer(Unknown Source)
	at java.awt.Component$FlipBufferStrategy.getBackBuffer(Unknown Source)
	at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Unknown Source)
	at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
	at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
	at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
	at java.awt.Component.createBufferStrategy(Unknown Source)
	at java.awt.Window.createBufferStrategy(Unknown Source)
	at java.awt.Component.createBufferStrategy(Unknown Source)
	at java.awt.Window.createBufferStrategy(Unknown Source)
	at com.yumeprojects.mathematics.GraphicsEngine.initFrameAndStuff(GraphicsEngine.java:149)
	at com.yumeprojects.mathematics.Mathematics.init(Mathematics.java:76)
	at com.yumeprojects.mathematics.Mathematics.main(Mathematics.java:56)

This is the method the exception occurs in and is catched in

public void initFrameAndStuff(){
		frame = new JFrame("Project Mathematics");
		frame.setSize(800, 600);
		frame.setResizable(true);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(windowListener);
		frame.setVisible(true);
		try{
			frame.createBufferStrategy(2);
		}catch(IllegalStateException e){
			e.getMessage();
			e.printStackTrace();
			frame.setVisible(true);
			frame.createBufferStrategy(2);
		}finally{
			strat = frame.getBufferStrategy();
			math.startThreads();
		}
	}

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.

//Kurten


frame.setVisible(true);
try{
            frame.createBufferStrategy(2);
        }catch(IllegalStateException e){
            e.getMessage();
            e.printStackTrace();
            frame.setVisible(true);
            frame.createBufferStrategy(2);
        }


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.

you could try to google this expection :smiley:
try running with “-Dsun.java2d.d3d=false”.

Stupid question :smiley: why you need a new bufferstrategy swing already have one.

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.

@Override
	public void run(){
		looping = true;
		while(looping){
			renderReInit();
			render();
			renderDispose();
			renderUpdate();
			try{
				Thread.sleep(20);
			}catch(InterruptedException e){
				System.out.println("Skiten kraschade");
				e.printStackTrace();
			}
		}
	}
	
	public void renderReInit(){
		try{
			g2d = (Graphics2D)strat.getDrawGraphics();
		}catch(IllegalStateException e){
			System.err.println("Buffer failed to recreate");
			strat = frame.getBufferStrategy();
			g2d = (Graphics2D)strat.getDrawGraphics();
			System.err.println("Buffer recreated");
		}
	}
	
	public void render(){
		g2d.setColor(bgColor);
		g2d.fillRect(0, 0, 800, 600);
		g2d.drawImage(bgImage[bgIndex], 0, 0, null);
		g2d.setColor(drawColor);
		g2d.drawString("Choice: " + math.choice, 20, 50);
		g2d.drawString("Unit: " + math.unit, 20, 70);
		
		g2d.drawString("Dimensions on frame: 800x600 pix", 50, 300);
		g2d.drawString("Button dimensions: " + geometrySectionButtons[0].getWidth() + "x" + geometrySectionButtons[0].getHeight() + " pix", 50, 320);
		
		if(drawMainMenu){
			for(int i = 0; i < mainMenuButtons.length; i++){
				mainMenuButtons[i].draw(g2d);
			}
			switchBackgroundButton.draw(g2d);
		}else if(drawAlgebraChoices){
			for(int i = 0; i < algebraSectionButtons.length; i++){
				algebraSectionButtons[i].draw(g2d);
			}
		}else if(drawGeometryChoices){
			for(int i = 0; i < geometrySectionButtons.length; i++){
				geometrySectionButtons[i].draw(g2d);
			}
		}
		
		if(database.drawImage){
			if(jimImageIndex < jimImage.length){
				g2d.drawImage(jimImage[jimImageIndex], 200, 300, null);
			}else{
				g2d.drawImage(jimImage[0], 20, 300, null);
			}
		}
		
		if(drawAlgebraChoices){
			g2d.drawString("Algebra! :D", 100, 100);
		}else if(drawGeometryChoices){
			g2d.drawString("Geometry! :D", 100, 100);
		}else if(drawArithmeticChoice){
			g2d.drawString("Arithmetic! :D", 100, 100);
		}
		
		if(!strat.contentsLost()){
			strat.show();
		}else{
			System.err.println("Buffer contents lost");
			System.err.println("Calm down though, this shit ain't serious");
			System.err.println("Just continue chillin', the program will handle it");
		}
	}
	
	public void renderDispose(){
		g2d.dispose();
	}
	
	public void renderUpdate(){
		database.update();
	}

You are getting the exception because of the timing between opening the frame and creating the BufferStrategy.
Try creating the BufferStrategy on the EDT:


SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        frame.createBufferStrategy(3);
    }
});

Ok, im a bit lost now ra4king, i have never seen such a code statement before, what does it do and what exactly are you suggesting i’d do to solve this issue?

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.

Thank you for the explanation, i’ll play around a bit and see if i can get this right :slight_smile:

Sorry for double post, but i can’t get this to work,

The code now looks like this but i get a null pointer exception when i try to get a graphics object from the buffer, it’s almost like the buffer never gets created, even though the code gets executed.

public void initFrameAndStuff(){
		frame = new JFrame("Project Mathematics");
		frame.setSize(800, 600);
		frame.setResizable(true);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(windowListener);
		frame.setVisible(true);
		try{
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					System.out.println("Check");
					frame.createBufferStrategy(2);
				}
			});
			//frame.createBufferStrategy(2);
		}catch(IllegalStateException e){
			e.getMessage();
			e.printStackTrace();
			frame.setVisible(true);
			frame.createBufferStrategy(2);
		}finally{
			strat = frame.getBufferStrategy();
			math.startThreads();
		}
	}

That’s because you need to get the BufferStrategy after it is created, so move the code “strat = frame.getBufferStrategy()” to the line after “frame.createBufferStrategy(2)”

still doesn’t work, even though the code gets called =/ When exactly does it call this swingUtilities method? Because it doesn’t seem to be called in chronical order


import java.awt.Color;
import java.awt.Image;
import com.yumeprojects.jimDB.JimDB;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * Graphics engine class for the program, handles rendering
 * @author Kurten
 * @version 0.42 

 * Last updated: 2011-11-08
 */
public class GraphicsEngine implements Runnable{
	
	protected boolean looping;
	
	private BufferStrategy strat;
	private Graphics2D g2d;
	private JFrame frame;
	private Mathematics math;
	
	private Image bgImage[];
	private int bgIndex;
	
	protected Color bgColor = Color.BLACK;
	protected Color drawColor = Color.RED;
	
	protected boolean drawAlgebraChoices, drawGeometryChoices, drawArithmeticChoice, drawMainMenu;
	
	
	
	protected Button[] mainMenuButtons = {
			new Button("Algebra", "/images/Buttondark.png", "/images/theButton3.png", 100, 50),
			new Button("Geometry", "/images/Buttondark.png", "/images/theButton3.png", 205, 50),
			new Button("Arithmetic", "/images/Buttondark.png", "/images/theButton3.png", 310, 50),
			new Button("Statistics", "/images/Buttondark.png", "/images/theButton3.png", 415, 50),
			new Button("Probability", "/images/Buttondark.png", "/images/theButton3.png", 520, 50)
	};
	protected Button[] algebraSectionButtons = {
			new Button("Linear Equations" , "/images/Buttondark.png", "/images/theButton3.png", 100,50),
			new Button("System of equations", "/images/Buttondark.png", "/images/theButton3.png", 205, 50),
			new Button("Variables", "/images/Buttondark.png", "/images/theButton3.png", 310, 50)
	};
	protected Button[] geometrySectionButtons = {
		new Button("Square area and volume", "/images/kvadrat volym och area100xtrans.png", "/images/kvadrat volym och area100xtransglow.png", 100, 50),
		new Button("Circle area and volume", "/images/cirkel volym och area100pxtrans.png", "/images/cirkel volym och area100pxtransglow.png", 205, 50),
		new Button("Triangle area and volume", "/images/Triangelvoloarea.png", "/images/Triangelvoloareaglow.png", 310, 50),
		new Button("Angles", "/images/Vinklar.png", "/images/Vinklarglow.png", 415, 50),
		new Button("Patterns", "/images/Buttondark.png", "/images/theButton3.png", 520, 50),
		new Button("Square perimiter", "/images/Buttondark.png", "/images/theButton3.png", 100, 155),
		new Button("Circle perimiter", "/images/Buttondark.png", "/images/theButton3.png", 205, 155),
		new Button("Triangle perimiter", "/images/Buttondark.png", "/images/theButton3.png", 310, 155),
		new Button("Degrees and radians", "/images/Buttondark.png", "/images/theButton3.png", 415, 155),
		new Button("Symmetry", "/images/Buttondark.png", "/images/theButton3.png", 520, 155)
	};
	
	protected Button switchBackgroundButton = new Button("Switch Background", "/images/Buttondark.png", "/images/theButton3.png", 695, 565);
	
	private String[][][] imageFilepath = {
			//Algebra
			{
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //section 1
				{"/images/Buttondark.png", "/images/theButton3.png"} //section 2
			},
			//Geometry
			{
				{"/images/Buttondark.png", "/images/theButton3.png"}, //Square are and volume
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Circle area and volume
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Triangle area and volume
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Angles
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Patterns
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Square perimiter
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Circle perimiter
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Triangle perimiter
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Degrees and radians
				{"/images/denshin enderman.PNG", "/images/enderman.PNG"} //Symmetry
			}
	};
	private String[][][] imageIDKeys = {
			//Algebra
			{
				{"Linear equations 1", "Linear equations 2"},
				{"System of equations 1", "System of equation 2"}
			},
			//Geometry
			{
				{"Square area and volume 1", "Square area and volume 2"},
				{"Circle area and volume 1", "Circle area and volume 2"},
				{"Triangle area and volume 1", "Circle area and volume 2"},
				{"Angles 1", "Angles 2"},
				{"Patterns 1", "Patterns 2"},
				{"Square perimiter 1", "Square perimiter 2"},
				{"Circle perimiter 1", "Circle perimiter 2"},
				{"Triangle perimiter 1", "Triangle perimiter 2"},
				{"Degrees and radians 1", "Degrees and radians 2"},
				{"Symmetry 1", "Symmetry 2"}
			}
	};
	
	protected JimDB database = new JimDB(this, imageFilepath, imageIDKeys);
	private Image[] jimImage;
	private int jimImageIndex;
	
	WindowListener windowListener = new WindowAdapter(){
		
		// anonymous WindowAdapter class
		public void windowClosing (WindowEvent w){
			exitProgram();
		} // end windowClosing
	};// end anonymous class
	
	
	/**
	 * Constructor
	 * @param f
	 * @param buff
	 * @param m
	 * @throws IOException
	 */
	public GraphicsEngine(JFrame f, BufferStrategy buff, Mathematics m) throws IOException{
		frame = f;
		strat = buff;
		math = m;
		bgImage = new Image[5];
		bgImage[0] = loadImage("/images/bakgrund01.png");
		bgImage[1] = loadImage("/images/bakgrund02.png");
		bgImage[2] = loadImage("/images/bakgrund03.png");
		bgImage[3] = loadImage("/images/bakgrund04.png");
		bgImage[4] = loadImage("/images/bakgrund05.png");
	}
	
	public void initFrameAndStuff(){
		frame = new JFrame("Project Mathematics");
		frame.setSize(800, 600);
		frame.setResizable(true);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(windowListener);
		frame.setVisible(true);
		try{
			SwingUtilities.invokeLater(new Runnable(){ //TODO finish this stuff with the EDT thread.
				public void run(){
					System.out.println("Check");
					frame.createBufferStrategy(2);
					strat = frame.getBufferStrategy();
				}
			});
			//frame.createBufferStrategy(2);
		}catch(IllegalStateException e){
			e.getMessage();
			e.printStackTrace();
			frame.setVisible(true);
			frame.createBufferStrategy(2);
		}finally{
			math.startThreads();
		}
	}
	
	public void exitProgram(){
		math.stopAll();	
		frame.setVisible(false);
		frame.dispose();
	}
	
	@Override
	public void run(){
		looping = true;
		while(looping){
			renderReInit();
			render();
			renderDispose();
			renderUpdate();
			try{
				Thread.sleep(20);
			}catch(InterruptedException e){
				System.out.println("Skiten kraschade");
				e.printStackTrace();
			}
		}
	}
	
	public void renderReInit(){
		try{
			g2d = (Graphics2D)strat.getDrawGraphics();
		}catch(IllegalStateException e){
			System.err.println("Buffer failed to recreate");
			strat = frame.getBufferStrategy();
			g2d = (Graphics2D)strat.getDrawGraphics();
			System.err.println("Buffer recreated");
		}
	}
	
	public void render(){
		g2d.setColor(bgColor);
		g2d.fillRect(0, 0, 800, 600);
		g2d.drawImage(bgImage[bgIndex], 0, 0, null);
		g2d.setColor(drawColor);
		g2d.drawString("Choice: " + math.choice, 20, 50);
		g2d.drawString("Unit: " + math.unit, 20, 70);
		
		g2d.drawString("Dimensions on frame: 800x600 pix", 50, 300);
		g2d.drawString("Button dimensions: " + geometrySectionButtons[0].getWidth() + "x" + geometrySectionButtons[0].getHeight() + " pix", 50, 320);
		
		if(drawMainMenu){
			for(int i = 0; i < mainMenuButtons.length; i++){
				mainMenuButtons[i].draw(g2d);
			}
			switchBackgroundButton.draw(g2d);
		}else if(drawAlgebraChoices){
			for(int i = 0; i < algebraSectionButtons.length; i++){
				algebraSectionButtons[i].draw(g2d);
			}
		}else if(drawGeometryChoices){
			for(int i = 0; i < geometrySectionButtons.length; i++){
				geometrySectionButtons[i].draw(g2d);
			}
		}
		
		if(database.drawImage){
			if(jimImageIndex < jimImage.length){
				g2d.drawImage(jimImage[jimImageIndex], 200, 300, null);
			}else{
				g2d.drawImage(jimImage[0], 20, 300, null);
			}
		}
		
		if(drawAlgebraChoices){
			g2d.drawString("Algebra! :D", 100, 100);
		}else if(drawGeometryChoices){
			g2d.drawString("Geometry! :D", 100, 100);
		}else if(drawArithmeticChoice){
			g2d.drawString("Arithmetic! :D", 100, 100);
		}
		
		if(!strat.contentsLost()){
			strat.show();
		}else{
			System.err.println("Buffer contents lost");
			System.err.println("Calm down though, this shit ain't serious");
			System.err.println("Just continue chillin', the program will handle it");
		}
	}
	
	public void renderDispose(){
		g2d.dispose();
	}
	
	public void renderUpdate(){
		database.update();
	}
	
	public void setButtonHoverStatus(int index, boolean status){
		if(!(mainMenuButtons[index].isHovered())){
			mainMenuButtons[index].setHover(true);
		}else{
			mainMenuButtons[index].setHover(false);
		}
	}
	
	public Image loadImage(String path) throws IOException{
		System.out.println("Loading image: " + path);
		Image tempImg;
		try{
			tempImg = new ImageIcon(getClass().getResource(path)).getImage();
			if(tempImg != null){
				System.out.println("Image " + path + " loaded without errors");
				return tempImg;
			}else{
				return null;
			}
		}catch(NullPointerException assfuck){
			throw new IOException();
		}
	}
	
	public void modifyBackgroundIndex(int delta){
		if(bgIndex + delta < bgImage.length){
			bgIndex += delta;
		}else{
			bgIndex = 0;
		}
	}
	
	public JFrame getAppFrame(){return frame;}
	
	public void setJimImage(Image[] img){
		jimImage = img;
	}
	public Image[] getJimImage(){return jimImage;}
}
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;

/**
 * Main class for the project
 * @author Johan Uttermalm
 * @version 0.42 

 * Last updated: 2011-11.06
 */

public class Mathematics extends JFrame implements Runnable{

	protected Choice c1Test = new Choice("lol");
	protected boolean squareIncomplete;
	
	protected String choice = "";
	private int amount;
	private float xValue, yValue, zValue;
	protected int unit;
	
	private int[] squareDim = new int[4];
	private int squareIndex;
	
	private Scanner amed = new Scanner(System.in);
	private boolean running;
	
	protected boolean drawing;
	
	protected JFrame frame;
	protected BufferStrategy strat;
	protected Graphics2D g2d;
	protected Thread graphicsLoop;
	protected Thread listeningLoop;
	protected Thread mainLoop;
	
	
	protected GraphicsEngine ge;
	protected InputListener inLis;
	protected MathGeek charles = new MathGeek();
	
	protected boolean drawShapes, drawSquare, drawTriangle, drawCircle;
	protected boolean performingEquation, performingQuadratic, performingNegativeQuadratic;
	
	protected double resultToGraphEng;

	private char currentTypedChar;
	
	public static void main(String[] args){ //TODO Remember to use the -Dsun.java2d.d3d=false command when executing!!!
		Mathematics math = new Mathematics();
		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 \"File path 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(InterruptedException e){
				e.printStackTrace();
			}
		}else{
			System.out.println(args[0]);
			math.init();
		}
		
		//math.start();
	}
	
	public void init(){
		try{
			ge = new GraphicsEngine(frame, strat, this); //If shit get's unstable, change 'frame' to 'this'
		}catch(IOException assfuckingIOException){
			assfuckingIOException.printStackTrace();
			try{
				Thread.sleep(10000);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			System.exit(1);
		}

		graphicsLoop = new Thread(ge);
		listeningLoop = new Thread(inLis);
		mainLoop = new Thread(this);
		ge.initFrameAndStuff();
		inLis = new InputListener(this, ge);
	}
	
	public void startThreads(){
		listeningLoop.start();
		mainLoop.start();
		graphicsLoop.start();
	}
	
	public void stopAll(){
		inLis.listening = false;
		ge.looping = false;
		running = false;
	}
	
	@Override
	public void run(){
		running = true;
		while(running){
			checkAction();
			checkSubSection();
			ge.database.update();
			try{
				Thread.sleep(20);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
	
	public void checkAction(){
		if(inLis != null && ge != null){
			if(inLis.isInAlgebra){
				ge.drawAlgebraChoices = true;
				ge.drawMainMenu = false;
			}else if(inLis.isInGeometry){
				ge.drawGeometryChoices = true;
				ge.drawMainMenu = false;
			}else if(inLis.isInArithmetic){
				ge.drawArithmeticChoice = true;
				ge.drawMainMenu = false;
			}
		
			else{
				ge.drawAlgebraChoices = false;
				ge.drawGeometryChoices = false;
				ge.drawArithmeticChoice = false;
				ge.drawMainMenu = true;
			}
		}
	}
	
	public void checkSubSection(){
		if(inLis != null && ge != null && ge.database != null){
			if(inLis.isInAlgebra){
				ge.database.inAlgebra = true;
				if(inLis.isInAlgLinearEquat){
					ge.database.inLinearEquat = true;
					ge.drawAlgebraChoices = false;
				}else if(inLis.isInAlgSystemOfEquat){
					ge.database.inSystemOfEquat = true;
					ge.drawAlgebraChoices = false;
				}else{
					//ge.test.inAlgebra = false; kanske ta bort det här sen, vet inte :3
					ge.database.inLinearEquat = false;
					ge.database.inSystemOfEquat = false;
				}
			}else if(inLis.isInGeometry){
				ge.database.inGeometry = true;
				if(inLis.inGeoSquareAreaVolume){
					ge.database.inSquareAreaVolume = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoCircleAreaVolume){
					ge.database.inCircleAreaVolume = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoTriangleAreaVolume){
					ge.database.inTriangleAreaVolume = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoAngles){
					ge.database.inAngles = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoPatterns){
					ge.database.inPatterns = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoSquarePerimiter){
					ge.database.inSquarePerimiter = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoCirclePerimiter){
					ge.database.inCirclePerimiter = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoTrianglePerimiter){
					ge.database.inTrianglePerimiter = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoDegreesRadians){
					ge.database.inDegreesRadians = true;
					ge.drawGeometryChoices = false;
				}else if(inLis.inGeoSymmetry){
					ge.database.inSymmetry = true;
					ge.drawGeometryChoices = false;
				}
				
				else{
					//row 0
					ge.database.inSquareAreaVolume = false;
					ge.database.inCircleAreaVolume = false;
					ge.database.inTriangleAreaVolume = false;
					ge.database.inAngles = false;
					ge.database.inPatterns = false;
					//row 1
					ge.database.inSquarePerimiter = false;
					ge.database.inCirclePerimiter = false;
					ge.database.inTrianglePerimiter = false;
					ge.database.inDegreesRadians = false;
					ge.database.inSymmetry = false;
				}
				
			}else if(inLis.isInArithmetic){
				ge.drawArithmeticChoice = true;
				ge.drawMainMenu = false;
			}
		
			else{
				if(!inLis.isInAlgebra || !inLis.isInGeometry){
					//do not draw sub-section choices of buttons
					ge.drawAlgebraChoices = false;
					ge.drawGeometryChoices = false;
					ge.drawArithmeticChoice = false;
					
					//draw main menu section choices
					ge.drawMainMenu = true;
					
					//do not draw sub-section images
					ge.database.inAlgebra = false;
					ge.database.inGeometry = false;
					
					//take care of rest variables
					ge.database.inLinearEquat = false;
					ge.database.inSystemOfEquat = false;
				}
			}
		}
	}
		
}

Ah well you want to start the threads after the buffers get created don’t you? move the startThreads() method to inside the invokeLater method :slight_smile:

no, invokeLater() invokes the runnable - well - later :wink:

you can use invokeAndWait() instead, but be careful, not to do it on the Event Dispatcher Thread, because you might create a deadlock on some JVMs.

I usually use a helper method for that:


	private static void updateGUI(Runnable runnable)
	{
		updateGUI(runnable, false);
	}

	private static void updateGUI(Runnable runnable, boolean async)
	{
		try
		{
			if(async) SwingUtilities.invokeLater(runnable);
			else if (SwingUtilities.isEventDispatchThread()) runnable.run();
			else SwingUtilities.invokeAndWait(runnable);
		}
		catch (Exception ex)
		{
			Logger.getLogger("updateGUI").log(Level.SEVERE, null, ex);
		}
	}

@ra4king: Ooooh, now i understood how the method works! :smiley: 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 :smiley: Once again, thanks for the help :slight_smile:

@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?

Glad to help :slight_smile:

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.

  • I’m not taking sides one way or the other, just giving an option.

I’m probably “reviving an old thread wahwah” but I’d just like to say… THANK YOU!

I’ve been having this problem for so long, had no idea it was because of video card acceleration!

The whole thread can be summed up as follows:

  1. Put -Dsun.java2d.d3d=false in the VM arguments…
    or 2. Create all swing related objects on the EDT, it’s so simple xD

Also, consider this my discretely hidden away “Hi, I’m new!” post :slight_smile: I think I’ll be hanging around here for a while :slight_smile: /me goes to find some nubs to help out.

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

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)