What is it wanting me to create a method?

83 views Asked by At

I am trying to create the menu for the game I am working on but when I try and use GetBufferStrategy it Eclipse tells me to create a method. I don't want a method. I want it to get the buffer strategy. I am kinda new to java so please explain why my code is wrong so I don't repeat it again.

package homeScreen;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;

public class homeScreen {

    static Toolkit tk = Toolkit.getDefaultToolkit();
    public static final int WIDTH = (int) tk.getScreenSize().getWidth();
    public static final int HEIGHT = (int) tk.getScreenSize().getHeight();

    BufferedImageLoader imageLoader = new BufferedImageLoader();

    private static homeScreen home = new homeScreen();

    private BufferedImage backgroundImage = null;
    private BufferedImage Buttons = null;

    public static void main(String args[]){
        JFrame frame = new JFrame("Game");
        frame.pack();
        frame.setSize(WIDTH, HEIGHT);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        home.loadImages();
    }

    private void loadImages() {
        try {
            backgroundImage = imageLoader.loadImage("/res/HomeScreen.png");
            Buttons = imageLoader.loadImage("/res/MainScreenButtons.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
        home.display();
    }

    private void display(){
        BufferStrategy buffStrat = this.getBufferStrategy();
        Graphics g = buffStrat.getDrawGraphics();

        if(buffStrat == null){
            createBufferStrategy(3);
            return;
        }
        ////////////////////////////////////////////////

        ////////////////////////////////////////////////
        g.dispose();
        buffStrat.show();
    }
}
1

There are 1 answers

0
MadProgrammer On

Your class doesn't extend from anything, therefore this.getBufferStrategy() does not exist within the context of you class.

Try extending from java.awt.Canvas instead

Having said that, be careful mixing heavy and light weight components, they tend to not play well together.

I would also avoid Toolkit.getScreenSize() as it does not take into account any "additional" elements (like the task bar) that might be present on the screen.

You "could" use frame.setExtendedState(MAXIMIZED_BOTH); instead...