-1

so I went on and started creationg game as usual. Except this time I got an error O.o. I was trying to find an answer, but non of the ones I thought maybe here I can get a good Answer! Heres the error:

Description Resource Path Location Type Cannot make a static reference to the non-static field image Game.java /POGA/src/packagehere line 71 Java Problem

Description Resource Path Location Type Cannot make a static reference to the non-static method createBufferStrategy(int) from the type Canvas Game.java /POGA/src/packagehere line 66 Java Problem

I get more of these type, but I guess If you show me and viewers of this question how to fix these errors we would be able to fix the rest of the "same" ones...

package packagehere;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;

BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public static boolean running = false;
JFrame frame;
public static String title = "POGA game thingy - 0.1 Aplha";
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final Dimension gameDim = new Dimension(WIDTH, HEIGHT);

synchronized void start() {
    Thread thread = new Thread();
    thread.start();
    running = true;
}

public void run() {
    while(running) {
        tick();
        render();
    }
}

synchronized void stop() {
    running = false;
    System.exit(0);
}

public Game() {
    setMaximumSize(gameDim);
    setMinimumSize(gameDim);
    setPreferredSize(gameDim);

    frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(this, BorderLayout.CENTER);
    frame.pack();

    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    frame.requestFocus();
}

public static void tick() {

}

public static void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null) {
        bs = createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();

    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

    g.dispose();
    bs.show();
}
}
PotatoIng
  • 1
  • 3
  • possible duplicate of [cannot make a static reference to the non-static field](http://stackoverflow.com/questions/8101585/cannot-make-a-static-reference-to-the-non-static-field) – jmj Apr 16 '14 at 18:03
  • Why do you have so many things marked 'static'? This doesn't look right. If you want to say `Game game = new Game()` and then call methods on `game`, don't use static. – David Koelle Apr 16 '14 at 18:04
  • The error message says it all... You cannot refer a non static field from a static block/method. If the static modifier is included in a field or method declaration, no instance of the class is required for it to be used. The field or method is associated with the class and not an individual object. Also have a look at http://stackoverflow.com/questions/17622088/when-to-use-static-method-and-field – Arun A K Apr 16 '14 at 18:05
  • 1
    Most likely `render() should *not* be `static`. – Code-Apprentice Apr 16 '14 at 18:07
  • @Potatolng Please answer `on what object are you calling getWidth() methods in render method?` – Braj Apr 16 '14 at 18:20
  • @Potatolng as you have edited your question hence I have also edited it in my post. – Braj Apr 16 '14 at 18:25
  • @Braj sorry, getHeight(); wasnt subject of this question. – PotatoIng Apr 16 '14 at 19:05

2 Answers2

0

Where are getBufferStrategy() and createBufferStrategy() declared? (Answer: In the type Canvas which you are extending)

They are NON-Static methods, you cannot access them from within your render() method, because that method is static.

You can access Static methods from within Static and non-Static methods. You can access non-static methods from non-static methods only.

dognose
  • 20,360
  • 9
  • 61
  • 107
  • @Braj Yes, because that's the error: `Cannot make a static reference to the non-static method createBufferStrategy(int)` (first question is rhetorical) – dognose Apr 16 '14 at 18:05
0

The problem is at below line

g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

Second on what object are you calling getWidth() and getHeight() methods?

I think you want to get the width and height of Canvas.

To solve this issue remove static from render() method.

Braj
  • 46,415
  • 5
  • 60
  • 76