I have made this code watching video from youtube. But my JFrame does not display anything. I have three class as follow and images are Mario_Back.jpg with 1200X300 and Stick.gif with 400X400 in the same directory. Can anyone help me?
-------------------------------------------------------------------------------------------------------------------------------------
Frame.java
--------------------------------------------------------
package TryGame;
import javax.swing.JFrame;
public class Frame {
public static void main(String args[])
{
JFrame frame = new JFrame("Survival");
frame.add(new Board());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(1200,300);
frame.setVisible(true);
}
}
------------------------------------------------------------------------------------------------------------------------------------
Dude.java
----------------------------------------------
package TryGame;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Dude
{
int x,dx,y;
Image still;
public Dude()
{
ImageIcon i = new ImageIcon("Stick.gif");
still = i.getImage();
x = 10;
y = 172;
}
public void move()
{
x = x + dx;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public Image getImage()
{
return still;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
dx = -1;
}
if(key == KeyEvent.VK_RIGHT)
{
dx = 1;
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
dx = 0;
}
if(key == KeyEvent.VK_RIGHT)
{
dx = 0;
}
}
}
------------------------------------------------------------------------------------------------------------------------------
Board.java
----------------------------------------------------------
package TryGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener
{
Dude p;
Image img;
Timer time;
public Board()
{
p = new Dude();
addKeyListener(new AL());
setFocusable(true);
ImageIcon i = new ImageIcon("Mario_Back.jpg");
img = i.getImage();
time = new Timer(5, this);
time.start();
}
@Override
public void actionPerformed(ActionEvent e) {
p.move();
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(img, 0, 0, null);
g2d.drawImage(p.getImage(),p.getX(),p.getY(),null) ;
}
private class AL extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
p.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
p.keyPressed(e);
}
}
}