Results 1 to 13 of 13
Thread: Custom icon not being displayed
- 05-02-2011, 08:41 PM #1
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
Custom icon not being displayed
I am working on a final project for my Java class and I have searched and searched to see what I am doing wrong because I am not able to get my icon to show up in my dialog box. In order for me to finish, I need that to show. If anyone can let me know what I am doing wrong, I would be much obliged. We haven't really spent any time on this icon thing which is why I am not able to figure it out. The icon is suppose to show after the initial question of if they want to play or not so that they can guess if the next roll of the die is going to be higher or lower. after trying over a half a dozen different code, this is what I gave up on.
import javax.swing.*;
public class P_Final_Exam {
public static void main(String[] args){
//ask player if they want to play
int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?");
if (answer == JOptionPane.NO_OPTION){
System.exit(1);
}
int dice = random();
diceRoll();
//JOptionPane.showMessageDialog(null, random());
}
public static void diceRoll(){
//dice roll
ImageIcon icon = new ImageIcon ("icons/" + dice + ".png");
Object[] options = {"Higher",
"Lower",
"Cancel"};
int n = JOptionPane.showOptionDialog(null,
"Will the next roll be higher?",
"Dice Game",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
icon,
options,
options[0]);
}
//ask player if their guess is higher or lower
//generate random number for die and icon
public static int random(){
int num;
num = (int)(Math.random()*6) + 1;
return num;
}
//calculate score
//display final score on exit
}
- 05-02-2011, 10:28 PM #2
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
I have redone the code and now have this which still isn't showing the icon.
import javax.swing.*;
public class P_Final_Exam {
public static void main(String[] args){
//ask player if they want to play
int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?");
if (answer == JOptionPane.NO_OPTION){
System.exit(1);
}
int dice = random();
//diceRoll();
//JOptionPane.showMessageDialog(null, random());
ImageIcon icon = new ImageIcon ("icons/" + dice + ".png");
Object[] options = {"Higher",
"Lower",
"Cancel"};
int n = JOptionPane.showOptionDialog(null,
"Will the next roll be higher?",
"Dice Game",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
icon,
options,
options[0]);
}
//public static void diceRoll(){
//dice roll and guess
//}
//generate random number for die and icon
public static int random(){
int num;
num = (int)(Math.random()*6) + 1;
return num;
}
//calculate score
//display final score on exit
}
- 05-02-2011, 11:46 PM #3
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Don't you get error when you run?
cuz i coppied your code and runed but
you actually haven't declared 'dice' in the diceRoll method.
so you better declare again because 'dice' variable is local variable in the main method.
or declare the 'dice' variable in class level not in the main method level
- 05-03-2011, 01:11 AM #4
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
that has nothing to do with what I am asking, I just would like to know what I am doing wrong for my icon not to show up and if you read my code again you will see that dice = random() in the main part and that I have // in front of things that I have yet to begin working on. I am taking this one step at a time and in order for me to know what is going on I need that icon to show up. I know that the dice variable is working just fine because I have tested that out.
- 05-03-2011, 01:46 AM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
ok i see.
well then your program should work
cuz i didn't change any your code and copied and runned it.
while it runs after i check 'Yes' for the first dialog, icon is showed up in the second dialog.
- 05-03-2011, 02:33 AM #6
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
which compiler are you using, I am using NetBeans and it isn't showing for me
-
are you sure you're looking in the right place for your images? Are you looking relative to the user directory?
- 05-03-2011, 03:09 AM #8
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
I have the icons folder in the same folder as the .java file, is that not were it goes for it to run
-
No, you're probably not looking where you think you're looking. To find out where Java is looking for the image, place this in your code:
Java Code:System.out.println(System.getProperty("user.dir"));
- 05-03-2011, 04:06 AM #10
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
I have no clue as to what you are talking about, we never got that far in the book. Our instructor only took us up to and including Chapter 6 which is single dimensional arrays. She only gave us that one line of code for the icon to use that I used.
that little code pretty much told me that I had the folder too far in so I just brought it out one and it works, ty for that helpLast edited by drewtrcy; 05-03-2011 at 04:13 AM. Reason: icon now shows
- 05-03-2011, 04:50 AM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 10
- 05-03-2011, 05:04 AM #12
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
I am using Netbeans as well.
And My directory for the icons is
in the project folder
for exampe... my project folder is 'IconProject' then the icons folder is 'icons' in the project folder
-
Similar Threads
-
Add custom icon to JOptionPane
By Return 0 in forum New To JavaReplies: 12Last Post: 01-27-2012, 01:44 PM -
TextField not displayed
By weezy2894 in forum Java AppletsReplies: 6Last Post: 04-18-2011, 02:37 AM -
Adding another icon to an icon
By whateverme in forum New To JavaReplies: 11Last Post: 03-29-2011, 08:00 PM -
Custom Icon Demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:19 PM -
Custom icon for Frame
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:38 AM
Bookmarks