Solve the problem that java Graphics drawImage can't display pictures

  • 2021-12-12 04:08:06
  • OfStack

Directory java Graphics drawImage Unable to display pictures Solution: Summed up in two problems drawImage () First call to pictures unsuccessful Solution: Load all pictures once Solution: Use MediaTracker class

java Graphics drawImage Unable to display picture


package com.sitech.test; 
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
 
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
 
public class ImageTest {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new ImageFrame();
frame.setTitle("sasdas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); 
} 
});    
}
}
class ImageFrame extends JFrame
{
public ImageFrame()
{
add(new ImageComponent());
pack();  
} 
 
} 
class ImageComponent extends JComponent
{
private static final int DEFAULT_WIDTH =800;
private static final int DEFAULT_HEIGHT = 800; 
private Image image; 
public ImageComponent()
{
    String path = "com/sitech/test/111.jpg";
image= new ImageIcon(ClassLoader.getSystemResource(path)).getImage();//  Load pictures in this way  
 
}
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;  
if(image==null)return;
 
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
 
g2.drawImage(image,0,0,this);// This way  
 
 
for(int i =0 ;i*imageWidth<= getWidth();i++)
    for(int j = 0 ;j*imageHeight<=getHeight();j++)
	if(i+j>0)
	    g2.copyArea(0, 0, imageWidth, imageHeight,i*imageWidth,j*imageHeight);  
}
public Dimension getPreferredSize(){return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);}
}

Here is the test case I used when I tested using drawImage, and I encountered the problem that I couldn't display pictures

Solution: Summed up in two problems

Question 1:


String path = "com/sitech/test/111.jpg";
image= new ImageIcon(ClassLoader.getSystemResource(path)).getImage();
// Load pictures in this way 

Load the picture in the above way to get the picture normally (the reason is unclear)

Question 2:


g2.drawImage(image,0,0,this);
// This way   Finally 1 Parameters use  this  Can be in the first place 1 Secondary runtime   Display pictures, which I used before null

But the first run can not show the picture, debug view has obtained the picture, but it is unable to show, I changed the size of frame, resulting in automatic repaint, just show the picture, through Baidu said that the last parameter can be displayed using this, try it, but I don't understand why, first mark1

drawImage () first call to picture unsuccessful

The following code may not display the picture when using it for the first time. The drawImage () function returns false


    public void paint(Graphics g) {
        g.drawImage(imagesBomb[count], x, y, null)
        count++;
        if (count >= 42) {
            this.life = false;
        }
    }

Solution: Load all pictures once


    public void paint(Graphics g) {
        if(!g.drawImage(imagesBomb[count], x, y, null)){
            for (int i = 0; i < 43; i++) {
                g.drawImage(imagesBomb[i], -1000, -1000, null);
            }
            g.drawImage(imagesBomb[count], x, y, null);
        }
        count++;
        if (count >= 42) {
            this.life = false;
        }
    }

Information

I constantly call 1 function in Applet

In this function, the picture is displayed with Graphics:: drawImage, as shown below

The return value b will often be false in the first 1 period of time, resulting in abnormal display. Why? Is there any solution to ensure that it will be displayed normally in the first 1 period of time?

However, as long as it is displayed for 1 period of time, that is, after the function has been called several times, the return value b will be 1 straight to true, and false will never be returned again.

When the image in the drawImage method is not fully loaded, the method returns false.

The drawImage method loads the required picture when it is called, so when it is called for the first time, the picture is not completely loaded, resulting in abnormal display or even no picture. After that, the picture is loaded due to the method call, and it can be displayed normally when this picture is taken as a parameter in the future.

Solution: Use the MediaTracker class


Image img = Toolkit.getDefaultToolkit().getImage(imgPath);
MediaTracker t = new MediaTracker(this);
t.addImage(img, 0);
t.waitForAll();

Calling this code before drawImage (), MediaTracker ensures that your image is loaded before DRAW for use.

By addImage method, add an Image and add an ID number, and waitForAll () waits for all the added pictures to be loaded.


Related articles: