0

I am using NetBeans IDE 8.2 and I created a simple clone game from tutorials. I am looking to add a background to the app and my research keeps pointing to using JFrame Forms and a JLabel.

None of the tutorials touched on backgrounds or JFrame Forms/JLabels. So I am uncertain how to take my completed project and add a background. I have attempted to reproduce JFrame Forms and JLabel code only to be unable to put my classes/interfaces "inside?" or "on top?" of the JFrame Form/JLabel.

I apologize if this really isn't an ideal first question, I just signed up and this is my first dip into the Java pool. Game class with JFrame (not Form) settings

EDIT: Adding full paste of my Game.java class.

package game;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.*;

public class Game {

public final static int WIDTH = 1920, HEIGHT = 1080;

private String gameName = "Tutorial Game";

private Canvas game = new Canvas();

private Input input;

private ArrayList<Updatable> updatables = new ArrayList<>();
private ArrayList<Renderable> renderables = new ArrayList<>();

// Helper methods for update/render Arrays
public void addUpdatable(Updatable u) {
    updatables.add(u);
}

public void removeUpdatable(Updatable u) {
    updatables.remove(u);
}

public void addRenderable(Renderable r) {
    renderables.add(r);
}

public void removeRenderable(Renderable r) {
    renderables.remove(r);
}

public void start() {
    // Initialize windows
    Dimension gameSize = new Dimension(Game.WIDTH, Game.HEIGHT);
    JFrame gameWindow = new JFrame(gameName);
    gameWindow.setDefaultCloseOperation(3);
    gameWindow.setSize(gameSize);
    gameWindow.setResizable(false);
    gameWindow.setLocationRelativeTo(null);
    gameWindow.add(game);
    game.setSize(gameSize);
    game.setMinimumSize(gameSize);
    game.setMaximumSize(gameSize);
    game.setPreferredSize(gameSize);
    gameWindow.setVisible(true);

    // Initialize Input
    input = new Input();
    game.addKeyListener(input);

    // Game loop
    final int TICKS_PER_SECOND = 60;
    final int TIME_PER_TICK = 1000 / TICKS_PER_SECOND;
    final int MAX_FRAMESKIPS = 5;

    long nextGameTick = System.currentTimeMillis();
    int loops;
    float interpolation;

    long timeAtLastFPSCheck = 0;
    int ticks = 0;

    boolean running = true;
    while(running) {
        // Updating
        loops = 0;

        while(System.currentTimeMillis() > nextGameTick && loops < MAX_FRAMESKIPS) {
            update();
            ticks++;

            nextGameTick += TIME_PER_TICK;
            loops++;
        }

        // Rendering
        interpolation = (float) (System.currentTimeMillis() + TIME_PER_TICK - nextGameTick)
                      / (float) TIME_PER_TICK;
        render(interpolation);

        // FPS Check
        if(System.currentTimeMillis() - timeAtLastFPSCheck >= 1000) {
            System.out.println("FPS: " + ticks);
            gameWindow.setTitle(gameName + " - FPS: " + ticks);
            ticks = 0;
            timeAtLastFPSCheck = System.currentTimeMillis();
        }
    }                
}

private void update() {
    for(Updatable u : updatables) {
        u.update(input);
    }
}

private void render(float interpolation) {
    BufferStrategy b = game.getBufferStrategy();
    if(b == null) {
        game.createBufferStrategy(2);
        return;
    }

    Graphics2D g = (Graphics2D) b.getDrawGraphics();
    g.clearRect(0, 0, game.getWidth(), game.getHeight());
    for(Renderable r : renderables) {
        r.render(g, interpolation);
    }

    g.dispose();
    b.show();        
}
}
Punch
  • 1
  • 2
  • *"using ... a JLabel"* - I wouldn't, [for these reasons](http://stackoverflow.com/questions/23665784/java-gui-background-image/23667373#23667373). Using the second example from [this example](http://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430) would be my preferred solution, and incase you wanted to make to scale with the window, something like [this example](http://stackoverflow.com/questions/12876615/how-do-i-resize-images-inside-an-application-when-the-application-window-is-resi/12876799#12876799) – MadProgrammer Jan 22 '17 at 06:15
  • Without seeing the code, I'd advise against using any kind of form editor until you have a deeper understand how a Swing UI works and how the component hierarcy works, it's to easy to make a made designs with form editors which just screw with you. As a general piece of advice, it's generally discouraged to extend directly from a `JFrame` or `JDialog`, it locks you into a single use case, which makes life very difficult, your also not adding any value to the class which might be re-used, better to start with a `JPanel` and create the windows you need as you need – MadProgrammer Jan 22 '17 at 06:19

0 Answers0