I'm doing a game in java, and my problem is setting the background.
I've tried using g.drawImage()
but as its repaints every time, cause fps to drop a lot. So i fixed this fps issue by setting background with JLabel
and ImageIcon
. But the label overlay the graphics, how can I fix this?
public class GameScreen extends Screen implements Observer {
private static GameScreen gamescreen;
private Game game;
private JLabel label = new JLabel();
private ImageIcon icon = new ImageIcon("./res/img/bg1.png");
private GameScreen() {
setGame(Game.getInstance());
setLayout(null);
label.setIcon(icon);
label.setBounds(0, 0, 600, 600);
add(label);
}
public static GameScreen getInstance() {
if(getGameScreen() == null) {
setGameScreen(new GameScreen());
}
return getGameScreen();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//PaintWorld
new DrawableWorld().draw(g, getWidth(), getHeight());
//Paint players
new DrawablePlayer().draw(g, getWidth(), getHeight(), getGame().getPlayers());
//Paint hud
new DrawableHud().draw(g, getWidth(), getGame().getPlayers());
}
@Override
public void update(Observable o, Object arg) {
repaint();
}
//getters & setters..
Example:
PS: All DrawableClasses extends Screen that extends JPanel
g.drawImage is a very fast (to an extent) operation. Do you scale the image while drawing? Do you use "maximum quality" rendering hints?