嘿,我正在尝试创建一个视频游戏,我正在测试是否可以让精灵或图像出现在我之前向同行和教授寻求帮助的小程序上,但他们似乎没有帮助. 我不知道我的图像是否在错误的位置,或者我的代码是否存在错误,但如果有人看一下它,我将不胜感激谢谢!哦!顺便说一句,我正在用 Java 编程,并且正在使用 Eclipse JUNO。
enter code here
package meh;
import java.awt.*;
import javax.swing.JApplet;
import javax.swing.ImageIcon;
public class Draw extends JApplet{
public static void main(String[] args)
{
Draw test = new Draw();
}
private Image exImage;
private boolean imagesLoaded;
public void run()
{
imagesLoaded = false;
try
{
loadImages();
try
{
Thread.sleep(10000);
}
catch(InterruptedException ex){}
}
finally{}
}
public void loadImages()
{
exImage = loadImage("C:/Users/Temp/workspace/From Scratch/bin/Ma_rn_0");
imagesLoaded = true;
repaint();
}
private Image loadImage(String fileName)
{
return new ImageIcon(fileName).getImage();
}
public void paint(Graphics g)
{
if(g instanceof Graphics2D)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
if(imagesLoaded)
{
drawImage(g, exImage,0,0, null);
}
else
{
g.drawString("Loading...", 5, 12);
}
}
public void drawImage(Graphics g, Image image, int x, int y, String caption)
{
g.drawImage(image, x, y, null);
g.drawString(caption, x+5, y + 12 +image.getHeight(null));
}
}