Calling getBufferStrategy and createBufferStrategy without an object
NickName:robzoid Ask DateTime:2016-12-01T09:36:58

Calling getBufferStrategy and createBufferStrategy without an object

I was watching a video on making a game in Java and saw the following code for creating a BufferStrategy object:

public void render()  {
    BufferStrategy bs = getBufferStrategy();
    if bs==null {
       createBufferStrategy(3);
       return
    }
}

I've seen this exact block of code in other examples and videos, so it seems pretty standard. My question is, since neither getBufferStrategy() or createBufferStrategy() are static methods, shouldn't they be called with an object? And since this render method belongs to a class that extends the Canvas class, shouldn't the code look like this?:

public void render()  {
    BufferStrategy bs = this.getBufferStrategy();
    if bs==null {
      this.createBufferStrategy(3);
      return
    }
}

The docs.oracle page for the Canvas class lists these methods as non-static. So why is it ok for us to call them without an object? Any help is greatly appreciated.

Copyright Notice:Content Author:「robzoid」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901220/calling-getbufferstrategy-and-createbufferstrategy-without-an-object

More about “Calling getBufferStrategy and createBufferStrategy without an object” related questions

Calling getBufferStrategy and createBufferStrategy without an object

I was watching a video on making a game in Java and saw the following code for creating a BufferStrategy object: public void render() { BufferStrategy bs = getBufferStrategy(); if bs==nul...

Show Detail

getBufferStrategy(); or createBufferStrategy(2/3); Which comes first? And does it matter?

I have been looking up some examples in books and in on line video's. I find that when people are creating a bufferStrategy they do it in so many different ways. For example, as I understand it, I'm

Show Detail

createBufferStrategy cannot be made as a variable?

The title roughly sums it up. Why can I not do this? private void render() { bs = window.getCanvas().getBufferStrategy(); if (bs == null) { bs = window.getCanvas().createBufferStr...

Show Detail

I'm getting an IllegalStateException by using createBufferStrategy(3);

When I try to run this method, I get an IllegalStateException. It says there's an error where it says "createBufferStrategy(3);" public void render(){ BufferStrategy bs = this.getBufferStr...

Show Detail

Trouble with createBufferStrategy in Java

I'm fairly new at java, however I suspect my problem is caused by the same reasons as this post Since this makes me think I am trying to render before my component is added to a container, I just ...

Show Detail

createBufferStrategy(3); not working

I have code like that : private int[] pixels; BufferStrategy bs = this.getBufferStrategy(); if(bs == null) { createBufferStrategy(3); return; } it returns an error on line 2 in the code e...

Show Detail

what happens to the value returned by 'createBufferStrategy'?

I'm learning about BufferStrategy and I'm kind of confused with the creation of BS. my code looks like this... public class Game extends Canvas{ /*code not relevant to this topic.. ...

Show Detail

What is the correct way to use createBufferStrategy()?

Even after using Java Swing for over a year, it still seems like magic to me. How do I correctly use a BufferStrategy, in particular, the method createBufferSrategy()? I would like to have a JFram...

Show Detail

Object movement issue

I am trying to make a square object move to the right, instead, it increases in size making it longer. There seems to be an issue with the rendering. Render code: private void render(){

Show Detail

Why does illegalArgumentException get thrown when I use createBufferStrategy(3)?

I'm starting to create a game using Java. In my render() method, I have a buffer strategy, with three buffers. However, when I run it, I get this error. Exception in thread "Thread-1" java.lang.

Show Detail