Pergunta

I followed a tutorial from the Java Hub about drawing images on JFrame. Everything works perfectly except the JFrame basically takes a screenshot of whatever is behind it; ex: Eclipse. It then makes that the background for no reason.

This is what i get:

enter image description here Any ideas?

Main.java:

package Package;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

public class Main extends JFrame {

BufferedImage sprite;

public Main(){
    setSize(800, 600);
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);        
    init();
}

private void init(){
    BufferedImageLoader loader = new BufferedImageLoader();
    BufferedImage spriteSheet = null;
    try {
        spriteSheet = loader.loadImage("sprites2.png");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    SpriteSheet ss = new SpriteSheet(spriteSheet);
    sprite = ss.grabSprite(0, 0, 20, 32);
}

@Override
public void paint(Graphics g){
    g.drawImage(sprite, 100, 100, null);
    repaint();
}

public static void main(String[] args){
    Main main = new Main();
    }
}

SpriteSheet.java:

package Package;

import java.awt.image.BufferedImage;

public class SpriteSheet {
public BufferedImage spriteSheet;

public SpriteSheet(BufferedImage ss){
    this.spriteSheet = ss;
}

public BufferedImage grabSprite(int x, int y, int width, int height){
    BufferedImage sprite = spriteSheet.getSubimage(x, y, width, height);
    return sprite;
    }

}

BufferedImageLoader.java:

package Package;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class BufferedImageLoader {

public BufferedImage loadImage(String pathRelativeToThis) throws IOException{
    URL url = this.getClass().getResource(pathRelativeToThis);
    BufferedImage img = ImageIO.read(url);
    return img;
    }

}


No exceptions are being thrown...

Foi útil?

Solução

Your application hangs due to the repaint() call in the paint() method. It is a recursive call, as repaint() calls paint().

You need to remove it, and your application won't hang anymore.


If you simply remove the repaint() call from the paint() method, your graphics won't be drawn, because you are overriding the paint() method without calling the superclass one. It should be like this:

@Override
public void paint(Graphics g){
    super.paint(g);
    g.drawImage(sprite, 100, 100, null);
}

However, it will not display the image, because you call the init() method after setting it visible. Call it before setting the frame visible and it will work.

However, this is not the correct way to put a background image in your JFrame, you should use a JPanel instead:

class ImagePanel extends JPanel {
    private Image image;
    int x, y;
    public ImagePanel(Image image, int x, int y) {
        this.image = image;
        this.x = x;
        this.y = y;
    }
    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, x, y, null);
    }
}

To call it:

SpriteSheet ss = new SpriteSheet(spriteSheet);
sprite = ss.grabSprite(0, 0, 20, 32);
ImagePanel backgroundImagePanel = new ImagePanel(sprite, 100, 100);
setContentPane(backgroundImagePanel);

Also, Remove the paint() override to the JFrame and call the init() method before setting the frame visible

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top