Results 1 to 11 of 11
  1. #1
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default image for a button

    Hello,

    I have a class where I am trying to create a button that has a image on it. This is my code for it.
    Java Code:
    Image imgForIcon = (Image) dPiece.getImage();
    ImageIcon imgIcon = new ImageIcon( imgForIcon);
    JButton newDominoButton = new JButton(imgIcon);
    the dPiece.getImage returns a BufferedImage. I think that I should be able to put
    Java Code:
    JButton newDominoButton = new JButton(new ImageIcon (dPiece.getImage()));
    but want to do it the other way currently. I am getting the following error though

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:228)

    Through looking at the ImageIcon class I see that the line throwing the error is
    Java Code:
    Object o = image.getProperty("comment", imageObserver);
    Is this because I have not set a "comment" property. Through reading the description of the getProperty method I thought it was optional to have a comment property. Since I don't know how to add a property to the BufferedImage I didn't think it mattered. Any help is appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: image for a button

    I think your NPE is coming from somewhere else.

    Can you change this:
    Java Code:
    Image imgForIcon = (Image) dPiece.getImage(); // no need to cast!
    ImageIcon imgIcon = new ImageIcon( imgForIcon);
    JButton newDominoButton = new JButton(imgIcon);
    To this:
    Java Code:
    Image imgForIcon = dPiece.getImage(); 
    System.out.println("imgForIcon is null: " + (imgForIcon == null));
    ImageIcon imgIcon = new ImageIcon( imgForIcon);
    JButton newDominoButton = new JButton(imgIcon);
    Tell us what gets printed out.

  3. #3
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: image for a button

    I changed what you said and this is what I got
    imgForIcon is null: true
    I see what you were doing. This means that the imgForIcon obj is null right. I know that I read the file correctly so I think it was screwed up where I stored it. Pretty much I have a object called DominoPiece that has two int values and a BufferedImage object. When I create the DominoPiece I read in a image file using
    Java Code:
    File check = new File("C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\" + imageFileName);
    if(check.exists())
    {
          System.out.println("I exist");
          ImageIO.read(new File("C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\dominoe66.jpg" ));
    }else
    {
          System.out.println("I tried to read " + check);
          System.out.println("I don't exist");
    }
    If you see anything I am doing wrong let me know where to look and I will do my best to fix it. Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: image for a button

    Quote Originally Posted by angrodm View Post
    I changed what you said and this is what I got
    imgForIcon is null: true
    I see what you were doing. This means that the imgForIcon obj is null right.
    Yes, that is correct. It's known as using println's as a PMD (a poor man's debugger), a debugging technique I use often (just remember to remove them from the final product).

    I know that I read the file correctly so I think it was screwed up where I stored it.
    Sounds good to me, but you can test every step in the process similarly to find out where the mess up is occurring.

    Pretty much I have a object called DominoPiece that has two int values and a BufferedImage object. When I create the DominoPiece I read in a image file using

    Java Code:
    File check = new File("C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\" + imageFileName);
    if(check.exists())
    {
          System.out.println("I exist");
          ImageIO.read(new File(
             "C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\dominoe66.jpg" ));
    OK, I sense a problem here. ImageIO should be able to read in the Image file, but where are you storing the BufferedImage that is returned? Shouldn't you be assigning it to a field of the class in the line above?

  5. #5
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: image for a button

    I am assigning it to the DominoPiece object. I am creating a list with an object for each domino piece. Pieces are [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [1,1] ... [6,6] each object has the following parameters
    Java Code:
    int side1, side2;
    BufferedImage dominoImage;
    
    DominoPiece(int s1, int s2, BufferedImage dImage)
    {
          side1 = s1;
          side2 = s2;
          dominoImage = dImage;
    }
    This is the summarized code. I have the variables set to private and they all have corresponding setter's and getter's. Also there is an equals method that determines if 2 pieces are equal by making sure the sides are equal. I can post the full code if you want but I just didn't want to overload the thread with a wall of code. :) If you want I will post the full DominoPiece code and the full code where I am creating the pieces.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: image for a button

    I want to see where you read the image file and assign it to an Image variable because I've yet to see code that shows this.

  7. #7
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: image for a button

    Okay here goes. Here is my DominoPiece class
    Java Code:
    /*
     * Created by Matt Tackett
     * 1/17/2012
     */
    import java.awt.image.*;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    class DominoPiece {
    
        private int side1, side2; //These hold the value of the sides of the piece.
        /*
         * dominoImg the image showing the dots on the dominos
         * dominoBack the image showing the back of the domino
         * currentImg what image is being displayed
         */
        private BufferedImage dominoImg, dominoBack, currentImg;
        
        private boolean revealed;//This says whether the domino is revealed or not
        private boolean isSpinner;//this says whether the domino is the spinner piece or not.
        
        DominoPiece(int s1, int s2, BufferedImage dImg) {
            side1 = s1;
            side2 = s2;
            dominoImg = dImg;
            try
            {
                dominoBack = ImageIO.read(new File("C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\" + "dominoeBack.jpg"));
            }
            catch (IOException e)
            {
                System.err.println("error");
            }
            revealed = false;
            isSpinner = false;
        }
        
        //This function is called to update the image based on whether the domino is revealed or not.
        void updateImage()
        {
            if(revealed)
                currentImg = dominoImg;
            else
                currentImg = dominoBack;
        }
        
        BufferedImage getImage()
        {
            System.out.println("returning the currentImg as a BufferedImage");
            return currentImg;
        }
    
        /*
         * The following functions are the getter's and setters for the side1 and
         * side2 variables.
         */
        int getSide1()
        {
            return side1;
        }
        
        void setSide1(int side)
        {
            side1 = side;
        }
    
        int getSide2()
        {
            return side2;
        }
        
        void setSide2(int side)
        {
            side2 = side;
        }
        
        void setRevealed(boolean isRevealed)
        {
            revealed = isRevealed;
        }
        
        boolean getRevealed()
        {
            return revealed;
        }
        
        void setIsSpinner(boolean isSpin)
        {
            isSpinner = isSpin;
        }
        
        boolean getIsSpinner()
        {
            return isSpinner;
        }
        
        public boolean equals(Object o){
            if(this == o)
                return true;
            if(!(o instanceof DominoPiece))
                return false;
            DominoPiece testing = (DominoPiece) o;
            return ((testing.getSide1() == this.getSide1())
                    && (testing.getSide2() == this.getSide2()));
    
        }
    }
    I have a DominoTable class where I create a list of DominoPieces. Here is the code that creates my list

    Java Code:
    private void createDominoes()
        {
            System.out.println("In createDominoes method");
            BufferedImage dominoeImage = null;
            String imageFileName = "dominoe";
            for(int s1 = 6; s1 >= 0; s1--)
            {
                for(int s2 = s1; s2 >=0; s2--)
                {
                    try
                    {
                        imageFileName += Integer.toString(s1) + Integer.toString(s2) + ".jpg";
                        File check = new File("C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\" + imageFileName);
                        if(check.exists())
                        {
                            System.out.println("I exist");
                        }else
                        {
                            System.out.println("I tried to read " + check);
                            System.out.println("I don't exist");
                        }
                        dominoeImage = ImageIO.read(new File("C:\\Users\\Matt\\Documents\\NetBeansProjects\\DominoeGame\\Images\\" + imageFileName));
                    }
                    catch (IOException e)
                    {
                        System.err.println("error");
                    }
                    DominoPiece newPiece = new DominoPiece(s1, s2, dominoeImage);
                    shakeList.add(newPiece);
                    imageFileName = "dominoe";
                }
            }
        }
    Last edited by angrodm; 01-29-2012 at 09:22 PM.

  8. #8
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: image for a button

    Don't know why but it is showing the first part of the first code section as all comments. They are not commented out it is just the color formatting for some reason that is showing it as commented out.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: image for a button

    Your code display problem is a forum bug.

    Regarding the code itself, where do you set the currentImg to a non-null value?

  10. #10
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: image for a button

    I feel like a huge idiot. I had that line originally in the constructor but must have deleted it for some reason. Thank you so much. I will add it in and run a few tests when I get back from some errands I have to run. Thank you again so much and I will let you know how it turned out a little later.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: image for a button

    You're welcome, and please do let us know how things turn out. Best of luck!

Similar Threads

  1. How to use image as button
    By mangesh.gho in forum New To Java
    Replies: 5
    Last Post: 01-09-2012, 06:02 AM
  2. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  3. AWT button image
    By timkd127 in forum AWT / Swing
    Replies: 2
    Last Post: 03-01-2010, 04:41 AM
  4. Set focus on image button in swt
    By diva_garg in forum New To Java
    Replies: 3
    Last Post: 08-18-2008, 12:27 PM
  5. Get Focus on Image button
    By diva_garg in forum SWT / JFace
    Replies: 0
    Last Post: 08-08-2008, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •