Posted: Sun Apr 28, 2013 4:41 pm Post subject: Java UI Help
Hello. Ive just started using Java in school, and unfortunately we are using Ready to Program. My final project is coming up and im planning making a game with a decent ui.
Ive read/watched some tutorials but rtp gives me errors when running apps that are using javax.swing. Im thinking of making some sort of whack-a-mole game where theres a 5x5 grid of buttons and a mole will randomly appear on one, then you have to click on it to get points. The game also has to have an educational component, so ill probably have a popup that comes up every so often where the user needs to complete a math question, which will then increment the level counter. Heres what im thinking it should look like:
I really need help here, so if anyone could suggest some tips about where i should get started or point me in the right direction.
Also from what ive dealt with over the past couple days, rtp seems useless, so i might also consider using a different ide. If anyone could let me know if
there are any ide's/editors that would be better in this case, that would be great. I cant use anything thats too different from rtp, but anything thats more practical would be nice.
Thanks in advance.
Sponsor Sponsor
DemonWasp
Posted: Sun Apr 28, 2013 8:40 pm Post subject: RE:Java UI Help
JCreator is the IDE I would call most similar to Turing. Most professionals use Eclipse, Netbeans or IntelliJ, but they are massive overkill for a beginner.
The most likely reason that you're getting errors when dealing with RTP and javax.swing is that RTP uses a built-in JVM with version 1.4.2 (or something like that), which means you can't just refer to the latest documentation or tutorials, you have to use versions that are around 10 years out of date.
If your teacher needs to be able to run your code, then you need to make sure you set up your IDE to the same compatibility level as your teacher will be using (probably whatever RTP uses). The exact procedure for that will vary depending on your IDE, and will require that you install the appropriate JVM first (you may not be able to do that at school).
Posted: Sat May 04, 2013 7:48 am Post subject: Re: Java UI Help
Thanks the API was very useful. Ive managed to setup the ui, and ive added actionlisteners to all buttons. Ive set it to setText of a textfield to the variable score, when the buttons are clicked, but when i run the program and click on any button i get this error:
code:
Exception occurred during event dispatching:
java.lang.NullPointerException
at WAM.actionPerformed(WAM.java:299)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
public class WAM implements ActionListener
{
static Random rand;
static Button b1, b2, b3, b4, b5, b6, b7, b8, b9;
static TextField points;
static int score;
public WAM ()
{
rand = new Random ();
int score = 0;
Frame f = new Frame (); //creates console frame (f)
f.setVisible (true);
f.setTitle ("MyConsole");
f.setSize (600, 500);
f.setLocation (200, 200);
Color greenBG = new Color (0x30A156);
f.setBackground (greenBG);
Label l = new Label ("Whack-A-Mole"); //title
Color greenTitle = new Color (0x0A4414);
l.setForeground (greenTitle);
Font myFont = new Font ("sansserif", Font.BOLD + Font.ITALIC, 27);
l.setFont (myFont);
l.setVisible (true);
l.setSize (200, 30);
l.setLocation (205, 75);
f.add (l);
Label l2 = new Label ("Game by Priyesh Patel"); //subtitle
Color greenSubTitle = new Color (0x7CFC92);
l2.setForeground (greenSubTitle);
Font myFont2 = new Font ("monospaced", Font.PLAIN, 13);
l2.setFont (myFont2);
l2.setVisible (true);
l2.setSize (200, 30);
l2.setLocation (215, 100);
f.add (l2);
TextField t = new TextField (); //textfield
t.setVisible (true);
t.setEditable (false);
t.setSize (200, 20);
t.setLocation (200, 200);
f.add (t);
for (int x = 0 ; x <= 50 ; x++) //loop which assigns mole to buttons randomly
{
int speed = 700; //how fast the color switches in milliseconds
int num = rand.nextInt (9); //random num generator
Color greenButton = new Color (0x34C318); //flash color
public static void main (String[] args)
{
new WAM ();
} // main method
} // WAM class
Thanks.
DemonWasp
Posted: Sat May 04, 2013 12:54 pm Post subject: RE:Java UI Help
The cause is right there in the exception: something is null but you tried to use it anyway, so you got a NullPointerException. If you look at the stack trace, the top line is where the exception was initially thrown (WAM.actionPerformed() at line 299 of WAM.java) and each successive line represents part of the call stack. In your case, the points variable is null (we know that it isn't score because score is an int and cannot be null).
There are also some serious code-repetition problems here. In general, don't repeat yourself: say any given thing exactly once.
For example, you often want to sleep for some number of milliseconds. Since you're ignoring the possible InterruptedException anyway, why not add a method:
Then you can replace 9 instances of that try-catch with a single sleep(speed).
You can do similar things to make setting up buttons easier.
You should also put your buttons in an array, which will make your life much easier. For example, the "game loop" that moves the moles around could be:
Java:
// If these don't change in your loop, then you can leave them outside.
// This could be important for the Color object, because you keep creating new ones on each iteration--oops!
int speed = 700; //how fast the color switches in milliseconds
Color green = new Color (0x34C318); //flash color
for (int x = 0 ; x <= 50 ; x++) {
// Note: according to the documentation, random.nextInt(9) will generate numbers 0, 1, ..., 7, 8 (but not 9!)
int num = rand.nextInt (9); //random num generator
If you follow this pattern throughout your program, you should be able to delete the majority of your code. This means you can replace a lot of long, difficult-to-read code with shorter, easier-to-read code, which means fewer bugs and less hassle to change/improve things.
chromium
Posted: Thu May 09, 2013 1:12 pm Post subject: Re: Java UI Help
Thanks again for your help. I fixed the random number generator by declaring it like this:
code:
int num = (rand.nextInt (9)) + 1;
I also used the method for the delay instead of remaking it each button.
However i still cant figure out how to fix the error when clicking on the buttons. Ive had a few people check it and they dont know how either. Could you please be a bit more specific in how to fix it? Im not really sure what you meant.
Thanks
DemonWasp
Posted: Thu May 09, 2013 1:53 pm Post subject: RE:Java UI Help
This line:
code:
TextField points = new TextField ("0"); //textfield for points
declares a local variable "points" and assigns it to a new TextField.
However, this static member:
code:
static TextField points;
is never initialized because you initialized the local variable instead. That means it has the default value of null, so when execution gets to:
code:
points.setText (Integer.toString (score));
you are trying to reference through a null value ("points") rather than the text field you initialized earlier.
In short:
code:
public class Example {
private static TextField points; // this is the field you wanted to initialize
public Example() {
...
TextField points = new TextField(...); // this does NOT initialize the 'points' field above (it creates and initializes a local variable with the same name, which "shadows" the class member above).
...
...
points = new TextField(...); // this will NOT create a local variable; it will initialize the member field above.
...
}
}
chromium
Posted: Fri May 10, 2013 4:27 pm Post subject: Re: Java UI Help
Thank you very much ...Fixed it
chromium
Posted: Sun May 26, 2013 6:41 pm Post subject: Re: Java UI Help
Hello again. I need some help on how to add an image to the background of a java awt button.
I understand that you can use this for JButtons but it doesnt seem to work on my buttons.
code:
ImageIcon pic = new ImageIcon("pic.gif");
button1.setIcon(pic);
Any advice would be greatly appreciated.
Sponsor Sponsor
chromium
Posted: Tue May 28, 2013 12:22 pm Post subject: Re: Java UI Help
anyone?...i need HAAAAAAAAAAAALLLLPPPPPP
DemonWasp
Posted: Tue May 28, 2013 1:06 pm Post subject: RE:Java UI Help
Try reducing the size of the problem. That is, try the icons-on-buttons thing in isolation.
Open a new file, and try to write the smallest possible program to put a button in a frame with an icon set. If it works there, then try to figure out what's different between that smaller example and the larger code you were working on.
If it still doesn't work, post your code here with an explanation of what it does, what you expect it to do, any errors you encounter, etc.
Zren
Posted: Tue May 28, 2013 1:13 pm Post subject: RE:Java UI Help
I'm guessing there was an error when you ran that code, what was it? How do you interpret that error?
~
First look in the docs. Specifically, you'd be looking for a property you can set. So look at the setters (functions starting with "set" Eg: setBlarg). Look at both the list of functions that are defined in this class, and the list of those that are inherited.
http://docs.oracle.com/javase/6/docs/api/java/awt/Button.html
Which says that the AWT buttons don't natively support images on them. Instead it links to a tutorial on extending the class to create that feature.
chromium
Posted: Fri May 31, 2013 1:22 pm Post subject: Re: Java UI Help
Ive decided to convert my program UI to swing (JFrame, JButtons, etc.). With awt it seemed inefficient and a lot of the resources i found werent working properly.
I managed to do this in about 5 mintues but i have an issue. When i run the program it comes up with a blank window (without any of my components) however if i maximise the window everything shows up as it should.
//frames
static JFrame f0 = new JFrame ("Configuration"); //config window
static JFrame f = new JFrame (); //creates game console frame (f)
static JFrame f1 = new JFrame (); //creates math frame
//Colors
static Color greenJButton = new Color (0x34C318); //green flash color;
static Color greenBG = new Color (0x30A156); //green background color
//numbers
static int pointsNum = 0; //number located in points textfield
static int levelNum = 1; //number located in level textfield
static int speed = 700; //how fast the color switches in milliseconds (defaults to 700 if no speed selected)
static Random rand;
//fonts
static Font myFont = new Font ("sansserif", Font.BOLD + Font.ITALIC, 27); //Bold Italic
static Font myFont2 = new Font ("monospaced", Font.PLAIN, 13); //Courier New
static Font myFont3 = new Font ("sansserif", Font.BOLD, 14); //Bold
static Font myFont4 = new Font ("sansserif", Font.PLAIN, 15); //Normal
selected = new JLabel (); //displays the speed that the user has selected
selected.setVisible (true);
selected.setLocation (260,151);
selected.setSize (100,20);
f0.getContentPane().add (selected);
rand = new Random ();
for (int x = 0 ; x <= 50 ; x++) //loop which assigns mole to buttons randomly
{
int num = (rand.nextInt (9)) + 1; //random num generator
int math1 = (rand.nextInt (50)) + 1; //random math int1
int math2 = (rand.nextInt (50)) + 1; //random math int2
mathJLabel1 = new JLabel ("" + math1); //label for first random number
mathJLabel1.setVisible (true);
mathJLabel1.setFont (myFont3);
mathJLabel1.setLocation (90, 75);
mathJLabel1.setSize (20, 20);
f1.getContentPane().add (mathJLabel1);
mathJLabel2 = new JLabel ("" + math2); //label for second random number
mathJLabel2.setFont (myFont3);
mathJLabel2.setVisible (true);
mathJLabel2.setLocation (130, 75);
mathJLabel2.setSize (20, 20);
f1.getContentPane().add (mathJLabel2);
If also attached some screenshots of how it looks normal and maximised. Any help would be much appreciated.
normal.JPEG
Description:
Filesize:
11.67 KB
Viewed:
7839 Time(s)
maximised.JPG
Description:
Filesize:
33.82 KB
Viewed:
202 Time(s)
DemonWasp
Posted: Fri May 31, 2013 2:45 pm Post subject: RE:Java UI Help
Google is your friend. Try the phrase "jframe blank". The top link is to a very similar question asked (and answered!) on StackOverflow. Read the answer posted there, it will help.
chromium
Posted: Mon Jun 03, 2013 8:08 pm Post subject: Re: Java UI Help
Ok ive gone back to using awt. Do you guys have any ideas of how i can make my main game frame restart after completing the first round?
Ive made it so that after getting to 50 points (where the next level will start) it will make the frame invisible and then after asnwering the math question correct it will set it to visible again. However this doesnt seem like a good way to go about this. All of the variables stay the same and it doesnt work well.
Heres my code so far:
//frames
static Frame f0 = new Frame ("Configuration"); //config window
static Frame f = new Frame (); //creates game console frame (f)
static Frame f1 = new Frame (); //creates math frame
//Colors
static Color greenButton = new Color (0x34C318); //green flash color;
static Color greenBG = new Color (0x30A156); //green background color
//numbers
static int pointsNum = 0; //number located in points textfield
static int levelNum = 1; //number located in level textfield
static int speed = 700; //how fast the color switches in milliseconds (defaults to 700 if no speed selected)
static int math1; //random math int1
static int math2; //random math int2
//fonts
static Font myFont = new Font ("sansserif", Font.BOLD + Font.ITALIC, 27); //Bold Italic
static Font myFont2 = new Font ("monospaced", Font.PLAIN, 13); //Courier New
static Font myFont3 = new Font ("sansserif", Font.BOLD, 14); //Bold
static Font myFont4 = new Font ("sansserif", Font.PLAIN, 15); //Normal
selected = new Label (); //displays the speed that the user has selected
selected.setVisible (true);
selected.setLocation (260,151);
selected.setSize (100,20);
f0.add (selected);
rand = new Random ();
for (int x = 0 ; x <= 10000 ; x++) //loop which assigns mole to buttons randomly
{
int num = (rand.nextInt (9)) + 1; //random num generator
math1 = (rand.nextInt (50)) + 1; //random math int1
math2 = (rand.nextInt (50)) + 1; //random math int2
mathLabel1 = new Label ("" + math1); //label for first random number
mathLabel1.setVisible (true);
mathLabel1.setFont (myFont3);
mathLabel1.setLocation (90, 75);
mathLabel1.setSize (20, 20);
f1.add (mathLabel1);
mathLabel2 = new Label ("" + math2); //label for second random number
mathLabel2.setFont (myFont3);
mathLabel2.setVisible (true);
mathLabel2.setLocation (130, 75);
mathLabel2.setSize (20, 20);
f1.add (mathLabel2);
Posted: Sat Jun 08, 2013 9:14 am Post subject: Re: Java UI Help
How to I add a background image to my frame?
I understand that I should use paint since I'm using awt, but I have 3 different frames and I'm confused about how I would specificy which frame to paint the image to.
Please halp me, and thanks in advance.