Strange error including background images in java

My problem is that when I run my program, I get a white screen and text from an earlier assembly instead of the background image that is supposed to be displayed. I removed all the code associated with this assembly.

I looked for help, and all the streams that I saw speak to write code, as I configured it. I don’t understand where the displayed background is coming from.

Here is the code snippet:

package tactics;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;

public class Tactics2 extends JFrame{
    private Screen s;
    private BufferedImage bg;
    private BufferedImage template;
    private boolean loaded = false;

    public static void main(String[] args) throws IOException{ 

        DisplayMode dm = new DisplayMode(1024, 768, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
        Tactics2 t = new Tactics2();
        t.run(dm);
    }

    //run method
    public void run(DisplayMode dm) throws IOException{
        loadpics();

        s = new Screen(); 
        try{
            s.setFullScreen(dm, this);
            try{
                Thread.sleep(5000);
            }catch(InterruptedException ex){}
        }finally{
            s.restoreScreen();
        }
    }

    public void loadpics() throws IOException{
        bg = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);
        template = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);
        ChaosBack cb = new ChaosBack();
        bg = cb.ChaosBack(bg, template);
        loaded = true;
        repaint();
    }

    @Override
    public void paint(Graphics g){
        if(loaded){
            g.drawImage(bg, 0, 0, null);
        }
    }
}
+3
source share
1 answer

You broke the drawing chain

@Override
public void paint(Graphics g){
    if(loaded){
        g.drawImage(bg, 0, 0, null);
    }
}

Basically, you could not call super.paint. Graphics- a common resource, that is, everything written for a given paint cycle uses the same context Graphics.

, , Graphics.

paint . , , , , , .

, - JPanel paintComponent (, super.paintComponent)

Thread.sleep(5000); - Swing. / .

Swing . , Dispatching Event.

:

+4

All Articles