我正在尝试将图像设置为小程序的背景。每当调用paint方法时,它都会重新绘制图像;这会导致闪烁。如何使图像只绘制一次?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Main extends Applet {
Image background;
@Override
public void init() {
setSize(800, 600);
try {
System.out.println(getCodeBase());
background = ImageIO.read(new File("1.jpg"));
} catch (IOException ex) {
System.out.println("Error reading the image");
}
}
@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, this);
}
}