Guys why is my image not loading on my JFrame??
BTW I am using eclipse on my mac.

Code for Board :

package ourgame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener {
        Dude p;
        public Image img;
        Timer time;

        public Board() {
                p = new Dude();
                addKeyListener(new AL());
                setFocusable(true);
                ImageIcon i = new ImageIcon("C:/character.png");
                img = i.getImage();
                time = new Timer(5, this);
                time.start();
        }

        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);
                }
        }
}

Code for Dude :

package ourgame;

import java.awt.*;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Dude {
        int x, dx, y;
        Image still;

        public Dude() {
                ImageIcon i = new ImageIcon("C:/background.jpg");
                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;
        }

}

Code for Frame:

package ourgame;

import javax.swing.*;

public class Frame {

        public Frame(){
                JFrame frame = new JFrame();
                frame.add(new Board());
                frame.setTitle("2-D Test Game");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(700,365);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
        }
        public static void main(String[] args){
                new Frame();
        }
}

Thanks in advance!

Overridding paint rather that paintComponent is almost always a mistake. In this case you call super.paint - which paints the Panel and all its children, then draw the background image over the top of all that.

You may also need to set one or more of size/preferredSize/minimumSize for your JPanel because Swing doesn't know about your image, so it has no way to know how big the JPanel should be.

Can you please post a code for that? @JamesChrrill

Sorry. This is a "we give you help & advice" site. We don't offer free coding services!
Try it for yourself, and if you get stuck come back here and post what you have done.

He told you what code you needed to put in.

size/preferredSize/minimumSize

setPreferredSize setMinimumSize setMaximumSize

Note the change I made here. In a GUI app, you should make its main
routine be a run() method. You need to do this so that all events received by your app go in an orderly fashion into the event queue. The invoke later method runs your run method.

    package ourgame;
    import javax.swing.*;
        public class Frame implements Runnable
        {
                public Frame()
                {
                        JFrame frame = new JFrame();

                }
                public void run()
                {
                        frame.add(new Board());
                        frame.setTitle("2-D Test Game");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setSize(700,365);
                        frame.setVisible(true);
                        frame.setLocationRelativeTo(null);
                }


                public static void main(String[] args){
                     Frame f = new Frame();
                     javax.swing.SwingUtilities.invokeLater(f);
                }
        }

You also need to be sure that your KeyListener is focusable. See the Java tutorial on the Focus System.

@ncmathsadist Hey , there are 6 errors in the code you posted :

 public void run()
                {
                        frame.add(new Board());
                        frame.setTitle("2-D Test Game");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setSize(700,365);
                        frame.setVisible(true);
                        frame.setLocationRelativeTo(null);
                }

Guys can you please read the question carefully. My problem is that the character and the background of the game are not displaying.

crossposted on SO (give me codezz - deleted by comunity)

You insult us by suggesting we didn't read your post "carefully".
Your original post was perfectly clear, as was the cause of the problem in your code.
Three people have explained what your problem is, and you chose to ignore them all. ncmathsadist even provided a code fragment with the fix applied. It wasn't intended to be a complete program, and doesn't have "6 errors", you just failed to understand what it was telling you.
If you chose not to act on our replies, and suggest that we are somehow at fault, then don't expect a lot more help here.

UPDATE My game has worked!!! Thanks for everything

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.