So i tried to research this. I tried using this magic 8 ball code i got from somewhere else but i want to use my own image when the J panel pops up to ask a question:
import java.security.SecureRandom;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Magic8Ball {
private final static ImageIcon image = new
ImageIcon(this.getClass().getResource("BuckminsterFuller.jpg"));
private final static SecureRandom randomNumber = new SecureRandom();
private final static String answers[] = {
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes - definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Signs point to yes",
"Yes",
"Reply hazy, try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful" };
public static void main(String[] args) {
boolean askQuestion = true;
while (askQuestion) {
String question = getUserQuestion();
String randomAnswer = getRandomAnswer();
displayAnswer(question, randomAnswer);
askQuestion = userWantsToAskAnotherQuestion();
}
showExitMessage();
}
private static String getUserQuestion() {
return JOptionPane.showInputDialog(null,
"PLease enter a yes or no question:",
"WELCOME: Have your questions answered!",
JOptionPane.INFORMATION_MESSAGE);
}
private static String getRandomAnswer() {
return answers[randomNumber.nextInt(answers.length)];
}
private static void displayAnswer(String question, String randomAnswer) {
JOptionPane.showMessageDialog(null, question + "\n" + randomAnswer, "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);
}
private static boolean userWantsToAskAnotherQuestion() {
return 0 == JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", JOptionPane.YES_NO_OPTION, 0, image);
}
private static void showExitMessage() {
JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", JOptionPane.PLAIN_MESSAGE, image);
}
}
I tried saving the image as BuckminsterFuller.jpg in the directory where the class is at, in a separate folder called "images" where the project, src, build, and class is at.
It gives me this error:
java.lang.ExceptionInInitializerError Caused by:
java.lang.RuntimeException: Uncompilable source code - non-static
variable this cannot be referenced from a static context at
Assignment6.Magic8Ball.<clinit>(Magic8Ball.java:10)