What I'm trying to do here is call the repaint() method from within my loadbg() method. However, repaint() isn't working, and my image (stored in the var bg) won't load onto the screen. Any suggestions as to why this won't work?
package core;
/*** @author Adil
* 2DPlatformer
* Written by Adil
* Built upon the player-core framework (written by Adil)
* GNU Licensed
*/
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
@SuppressWarnings("serial") //Suppress serial warning ID
public class Core extends JFrame {
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800, 600, 16,
DisplayMode.REFRESH_RATE_UNKNOWN);
//new display with parameters 800x600 + 16 bit color depth
Core i = new Core(); //new core class var
i.run(dm);
}
//variables for image loading below
public Screen s;
public Image bg;
public boolean loaded = false;
//run method below
public void run(DisplayMode dm) {
setBackground(Color.BLACK);
setForeground(Color.WHITE);
setFont(new Font("Ubuntu", Font.PLAIN, 24));
s = new Screen();
loaded = false;
try {
s.setScreenSize(dm, this);
loadbg();
try {
Thread.sleep(5000);
} catch (Exception ex) {
}
} finally {
s.RestoreScreen();
}
}
public void loadbg() {
bg = new ImageIcon(
"file:\\\\home\\adil\\Desktop\\pack_2\\bgame.jpg").getImage();
loaded = true;
s.repaint();//s is my screen object, but it won't repaint.
}
public void paint(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (loaded) {
g.drawImage(bg, 0, 0, null);
}
}
}