Results 1 to 11 of 11
Thread: image for a button
- 01-29-2012, 08:37 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
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.
the dPiece.getImage returns a BufferedImage. I think that I should be able to putJava Code:Image imgForIcon = (Image) dPiece.getImage(); ImageIcon imgIcon = new ImageIcon( imgForIcon); JButton newDominoButton = new JButton(imgIcon);
but want to do it the other way currently. I am getting the following error thoughJava Code:JButton newDominoButton = new JButton(new ImageIcon (dPiece.getImage()));
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
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.Java Code:Object o = image.getProperty("comment", imageObserver);
-
Re: image for a button
I think your NPE is coming from somewhere else.
Can you change this:
To this:Java Code:Image imgForIcon = (Image) dPiece.getImage(); // no need to cast! ImageIcon imgIcon = new ImageIcon( imgForIcon); JButton newDominoButton = new JButton(imgIcon);
Tell us what gets printed out.Java Code:Image imgForIcon = dPiece.getImage(); System.out.println("imgForIcon is null: " + (imgForIcon == null)); ImageIcon imgIcon = new ImageIcon( imgForIcon); JButton newDominoButton = new JButton(imgIcon);
- 01-29-2012, 08:52 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
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
If you see anything I am doing wrong let me know where to look and I will do my best to fix it. ThanksJava 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"); }
-
Re: image for a button
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).
Sounds good to me, but you can test every step in the process similarly to find out where the mess up is occurring.I know that I read the file correctly so I think it was screwed up where I stored it.
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?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" ));
- 01-29-2012, 09:04 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
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
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.Java Code:int side1, side2; BufferedImage dominoImage; DominoPiece(int s1, int s2, BufferedImage dImage) { side1 = s1; side2 = s2; dominoImage = dImage; }
-
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.
- 01-29-2012, 09:17 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: image for a button
Okay here goes. Here is my DominoPiece class
I have a DominoTable class where I create a list of DominoPieces. Here is the code that creates my listJava 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())); } }
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.
- 01-29-2012, 09:26 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
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.
-
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?
- 01-29-2012, 09:45 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
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.
-
Similar Threads
-
How to use image as button
By mangesh.gho in forum New To JavaReplies: 5Last Post: 01-09-2012, 06:02 AM -
Make a button class that uses your button image.
By eLancaster in forum New To JavaReplies: 1Last Post: 04-26-2011, 11:32 AM -
AWT button image
By timkd127 in forum AWT / SwingReplies: 2Last Post: 03-01-2010, 04:41 AM -
Set focus on image button in swt
By diva_garg in forum New To JavaReplies: 3Last Post: 08-18-2008, 12:27 PM -
Get Focus on Image button
By diva_garg in forum SWT / JFaceReplies: 0Last Post: 08-08-2008, 11:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks