Basically, I have one class where I've drawn a flower, and positioned it on the left of the screen
and another where I've drawn a sun in the top right.
How would I exactly draw the two together? In any way? All this JFrame and Component stuff is confusing me, can someone explain it?
Overall, i want users to be able to answer questions to decide which flower to draw. I really don't know how to do that either.
Can someone get me on the right track?
I think that the basic solution you want is to create two sub-components to sit side by side in your main frame/pane. To control where each component, be it a picture, some text, a button, etc.. sits you should use a layout manager. I am not sure I understand exactly what you want to achieve so I suggest you look through the options and choose which works best for you. Personally I think the gridLayout will do you justice. There are some good examples in the above link too.
Related
I was wondering if there was a way to make one component (in this case a JPanel) visible in more than one of the tabs.
I have a simple JFrame which is completely filled with a JTabbedPane. Each tab has a slightly different function, which is why they're in different tabs. However, they each output the same type of information. I was wondering if there was a way to make it so the same output panel could be seen in all the tabs, without having to create one panel for each tab.
I realize that it is impossible to add one component multiple times (and have it display independently each time), which is why I'm not optimistic about this being possible, however if it is, it would make my code much cleaner. In the case that this is not possible, I am completely open to alternate suggestions that achieve a similar result. I am in the very beginning of my project, so it won't be too difficult to change things.
This image gives a rough idea of what I'm trying to make it look like:
I have searched around for anyone dealing with this issue, however I have had little luck finding anything relevant. If I missed something, I apologize for wasting your time.
I'm sorry this is probably way too basic to be on here, but it's a subject I've been struggling with for about a month now and I don't know where else to go (as far as I know there is no "noob overflow", lol).
I'm trying to create a class that would:
1. put an image on a window (a JFrame, JPanel or other container)
2. be able to support keyboard and mouse listeners
3. could have multiple instances in the same container
So anyway I've tried all the usual places - Google, YouTube, the official Java site (sorry forgot the URL) and of course here on Stack Overflow - but haven't been able to find anything even remotely similar to what I'm trying to do.
Of course, I've also considered the possiblity that maybe it can't be done at all. There doesn't seem to be any kind of standard "JImage" or "JGraphic" that works like JButton or JLabel, and for whatever reason graphics requires a completely different list of (extremely involved) processes and procedures. As an example, in this post: How to "really" draw images in a Java app - it took me 60+ lines of code and 2 classes to just come close. That project didn't work in the end because for some reason it would only let me create one instance (even it you created 2-4 in the main method, it would only display the last one you told it to add).
But anyway, assuming that I'm not trying to "re-invent the wheel" here and it is actually possible (in Java), does anyone have an idea as to how (or at least know of a better site to study it)? Unfortunately most of the sites I've visited tend to assume you know all the inner workings of images (I know what a pixel is but that's about it - Buffers, Rastars etc. are still beyond me). It would be absolutely outstanding if there were a site that would explain it in layman's terms, if such a site exists. Thanks in advance.
Just use a plain old JLabel.
Regarding your requirements:
put an image on a window (a JFrame, JPanel or other container).
You can give a JLabel an ImageIcon of the image of interest and it will display it. This can then be easily placed in any other container such as a JPanel or JFrame.
be able to support keyboard and mouse listeners
Any component that extends JComponent, such as a JLabel allows for use of MouseListener, MouseMotionListener and can listen for keyboard input via Key Bindings.
could have multiple instances in the same container
You can add as many as you'd like to any container. Just be cognizant and respectful of the layout managers in use.
I have a project due in about an hour and I need to figure out the problem in my code before submitting.
In essence, there are two classes: one is a game and the other is a gui that should contain that game, some useless buttons, and a picture.
I'm having trouble making it all fit. I think I have a problem with the JPanels. (There might be some useless looking methods, but I had plans for them and I'll see if I have enough time for them to come into fruition)
This is life and death for my grade. I'd be eternally grateful if you could fix this code for me.
I'm positive the problem is not in the nitty gritty of the second link. It's been tested on its own and works.
Here's the code
http://pastebay.net/1236108
http://pastebay.net/1236109
Here's what I get when I run the code:
Get rid of the window in the Hangman constructor. Hangman is already frame.
Use pack instead of setSize
Git rid of game1 and game2 panels, they are adding nothing and causing you all the problems.
You will also find that if you override the getPreferredSize methods of DrawingDemoPanel and SubKillerPanel and return an appropriate size, that they will layout better.
Alternativly, you could create a single game panel and use a GridLayout to add the DrawingDemoPanel and SubKillerPanel to it, then add this to the CENTER position of the main window it will allow out better...
So this may seem strange, but I am trying to create a very basic game that works somewhat like what an older RPG game may have as the interface (think choosing attacks from a list (ex: Pokemon)).
Right now I have a class that extends JFrame, and it contains private variables of three panels: one for displaying the sprites at the top 75% of the screen, and the other two are the ones I wish to display text on (one for announcements like, "CRITICAL HIT" and the other for the selectable choices). The second of the text boxes would only be visible sometimes, but that is irrelevant.
Can anybody start me off or lend me a hand? I am finding many way but none that seem to work for my needs.
Edit: My game is laid out and the sprite panel works exactly as it should, as much as it may sound like it, I am not rushing into anything blindly. I have the game working except for displaying the dialogs in an effective way.
EDIT 2: Ok maybe I am being unclear, my basic concern is finding the best Java component to draw strings to the bottom panels. The strings will be changing regularly, so some methods that I have tried such as the Graphics drawString() are not very effective.
Thank you,
roflha
Well, your original question statement lacks any actual cohesive question, other than "Can anybody start me off or lend me a hand?" which makes it rather hard to figure out what you really need. I discerned that what you want help from us about is displaying dialogues that popup to the user when some event happens. If you don't care too much about special effects, there are three easy ways to do this:
1) Simply have sprites for your messages, and set them to visible as needed. If you have a limited number of important messages, this makes it easy to control the visibility/flashiness of the message.
2) JTextArea allows you to simply print some text to a box. It is useful if you have a wordy console or messages that can't simply be a few images. You would just have a JTextArea in your panel, and update it as needed:
JTextArea messageBox;
messagePanel.add(messageBox);
//displays a message
messageBox.setText("CRITICAL HIT!!!");
But the user may not notice when the text changes, since it changes instantly. Whether you want to flash the text, or display some animation on top of the text area is up to you.
3) If you want a more intrusive message, you can actually have a popup dialogue where the user would have to click "OK" to continue. This is relatively easy to do, and you can even put custom icons for the message:
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html
JOptionPane.showMessageDialog(myFrame,
"You got a critical hit!!!",
"Critical Hit",
JOptionPane.INFORMATION_MESSAGE,
criticalHitIcon);
I am creating a GUI program using MVC which should look like this..
alt text http://img137.imageshack.us/img137/6422/93381955.jpg
I have created a Window and Panel class. I am thinking of creating the Input and Display tabs in the Panel class and then creating two more classes, InputPanel and DisplayPanel. So the InputPanel will contain the stuff in this picture under the Input tab and same for the Display tab. Is there a better way to design this?
Also, since there are 3 sections in the Input tab (Name and sentence, crime, button), should I create 3 panels or just 1 panel containing all those?
Thanks
To answer your specific question about using three panels instead of 1, I would suggest two. There's rarely a need to create a panel just to create a single widget. So, one widget for the name and sentence, one for the crime.
As for the question about "is there a better way to design this"?... It sounds like you are learning, so I suggest you don't focus too much on the perfect way to do it. Stick with your original design then after the task is done ask yourself what worked* and what didn't. With that information you'll be able to decide for yourself whether what you did was the right design.
There usually isn't a "best" when designing GUI code -- there are many ways to solve the problem. What you've described sounds like a perfectly good way to attack the problem
(*) "worked" in this context means, was it easy to code? Did it allow you to achieve the layout you desired? Does it make the code maintainable over time if, for example, a requirement comes down to reorganize the GUI?.
Bryan gave good advices, I will just add that ergonomics isn't an exact science, although experience helps there.
Tabs are nice, eg. to separate settings, or group in a same panel (toolbox for example) different sets of tools (layers, colors, brushes...).
But they might not be adapted to all workflows. But we are lacking information about the role of the Display tab. Is it supposed to list all crimes in a table? Can't the table, if any, be below the controls?
As hinted by Bryan, it is better to design the GUI, then to test it, like would do a real user. Do you find the workflow easy to understand? (make somebody else to test it!) Does the usage feels natural? Is it fast to use?
Then you can adjust the design in light of these observations.
You were right to create InputPanel and DisplayPanel as seperate classes.
As for further splitting those panels? Yes you should further split them up, but not into separate classes. You should add jPanels inside of your InputPanel, and DisplayPanel, and group the controls within those internal jPanels.
Please let me know if you would like me to clarify what I mean.