JMenuBar makes JFrame "jump" - java

I am creating a Java application of a sea map, where I use a JFrame as Canvas to draw the sea itself. I have a GUI on top of this as a JPanel. I recently added a JMenuBar to the JPanel with different functions. However, I noticed now that the the newly added MenuBar 'pushes the application' downwards ~10-15 pixels — and when I zoom/pan the map, the map makes some weird 'jumps' – likely trying to readjust.
Does anyone know why this happens?

Nine out of ten, you need to be looking at insets.
It's been a while since I used Swing directly, but all containers have a method called getInsets(). This returns an Insets object, describing the size of the border of the container--including your JFrame. JMenuBars tend to nudge those insets a bit, leaving you two two main options.
What I recommend is doing your drawing to a JPanel, placed in the JFrame; so that you don't have to worry about the JMenuBar. Use the panel as a canvas instead.
The other option is to poll for the insets at the time of drawing, and alter your coordinates accordingly; but I have to recommend against this if you can still avoid it, as it runs against just about all of my modular programming instincts.

Related

Java button within graphics panel

I'm relatively new to developing GUI's within java so this may well be a stupid question or quite simply not possible to achieve but here we go.
I've created 1 single JPanel with no border layout set up or anything like that and I intended to paint a GUI on top of it using the graphics class. The JPanel is just plain black and then I've drawn a huge box over it leaving the black just as a border, and painted the whole GUI within this white box.
I want to add buttons within that white box GUI as well but I've no idea how. In fact they don't even have to be traditional buttons JButtons, if I could just draw a shape and have that act as a button then add an event handler to just that shape that would work also but I don't know how I'd do that either.
I have so much code for my whole program (it's a school coursework project) that I'm not sure which parts would even be worth sharing to assist with this question since there's so many GUI aspects I've already drawn so I've tried to just explain my issue in words.
Honestly I have no clue what I'm doing so any help would be appreciated.
EDIT: Here's a screenshot of my current GUI with a 'sketch' of how and where I'd like to be able to add buttons.
GUI Image
As with any suitably complex UI, you need to start by breaking it down into manageable chunks, focusing on areas of mutual interaction and functionality.
For example...
Says to me that you have two primary UI elements, the left and the right.
This could easily be established with a GridLayout, but, if the two sides are not equal in width, a GridBagLayout might be more appropriate
The right side to me says simply, JTable. You could place this within a container using a BorderLayout, allowing the table to occupy the CENTER position.
The key information would then a component laid out with either a GridLayout (top and bottom) or a GridBagLayout if the requirements are more complex. This component would then be added to the SOUTH position of the BorderLayout.
Again, this is pretty simple. The primary layout would probably be a BoderLayout, with the title in the NORTH position, the graph in the CENTER and the buttons wrapped in a component in the SOUTH.
You could use either a FlowLayout or GridBagLayout to layout the buttons depending on your how you want them to appear
Recommendations
Have a look at:
Laying Out Components Within a Container
How to Use Tables
And for the "border", I'd recommend you have a look at LineBorder. Take a look at How to use Borders more details

Java - Swing - displaying multiple JComponents in one JFrame

in a game, I would like to display simultaneously two JComponent instances in a JFrame. One would be a background board, the another - a player character.
So, one component (a background) would be behind another (a character). The character would be drawn of several rectangles and thus it will most commonly have some wholly transparent area.
How to do that? I know that normally, when I add two components to a frame (method add(Component)), only the last-added component is visible. This is done by following code:
frame.add(backg); // backg is an instance of a certain class that derives from JComponent
// (...)
frame.add(psc); // psc is an instance of an another class that derives from JComponent
frame.pack();
frame.setVisible(true);
How should I change the code above?
First of all, if you are looking to write a game in Java, try out Slick2D provides numerous tools and far better graphical capabilties (being wrapped around LWJGL which wraps around OpenGL). Secondly, if you do decide to go the Swing route, here's a simple solution:
//Player + background components defined here
playerComp.setOpaque(false);
JLayeredPane layer = new JLayeredPane();
layer.add(backgroundComp,1,0);
layer.add(playerComp,2,0);
I believe that both these solutions were mentioned above in the comments. Setting the player component opaquity to false allows those transparent areas to show components behind the player, or the background image. If you're familiar with a z-index in CSS/HTML, a JLayeredPane basically adds a z-index to Swing by allowing you to set the order in which components are rendered. So, set the player to opaque, and then render it in front of the background component.

JPanel vs JFrame when animating

I recently did a program which moves an image along the window and forces it to change its direction when it hits the borders. The animation happened within a JPanel class which was then of course added to a JFrame class which contains the main() function. Now my question is why must it be done in this manner. Can't I simply have just the JFrame and use that both as my window and my animation class which moves the image? Is the JFrame incapable of representing animation on it's own.
Regards.
Omar's answer is correct but I thought I might elaborate just a bit:
Though JFrame's are capable of hosting your animation, it is not a typical use of the JFrame. JFrame's are typically the outermost visual container and host one or more other visual containers (such as JPanel). Though there are exceptions, I would consider the JPanel the most flexible and common way to contain your animation (or other GUI 'controls') to leave you flexibility for adding visual features later.
But as Omar points out, either JFrame or JPanel can be used as they are both considered "containers" in Swing. Tying back to my last comment, using a JPanel is a bit more flexible. For instance, you might later want to incorporate your animation into an Applet/JApplet which you might find confusing and/or difficult if you used a JFrame.
I tried to just comment on Omar's answer but I am a new user and it won't let me add comments yet. Therefore, upvotes are appreciated :)
It's just better to use JPanel as you may want multiple Panels in one frame each with different animations.
Yes, you could do it with JFrame as well.

Make JFrame Title Bar not count for pixels

I am making a game in Java as a way to teach me more about Java and Game Programming in general. But, I have one question. The Title Bar on my JFrame takes up 29 Y-pixels of the space. Therefore, I have less drawing room then I had planned. Is there a way to make it so the corner of the JFrame Canvas is the point (0,0) and not (0, 29)? Thanks
The answer is simple: don't draw in the JFrame's paint(...) method but rather in a JPanel's paintComponent(...) method, and then make the JPanel the JFrame's contentPane. This solution is well described in the Swing Tutorials painting section, and there are several reasons to do this including:
You don't risk drawing over things that shouldn't be drawn over including borders and title bars.
You get the advantage of Swing's default double buffering which can make a huge improvement if you do any animation.

java JLayeredPane how to add JPanel in front of another panel generated by Window builder

I am writing in java some kind of application and i have to place something in front of another objects( in Z order)
I know that that i should use JLayeredPane but in fact i am not very familiar with it.
My idea is to make two JPanel's with different Z-order factors while inserting them to JLayeredPane.
i pasted my code http://www.wklejto.pl/130038
i would be very grateful if you tell me what is wrong because i am doing it for a long of time with no effect.
I don't see anything wrong with this code. Maybe you're trying to paint a transparent (not opaque) JPanel (e.g. with message) on top of the underlying base JPanel.
In that case you should invoke setOpaque(false) on your front JPanel.
JPanel second = new JPanel();
second.setOpaque(false);
second.add(new JLabel("message"));
jlp.add(second, new Integer(300));
JPanels are opaque by default - on the other hand JLabels aren't.
And take a look into tutorial.

Categories

Resources