How would I make an In-game window?
So to put this simply, I'm making a game. the window for the game is not full screen, it is only 1000 by 800 pixels. for this game, i am making a blueprint class for in-game windows, so that i can have a window for inventories, stores, main menu, pretty much anything you can think of. So, I've tried using JDialogs, but they are separate windows, and I kind of want it to be painted to the main panel... kind of like a JLabel, but as a window. I've been doing a lot of google searches, and tried a lot of different things, such as the aforementioned dialog, and label, but I'm just not sure how to do this. All I need is an in-game window/menu. does anyone know how to do this? Thanks in advance!
Additinal info: if anyone has played Guild Wars 2, or World of Warcraft, you know what i'm talking about. Think about the separate inventory windows, and store windows, etc.
EDIT: I tried what the comments suggested, and here's how I initialized the new panel:
setSize(w, h);
setLayout(null);
setBackground(new Color(0, 0, 0, 0));
And I add this panel using mainFrame.add(new GameMenu()); and nothing happens. When I make the frame, in the main method, I do it this way:
f = new Frame();
core.setFrame(f);
f.add(new GameMenu());
f.add(new Panel()); //panel is the main game panel
f.setVisible(true);
And it shows up and works just fine. So what is different about this, and why is my first way not working and the second work?
JInternalFrame might be what you are looking for. An internal frame is normally added to a JDesktopPane but it also works if you don't want that overhead. The Java Tutorial has a good introduction.
Figured it out! all i had to do was just make a smaller JPanel (cause i didnt know that you could make it do that XD) and have it do what i want! Thanks for the help though
Related
my question is simple but I can not find exact solution for this. I try to write a program with Java GUI, I use absolute layout for my program and I set frame bounds(100, 100, 450, 300) . When I run the program and make it full screen, components stays on their places. I want them to replace automatically according to screen size.
All supports are accepted, thank you!
The solution is quite simple. You state, "I use absolute layout for my program and I set frame bounds(100, 100, 450, 300)." Don't do this. Use the layout managers to help you place your components.
For instance, if you used a BorderLayout for the main JPanel, and added the JButton to a new GridLayout(0, 1, 0, 5) using JPanel (1 column, variable rows, 5 spaces between rows), adding this JPanel to main in the BorderLayout.LINE_END position, your problem is likely solved. As a general rule, you should avoid use of null layout and use of setBounds(...) for component placement as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.
I have made this TicTacToe app using the Java Swing library. Ever since I've added the menu, it wouldn't launch as expected. I mean, the functionality is fine but it would sometimes display as one of the three undesirable methods I have in the image when I launch it. However, once I maximize and minimize the frame, it would display in the desired manner.
Kindly help me fix this.
You are adding components to the frame once its already visible. Call frame.setVisible(true); only after you have added all components or you will have to revalidate the container. Once a container is visible and layed out, you have to call validate/revalidate if you add or remove components.
You are showing the frame before all the items are added
public void setUpFrame() {
...
frame.setVisible(true);
}
just move the frame.setVisible(true); to the end of the setUpFrame() method :)
i know that i already answer your question but i made this answer because i saw that if i press on the menu item of the exit, it "close" the window, it stays open in the background.
if you want to close the program completly use System.exit(0); instead of frame.setVisible(false); in the item_exit ActionListener
nice game and keep programming ;)
I'm working on an online mode for a new game and in order to prevent cheating I need fix window sizes (and both players need a window with the same sizes).
I used 'jframe.setResizable(false);' but it seems to be "glitchy".
When I click the window and move it away from the border of the screen, Windows does minimize it.
Here's a video about it:
http://www.youtube.com/watch?v=AQ7OHJOuLSk&feature=youtu.be
I've tried following code in order to fix it:
Dimension d = new Dimension(width, height);
panel.getJFrame().setMaximumSize(d);
panel.getJFrame().setMinimumSize(d);
panel.setMaximumSize(d);
panel.setMinimumSize(d);
and I created a Component Listener:
if (max_height!=-1){
if (e.getComponent().getSize().getHeight()>max_height){
e.getComponent().setSize((int) e.getComponent().getSize().getWidth(),max_height);
}
}
if (max_width!=-1){
if (e.getComponent().getSize().getHeight()>max_width){
e.getComponent().setSize(max_width,(int) e.getComponent().getSize().getHeight());
}
and I tried to work with Layouts but nothing worked.
What I need now is either the possibility to prevent that minimize "glitch" (If it is a glitch) or a way to make the JPanel not resizable. Like when the size of the JFrame window is changed, the JPanel always stays the same. It's neither streched nor minimized.
Help is much appreciated :)
Sincerely Felix
So far the best patch for this annoying issue is the following. Doesn't matter where you call the setResizable(false) method. Just add this piece of code after you setVisible(true).
private void sizeBugPatch() {
while (frame.getWidth() > yourWidth) {
frame.pack();
}
}
Where yourWidth is the width you've set in any of the possible ways, either manually or by overriding setPreferredSize methods. The explanation is quite easy, frame.pack() seems to reset frame.setResizable(boolean b) somehow. You could use an if instead of the while loop but I prefer while to exclude the case the window would still be extra-sized even after a second pack().
Did you initialize the variable jframe or are you calling the general Class?
Because if you do it like this:
JFrame frame = new JFrame();
frame.setResizable(false);
It works fine for me...
I am loading a JFrame showing the company logo and credits etc before starting the main application. I am having some problems. Fist of all, the size of my new JFrame can never be set. The JFrame looked fine when I previewed it under netBean but came out smaller every time. I tried to do it with a new constructor and setSize(), but still not working. Second, the JFrame has been loaded very slowly. No images and everything could be loaded and the JFrame stays blank for at least five seconds, really kind of annoying. Do it have anything to do with where I put the image files?
Thanks alot.
I am loading a JFrame showing the company logo and credits etc before starting the main application. I am having some problems. Fist of all, the size of my new JFrame can never be set. The JFrame looked fine when I previewed it under netBean but came out smaller every time. I tried to do it with a new constructor and setSize(), but still not working.
It is very difficult to suss out what is wrong without seeing your code, but having said that, your comment about this being a NetBeans-generated GUI suggests that the code will be very large and hard to read and interpret. It is for this and many other reasons that I am not a fan of using NetBeans to generate GUI's, especially for newbies who are just learning how to use Swing. I suggest that you write out your GUI code by hand with some user-friendly layout managers, nested by nesting JPanels if necessary. If you do it this way, you'll have some greater flexibility and control in the construction of your GUI, and you'll also have readable and debuggable code that you can post here for our assessment and help should it not work out right for you.
Second, the JFrame has been loaded very slowly. No images and everything could be loaded and the JFrame stays blank for at least five seconds, really kind of annoying. Do it have anything to do with where I put the image files? Thanks alot.
This sounds like a threading issue. I'd just load the images for the intro GUI first, then show the intro window, and then in a background thread, load any other resources that the program needs.
Having said all this, you probably want to look into using Java's own splash screen as this may do all that you're trying to cobble together on your own. The tutorials can help you with this (please click on link above or here).
Much unclear question description though I want to point some tips...
Do it have anything to do with where I put the image files?
If you mean your JFrame title image so you can use its setIconImage() method
Fist of all, the size of my new JFrame can never be set.
It is quite strange :( because you can always write code like a
JFrame aFrame=new JFrame();
aFrame.setSize(x,y);
... to control your frame scale
Second, the JFrame has been loaded very slowly. No images and
everything could be loaded and the JFrame stays blank for at least
five seconds
It may be caused by single thread overloaded but still to analyze the problem show the main class code or its exceptions stack trace
previewed it under netBean but came out smaller every time
what component do you use to paint your logo? And how do you paint it? Show the snippet
You can set the size by right click on the jFrame and
Set Default size
(netbeans design view)
I hope this is what you want
this may be useful for you?
JPanel aa = new JPanel(new GridBagLayout());
aa.setMaximumSize(new Dimension(500,500));
aa.setMinimumSize(new Dimension(490,490));
aa.setBorder(BorderFactory.createLineBorder(Color.black));
I opened the properties dialog for my JFrame and set "minimum size" field to [500,500] but that did not work.
What did work for me was selecting the "..." button at the right of the "minimum size" property. In the "JFrame Mininumsize" dialog as follows:
set dropdown (Set forms minimum size property using: ) = "Custom Code"
set code in (Form.setMinimumSize( ) ) = "new Dimensiom(500,500)"
i am learning Java at the moment and have the following question:
i am adding my controls to JFrame and then pack() before displaying.
this runs the application and all is very nice.
i was wondering is there a way to stop the user from resizing the application window?
also is there a way to for the image in JLabel to expand as the user changes the application window?
at the moment i have it as:
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
and it only centers the image, i would like to be able to expand/shrink the image.
thanks.
i was wondering is there a way to stop
the user from resizing the application
window?
If you don't want to allow users to resize your window at all then you can do
frame.setResizable (false);
I'm not sure that this technique can be successfully used at the same time with pack(). You should certainly try, but I remember there was an issue that if you do:
frame.setResizable (false);
pack();
then pack() simply doesn't do what it should and if you do:
pack();
frame.setResizable(false);
then you window becomes a little more narrow then after only pack(). Of course, some workaround can be found (or maybe even it's just my bad experience).
for the first question, you can stop the user from resizing your window using the method setResizable(boolean):
frame.setResizable(false);
I don't use GridBagLayout, so I can't help you with the second question.
To prevent resizing the window, you can simply call setResizable(false) on your Window or (J)Frame.
As to the JLabel and image, there is no easy way to make it scale the image according to it's actual size. If you use the label just for the image (no text), it's probably easier for you to implement your own component, in which you paint the image yourself.
also is there a way to for the image in JLabel to expand as the user changes the application window?
Yes you need to create your own LabelUI class and set it to your label via setUI().
In your custom LabelUI class, you will have to override getPreferredSize and paint the imageIcon as you wish.
It is slightly programmatic, you may want to look at source of BasicLabelUI to understand how things are done.( http://www.java2s.com/Open-Source/Java-Document/Apache-Harmony-Java-SE/javax-package/javax/swing/plaf/basic/BasicLabelUI.java.htm )
or
you could over-ride paintComponent() API of JLabel by creating your own extended JLabel class.