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

Thread: Problem with image in swing gui

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem with image in swing gui

    I'm making a dice program that will displace a random image of a dice between 1 and 6, my problem is displaying and image in my program.

    Here's my GUI source code:

    package terninggui;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class GUI extends JPanel 
    {
        Image ener = Toolkit.getDefaultToolkit().getImage("ener.png");
        Terning minTerning = new Terning();
        public void paintComponent(Graphics g){      
            g.drawImage(ener, 10,10,this);
        }
     
    }

    And here's my main method source code:

    package terninggui;
    import javax.swing.*;
    public class TerningGUI {
        public static void main(String[] args) {
            JFrame Vindue = new JFrame("Terning");
            Vindue.add( new GUI());
            Vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            Vindue.setSize(200,200);
            Vindue.setVisible(true);
        }
    }

    I've got my image in my source folder as seen in this screen shot


    Also worth noting in this screen shot is that the image name is in orange instead of blue, as if it's just a text, I don't how much it matters, but it's one of the things worth noting.

    Thanks for reading my post, and I hope you can help, and I apologize for the danish in the source code, this is just ment to be a "training" project for me
    Attached Images Attached Images
    Last edited by Pusillus; June 14th, 2012 at 04:04 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,587
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Problem with image in swing gui

    Can you explain what your problem is?

    The paintComponent method can be called many times. You should read the image file outside of the paintComponent method so it is only read one time.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Pusillus (June 14th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with image in swing gui

    My problem is that the image doesn't show, when I run the program, all I get is an empty window.

    edit:
    I moved it so I read my image outside the paintComponent, but it still doesn't show.
    Last edited by Pusillus; June 14th, 2012 at 04:03 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,587
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Problem with image in swing gui

    The code does not compile because the Terning class is missing.
    When I remove that line and put in my image file, the code compiles and shows the image.
    Make sure the image is in the folder where the program is looking for it to be.
    Last edited by Norm; June 14th, 2012 at 08:29 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Pusillus (June 14th, 2012)

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with image in swing gui

    Thanks a lot Norm that cleared up my issue :-D

Similar Threads

  1. IO-swing problem
    By haresh2906 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 06:00 AM
  2. [SOLVED] Problem with swing-buttons
    By Minken in forum AWT / Java Swing
    Replies: 8
    Last Post: October 15th, 2010, 09:20 PM
  3. simple game with swing and awt problem
    By Pulse_Irl in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2010, 02:04 PM
  4. [SOLVED] Java Swing problem?
    By nasser in forum AWT / Java Swing
    Replies: 2
    Last Post: July 3rd, 2010, 12:34 PM
  5. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM

Tags for this Thread