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: My image doesn't always appear on the screen?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default My image doesn't always appear on the screen?

    Hi guys I've been having this problem lately with an image I added to my panel. Whenever I compile and run my program the image sometimes appear on screen but most of the time it doesn't. I really don't know what's going on so I would really appreciate the help

    *The dimension of the image is 500x256

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class GameMainZombie {
     
    	public static void main(String[] args)	{
     
    	GameMainZombieGUI();
     
    	}
     
    	public static void GameMainZombieGUI() {
     
    	try	{
    	final JFrame pageMain = new JFrame("ZOMBIE GAME");
    	pageMain.setVisible(true);
    	pageMain.setSize(500,600);
    	pageMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	JPanel panel = new JPanel((LayoutManager) new GridBagLayout());
    	pageMain.getContentPane().add(panel, BorderLayout.CENTER);
    	GridBagConstraints c = new GridBagConstraints();
     
    	JButton Start = new JButton("START GAME");
    	Start.addActionListener(new ActionListener() { 
    		public void actionPerformed(ActionEvent e)	{
    		GameLocationGUI(); pageMain.setVisible(false);	}
    	});
    	c.gridx = 0;
    	c.gridy = 1;
    	c.insets = new Insets(10,10,10,10);
    	panel.add(Start, c);
     
    	ImageIcon image = new ImageIcon("Zombies.jpg");
    	JLabel main = new JLabel(image);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel.add(main, c);
     
    	}catch(Exception e)	{}
     
    	}


  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: My image doesn't always appear on the screen?

    One possible problem is the call to setVisible() is done BEFORE everything is ready to be seen. Try moving the call to after everything is ready.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: My image doesn't always appear on the screen?

    Your label is getting on the screen. The issue is that it is not finding the file. I would google "load image with java" to see how to load an image file from your source code.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    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: My image doesn't always appear on the screen?

    the image sometimes appear on screen
    That sounds like a timing thing, not a location problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  2. Does anyone know how to display an image from a file on your screen?
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:06 PM
  3. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 12:29 AM
  4. Need help making an image move from one side of screen to the other.
    By Xillius200 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 25th, 2011, 12:46 PM
  5. Showing an Image on screen
    By doobybug in forum AWT / Java Swing
    Replies: 1
    Last Post: May 10th, 2011, 07:49 AM