-1
我是新來的圖形在Java中,並一直試圖讓它釘牢,但我一直在這個問題卡在最後兩個星期。緩存的圖像正在讀取,但沒有出現
有兩個類,一個讀取文件(我認爲工作正常)和一個顯示它們。 (我想這就是問題的代表)
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class jpaneTesting extends JFrame implements Runnable
{
public jpaneTesting(int x, int y)
{
super("testing");
this.setBounds(0, 0, x, y);
this.setVisible(true);
}
@Override
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
loader loader = new loader();
BufferedImage character = loader.load("src\\resources\\hey.png"); //accesses hey.png
g2.drawImage(character,null,5,5);
}
@Override
public void run()
{
repaint();
}
public static void main (String args[])
{
Thread thread = new Thread (new jpaneTesting(500,500));
thread.start(); //runs the thread that was sent that argument
}
}
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class loader {
public BufferedImage load(String path){
try {
BufferedImage img = ImageIO.read(new File(path));
return img;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("oh no!"); //testing if image actually got there for myself
return null;
}
}
請參閱編輯回答。 –