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

Thread: Can't Import AWT Image into custom JPanel

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can't Import AWT Image into custom JPanel

    Hello, I'm a beginning Java programmer and I am having a little trouble importing images into a GUI.

    I am working on a Blackjack game which utilizes a GUI. I want to paint images of cards, words etc. without using ImageIcons or Labels etc. So I made a custom class which extends JPanel that will use Graphics2D.

    Everything works fine except I am unaware as to how I should import Image objects into my program to be painted. When in applets I simply use getImage(getCodeBase(),""); since I am in an applet, but this obviously doesn't apply in a JPanel. I am not interested in using a JApplet, so if there are any other ways to import images I would prefer to use those.

    EDIT: Should have read the top of the message board... either way if you guys actually need code I can provide it but I don't believe it is necessary.

    tl;dr How do you import an AWT Image into a JPanel?
    Last edited by JavaSquared; January 9th, 2011 at 01:01 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't Import AWT Image into custom JPanel

    I don't think it might be possible without ImageIcons, but you can without JLabels or JButtons or any of that sort.

    There's several paint methods for each type of GUI. One of those might involve painting an image.

    In fact, paint(Graphics g) has, in the Graphics class, a method to paint an Image to a JFrame. It probably has one for a JPanel too. However, you do need an Image or an ImageIcon or an Icon object. That part I can't figure out how to get around.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't Import AWT Image into custom JPanel

    However, to call, drawImage(), you'd need to have an ImageObserver object too.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Can't Import AWT Image into custom JPanel

    Use getClass().getResource(), where the parameter to getResource is the path (relative to the class in the package) the image is located. For example
    public class MyClass extends JPanel{
        Image image;
     
        public MyClass(){
            super();
            image = ImageIO.read(this.getClass().getResource("/image.jpg"));//indicates the image is in the root of the classpath, relative to the jar
        }
        ////paint using image
     
    }
    Last edited by copeg; January 8th, 2011 at 09:54 PM.

  5. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 8th, 2011)

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can't Import AWT Image into custom JPanel

    Thanks for the help, however, I am getting a type mismatch error.
    Its telling me that the getClass().getResource() is returning a URL and not an image.
    I figure now is a good time to share code. (I put some stuff in comments for clarification...)

    import java.awt.Image;
     
    public class Deck
    {
    int VALUE;
    String TYPE;
    String FACE;
    boolean DEALT;
    Image PICTURE;
    private boolean SHOWING;
     
        //The constructor gives each card its value, face, etc.
     
        public Deck(int value)
        {
            DEALT = false; 
            SHOWING = false; 
            switch(value%4)
            {
            case 0: TYPE = "Diamonds";break;
            case 1: TYPE = "Hearts";break;
            case 2: TYPE = "Spades";break;
            case 3: TYPE = "Clubs";break;
            default : TYPE = null;break;
            }
     
            switch(value%13)
            {
            case 0: VALUE = 10; FACE = "King";break;
            case 12: VALUE = 10; FACE = "Queen";break;
            case 11: VALUE = 10; FACE = "Jack";break;
            case 1: VALUE = 11; FACE = "Ace";break;
            default : VALUE = value%13; FACE = (""+value%13);break;
            }   
     
        }
     
      //I want the deal method to assign each card a picture ONLY when it is currently in play. I thought this     
      //would help the program run quicker as I don't have to import and keep 52 images in memory. Also, I  
      //need to sometimes assign certain cards a unique image of being face down, which will be accomplished 
      //by this if statement
     
     
        public void deal(boolean b, boolean vis)
        {
        DEALT = b;
        setVisible(vis);
     
        //My images are in a folder called "Cards". Also, I don;t know very much about
        //file management, and I'm using a pretty barebones IDE. (BlueJ)
        //My .java files are just kind of sitting in a folder called "Blackjack", "Cards" is located within "Blackjack".
     
        PICTURE = this.getClass().getResource("Cards/clubs-3-75.png"); 
     
             if(SHOWING == false)
            {}
            else
            {}
        }
     
        public void setValue(int x)
        {
        VALUE = x; 
        }
     
        public boolean getVisible()
        {return SHOWING;}
     
        public void setVisible(boolean b)
        {SHOWING = b;}
     
        public String toString()
        {
        return ""+FACE+" of "+TYPE;
        }
     
    }

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Can't Import AWT Image into custom JPanel

    Sorry, that was my fault...I edited the code...you should be able to read the returned URL using ImageIO.

  8. The Following User Says Thank You to copeg For This Useful Post:

    JavaSquared (January 9th, 2011)

  9. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can't Import AWT Image into custom JPanel

    Thanks for the help.
    After I added the exceptions etc. it at least compiled... but didn't paint anything.
    So I did a little more hunting and found this...

    Image i = Toolkit.getDefaultToolkit().getImage ( "" );

    Apparently, this looks within the source folder and finds the image... and it works.
    And I didn't have to use try/catch statements, thanks a ton for the help anyway.

    Don't know if I can lock my own thread, but I'll set it to solved.

    EDIT: Or maybe I won't... ah well for anyone reading this is solved so you can lock it.
    Last edited by JavaSquared; January 9th, 2011 at 01:01 AM.

Similar Threads

  1. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 08:28 PM
  2. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  3. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  4. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 09:02 AM
  5. How to import an .tiff image in JSP?
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: May 12th, 2008, 06:55 AM