我正在尝试获取一张动画图像,但面板保持白色。 该图像与该类位于同一文件夹中。
public class JBallonPanel extends JPanel implements Runnable{
private Image img;
private int x, y;
JBallonPanel(){
setBackground(Color.white);
img=Toolkit.getDefaultToolkit().createImage(".//Ball.jpg");
x=this.getWidth()/2;
y=this.getHeight()/2;
}
public void paintComponent(Graphics g){
g.drawImage(img, x, y,
img.getWidth(this), img.getWidth(this), this);
}
答案 0 :(得分:0)
下面看一下,我做了一些调整,但是您的代码正在运行。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
//public class JBallonPanel extends JPanel implements Runnable {
//You can remove implements Runnable
public class JBallonPanel extends JPanel {
private Image img;
private int x, y;
JBallonPanel(){
setBackground(Color.white);
// your path should have \\ for example C:\\Ball.jpg or C:\\myImageFolder\\Ball.jpg
// but in your case, it works too.
img=Toolkit.getDefaultToolkit().createImage("C:\\Ball.jpg");
x=this.getWidth()/2;
y=this.getHeight()/2;
}
public void paintComponent(Graphics g){
g.drawImage(img, x, y, img.getWidth(this), img.getWidth(this), this);
}
//And run like this
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new JBallonPanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setSize(800, 800);
frame.setVisible(true);
}
}