Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Image not diplaying in Jframe. Is there anything wrong in my Code?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image not diplaying in Jframe. Is there anything wrong in my Code?

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

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Image not diplaying in Jframe. Is there anything wrong in my Code?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    You should override the paintComponent() method for Swing classes, not the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Image display problem on JFrame...Whats wrong......need help?????'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 12th, 2012, 01:06 PM
  2. Replies: 2
    Last Post: October 31st, 2012, 01:07 AM
  3. Image display problem on JFrame...Can any1 help?????'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2012, 09:26 AM
  4. Replies: 1
    Last Post: April 30th, 2012, 08:16 AM
  5. Java Gif image problem in JFrame
    By Zachary Wins in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 23rd, 2010, 08:51 PM

Tags for this Thread