Reputation: 3172
I wrote this code in JFrame + Applet for a Game.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.URL;
public class myGame extends Applet {
static myGame k = new myGame();
JFrame f = new JFrame("myGame");
URL url;
Image player;
public void init(){
url = this.getDocumentBase();
player = this.getImage(url,"as.jpeg");//Here is the Image import
}
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.drawString("HI THERE",200,200);
g.fillRect(120,130,50,50);
g.drawImage(player,20,200,this); Here I draw it
}
public void start(){
f.setSize(600,400);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.add(k);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
k.start();
}
}
I cannot see the Image showing up as.jpg.
I wanted to Import this Image as a player sprite. My code is not giving any errors, it just not showing the image.
Upvotes: 0
Views: 384
Reputation: 10994
1) You forget to call init()
method of your Applet, because of your Image
doesn't initialized.
2)Use BufferedImage
for your image, load that like next:
public void init() {
url = getClass().getResource("as.jpeg");
try {
player = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
}
3) add k.init();
before k.start();
in your main
method.
Upvotes: 2