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 4 of 4

Thread: Image Not Showing...

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

    Default Image Not Showing...

    So I was following a tutorial on ZetCode.com for creating basic animations (in this case, an animated star image on a black background). However, over the past few days of working with Swing, I've run into a recurring error, which I'm not sure of the reason for why it is happening. Essentially, I have the JFrame, but I can't seem to add top-level components to it. What I mean by this is that I can't add JPanels and such to the frame (I did manage to make a few buttons that for some reason showed up). I'm not sure whether it is a software issue or an error in my programming. If someone could look through the code to detect if anything is wrong, that would be awesome!

    For the program, all that shows up is the 280 x 240 black screen. There is no image displaying on it.

    P.S. I'm not sure what half of this stuff does. I'm kinda working through it line by line to try to figure out how to use the different libraries one at a time .

    P.S.S. I'm at school right now. If there's no solution by the time I get home, I'll try running the program from my home computer to test if it is actually a software error, because in all the programs that have experienced this "blank frame" problem, there hasn't been any compile errors.

    import javax.swing.JFrame;
     
    public class Star extends JFrame {
    	public Star() {
    		add(new Board());
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(280, 240);
    		setLocationRelativeTo(null);
    		setTitle("Star");
    		setResizable(false);
    		setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		new Star();
    	}
    }

    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import javax.swing.*;
     
    public class Board extends JPanel implements ActionListener {
    	Image star;
    	Timer t;
    	int x, y;
     
    	public Board() {
    		setBackground(Color.BLACK);
     
    		ImageIcon ii = new ImageIcon("/Desktop/images/star.png");
    		star = ii.getImage();
     
    		setDoubleBuffered(true);
     
    		x = y = 10;
    		t = new Timer(25, this);
    		t.start();
    	}
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(star, x, y, this);
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		x += 1;
    		y += 1;
     
    		if (y > 240) {
    			y = x = -45;
    		}
     
    		repaint();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image Not Showing...

    With Swing components you should override the paintComponent() method not the paint() method.

    The code works for me. Check that the path to the image is correct.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Pune
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Not Showing...

    Hi,

    You should specify full path of your image.

    Example: C:/Users/shahabmo/Desktop/abc.jpg

    Regards!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Image Not Showing...

    @Shahabuddin, welcome to the forums. Please note dates on posts. While this thread is only a month old, in forum times a month can be an eternity (and given the lack of activity one can often make the (more often than not) correct assumption the original poster has solved the problem or moved on).

    Quote Originally Posted by Shahabuddin View Post
    You should specify full path of your image.
    I would recommend against this approach, as doing so forces non-portability of the code beyond a given computer. Rather, provide a path relative to the class itself (which allows images to be deployed with code in a jar). I would recommend reading How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) , which describes the behavior of an ImageIcon if the image is not found, and how to make the appropriate checks for a correct file path.

Similar Threads

  1. Selecting and dragging a image from multiple image in Java Applet
    By CY5 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 02:44 PM
  2. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  3. Frame not showing Image until resized
    By gamalytical in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 13th, 2011, 07:09 AM
  4. Showing an Image on screen
    By doobybug in forum AWT / Java Swing
    Replies: 1
    Last Post: May 10th, 2011, 07:49 AM
  5. Can't figure out why my image isn't showing up!!
    By thrashingboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 06:01 PM