0

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);
        }
    }
}
Stedy
  • 7,359
  • 14
  • 57
  • 77
Adil
  • 572
  • 1
  • 6
  • 20

1 Answers1

6

1) better way would be put Icon/ImageIcon to JLabel, rather than paint Image by using paint()

2) for Swing is there paintComponent() instead of paint()

3) don't use Thread.sleep(int) for Swing GUI use javax.swing.Timer instead, because during Thread.sleep(int) you GUI simply freeze, nothing else

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I've tried some other methods, like changing the way the file is drawn and loaded through paintComponent(), doesn't seem to work :| – Adil Nov 14 '11 at 17:47
  • check this post is same as you need http://stackoverflow.com/questions/8084115/use-of-seticon-on-jlabel-repeats-old-image/8084657#8084657 or http://stackoverflow.com/questions/7943584/update-jlabel-every-x-seconds-from-arraylistlist-java/7944388#7944388 – mKorbel Nov 14 '11 at 17:50
  • As I am a noob at Java (learning off a tutorial), how would I go about implementing this? (There was a comment on the tutorial, however, that didn't help at all) – Adil Nov 14 '11 at 21:35
  • Pretty sure u don't need to call s.repaint(); u can just call repaint(); – imulsion Oct 18 '12 at 09:44
  • @imulsion in most cases could be contraproductive, because you (maybe) want to repaint only part of visible GUI .... depends of – mKorbel Oct 18 '12 at 11:12