Computer Science Canada Java UI Help |
Author: | chromium [ 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. |
Author: | DemonWasp [ 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. The API documentation is here: http://docs.oracle.com/javase/1.4.2/docs/api/index.html . There's a tutorial on Swing here: http://docs.oracle.com/javase/tutorial/uiswing/TOC.html , but keep in mind that a lot of things may have changed between 1.4.2 and now. 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). For graphics, here's the tutorial: http://docs.oracle.com/javase/tutorial/2d/ ; but again keep in mind that some of those facilities may not exist in 1.4.2 . |
Author: | chromium [ 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:
Heres my code:
Thanks. |
Author: | DemonWasp [ 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:
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. |
Author: | chromium [ 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:
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 |
Author: | DemonWasp [ Thu May 09, 2013 1:53 pm ] | ||||||||
Post subject: | RE:Java UI Help | ||||||||
This line:
declares a local variable "points" and assigns it to a new TextField. However, this static member:
is never initialized because you initialized the local variable instead. That means it has the default value of null, so when execution gets to:
you are trying to reference through a null value ("points") rather than the text field you initialized earlier. In short:
|
Author: | chromium [ Fri May 10, 2013 4:27 pm ] |
Post subject: | Re: Java UI Help |
Thank you very much ![]() |
Author: | chromium [ 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.
Any advice would be greatly appreciated. ![]() |
Author: | chromium [ Tue May 28, 2013 12:22 pm ] |
Post subject: | Re: Java UI Help |
anyone?...i need HAAAAAAAAAAAALLLLPPPPPP ![]() ![]() ![]() ![]() ![]() |
Author: | DemonWasp [ 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. |
Author: | Zren [ 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 I can't see one. So we check with google. https://www.google.ca/search?q=java.awt.button+image Which has a link to stack overflow. http://stackoverflow.com/questions/6718944/how-to-make-awt-button-and-use-imageicon-icon 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. |
Author: | chromium [ 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. Heres my code:
If also attached some screenshots of how it looks normal and maximised. Any help would be much appreciated. |
Author: | DemonWasp [ 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. |
Author: | chromium [ 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:
Thanks |
Author: | chromium [ 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. |
Author: | chromium [ Mon Jun 10, 2013 10:16 am ] |
Post subject: | Re: Java UI Help |
Anyone know how? |
Author: | Zren [ Mon Jun 10, 2013 11:49 am ] | ||
Post subject: | RE:Java UI Help | ||
Each of your Frame instances is using the method defined in the Frame class (or whatever subclass defined it). In order to draw differently for a specific frame, you are going to need to subclass the Frame class and override the paint method.
Thing is, by overriding the paint method, you need to make sure to call super or the default behaviour (drawing all it's children probably) won't happen. |