My problem is that the image is not showing on my java.
here is my code
MyGame.java
package mygame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyGame extends JFrame {
Container c;
Player p;
MyGame()
{
this.setTitle("My Game");
this.setSize(600, 400);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = this.getContentPane();
p = new Player();
c.add(p);
this.addKeyListener(p);
}
public static void main(String[] args) {
MyGame m = new MyGame();
m.setVisible(true);
}
}
Player.java
package mygame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Player extends JPanel implements KeyListener {
int x=10,y=10, ctr=1;
ImageIcon img = new ImageIcon("image.gif");
public void paint (Graphics g)
{
super.paint(g);
g.setColor(Color.GRAY);
g.fillRect(0,0,600,400);
g.drawImage(img.getImage(), x, y, null);
}
public void keyPressed(KeyEvent e){
int i = e.getKeyCode();
String key = KeyEvent.getKeyText(i);
if (key.equals("Up"))
{
y=y-3;
ctr=ctr%6+1;
img = new ImageIcon("d"+ctr+".gif");
this.repaint();
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}