Advertisement

BufferStrategy error

Started by July 07, 2015 11:25 PM
1 comment, last by KenWill 9 years, 5 months ago

I am getting an error saying that it cannot make a static reference to a non-static for the BufferStrategy...


package main;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{

	private static final long serialVersionUID = 1L;
	public static int width = 300;
	public static int height = width / 16 * 9;
	public static int scale = 3;
	public boolean isRunning = false;
	
	private Thread gameT;
	private JFrame frame;

	public synchronized void start(){
		isRunning = true;
		gameT = new Thread(this);
		gameT.start();
	}
	
	public synchronized void stop(){
		isRunning = false;
		try{
			gameT.join();
		}catch(InterruptedException e ){
			e.getStackTrace();
		}
	}
	
	public Game(){
		Dimension resolution = new Dimension(width * scale, height * scale);
		setPreferredSize(resolution);
		frame = new JFrame();
		
	}
	
	public static void main(String[] args){
		Game game = new Game();
		game.frame.setVisible(true);
		game.frame.setResizable(false);
		game.frame.setTitle("GameTest");
		game.frame.add(game);
		game.frame.pack();
		game.frame.setLocationRelativeTo(null);
		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.start();
	}

	@Override
	public void run() {
		update();
		render();
		
	}
	
	public static void update(){
		
	}
	public static void render(){
		BufferStrategy bs = getBufferStrategy();
		if(bs == null){
			createBufferStrategy(3);
			return;
		}
	}
}

Now i don't know how to get over it because is from the Canvas class, and i tried to make it a static object but it doesn't work either.

Your 'render()' methd is static. It has no Canvas instance to call that "getBufferStrategy" on. So you're calling it from nowhere essentially. Thats why its telling you "getBufferStrategy isn't static, you can't call it from nowhere!" ( "nowhere" here being a static method context, like say, your 'render' method).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Advertisement

Your 'render()' methd is static. It has no Canvas instance to call that "getBufferStrategy" on. So you're calling it from nowhere essentially. Thats why its telling you "getBufferStrategy isn't static, you can't call it from nowhere!" ( "nowhere" here being a static method context, like say, your 'render' method).

Awwww, right! -.-' Thanks!

This topic is closed to new replies.

Advertisement