Component only fully shows when I maximize frame - java

I'm programming the game hangman and I have a Label in the center for the word to be guessed, a score panel on the right(east) and a panel of button characters on the bottom(south). As i was moving onto painting the hangman gallows, i tried to paint a simple circle at first and add it to the left(west) of the frame, but only a small portion of the circle appeared, the problem seemed to be fixed temporarily when i maximized the frame and the resized it to its original size, how do i fix this? I created a class that extends JPanel and overrided its pain component method. what should i do so that it fully shows the circle when the program is launched.

I created a class that extends JPanel and overrided its pain component method. what should i do so that it fully shows the circle when the program is launched.
You also need to override the getPreferredSize() method to return the size of the panel so that the layout managers can use this information.
overrided its pain component method
You should be overriding the paintComponent() method, not the paint() method.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Related

Swing - Awt. How do I render a new frame without the elements from the previous frame?

I'm making a little game using the Swing and the Awt libs.
When I render a frame the Images I added through the paint() method from the previous frame stay there, my character, the green square, is the only mooving element.
How do I "clean" my JPanel before updating the game status and draw a new frame?
When I render a frame the Images I added through the paint() method
Custom painting is done by overriding the paintComponent() method of a JPanel and you add the panel to the JFrame. And you make sure you invoke super.paintComponent(...) as the first statement of the method.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.

Java Swing component not repainting

I have an LWJGL OpenGL Display showing up inside an AWT Canvas, which in turn is inside a Swing JPanel that is used as content pane for a Swing JFrame. At some point in the program, I want to switch the AWT Canvas containing the Display for a JComponent, so that instead of having something like that:
JFrame > JPanel > Canvas > Display
I have something like so:
JFrame > JPanel > JComponent
However, even though I remove the Canvas from the JPanel and add the JComponent, then revalidate the JPanel and repaint it, the Display still shows until I CTRL-ALT-SUPPR to task manager (my JFrame is set Undecorated and ExtendedState is JFrame.EXTENDED_BOTH, so it is full screen). At which point, the JComponent shows up like nothing ever happened..
I'll share the part of my code that does the transition so you can maybe help me point out what I have done wrong:
public static void switchTo(Container container){
pan.removeAll();
container.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
pan.add(container);
frame.getContentPane().validate();
pan.revalidate();
pan.repaint();
}
where pan is my JPanel and frame is my JFrame.
I have also tried directly setting my JComponent as my JFrame's content pane, but that gives the exact same result.
The only way I managed to make it function correctly was by calling destroy() on the Display beforehand; however, I need to keep the OpenGL context running so that I don't have to re-initialize the Display and reload every texture when switching back to the Display, which would be quite a long process given the number of textures I have.
Thank you very much for any answer, I hope I made myself clear!

JPanel derived class in JFrame - painting duplicate

I've added my JPanel derived class to the JFrame. Now when I want to draw an oval (using fillOval in my JPanel's paintComponent method) I see two ovals being painted. The problem disappears when I invoke super.paintComponent or when I invoke setContentPane in the JFrame class with my JPanel as parameter. The question is, why does it happen? Of course when the upper left corner of both JPanel and JFrame are in the same place, it doesn't happen. But somehow the JPanel isn't opaque unless I invoke super.paintComponent. The main question is why does it paint BOTH on the default content pane AND my JPanel. Thanks for help.
" The main question is why does it paint BOTH on the default content pane AND my JPanel."
It just appears that way, but actually you are seeing both being drawn on the panel. Always call super.paintComponent in the paintComponent method (no if ands or buts) or you will see paint artifacts as the one you're experiencing. The paintComponent method can be called for any number of reasons, and each time it's called, is another opportunity for paint artifacts to appear. Calling super.paintComponent wipes those out. When you set the content pane to the panel, it appears the paintComponent method is not being called more than once, so you don't get those artifacts. But to reiterate, always call super.paintComponent. Setting the content pane has nothing to do with the problem or the solution.

Some JPanel's backgrounds fade when I interact with page

As i wrote in the title, I've this problem with JPanel.
My page is structured as a mainJPanel, another JPanel inside the main (where I paint a background) and some component added to this background panel.
Everytime I interact with something (for example i click a button) the background of the components fade to a lighter version of themselves.
I really can't figure out why.
mainPanel have setOpaque(false) and setLayout(null);
backgroundPanel have setOpaque(false) and setLayout(null) (i painted the background when i create the class backgroundPanel that extends JPanel, with an override of the method paintComponent);
all the components that becomes lighter have setOpaque(true) and setLayout(null);
the background of this components are a custom grey (rgb(232,232,232)).
Thank you all
Using setOpaque(false) should not cause a problem (ie. JLabels are non opaque). It sounds like a painting problem when using transparent colors.
Check out Background With Transparency for an explanation of the problem and a couple of solutions.

Java - control Z order of JPanels

In short, my need is to have a background Image in my java app, and upon some event, create some other graphics on top of that image.
I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program, and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything.
Is there any way to control the Z index/order of the JPanels, or am I going about this completely wrong?
You can use the setComponentZOrder() to handle Z-Order in your application.
Resources :
JavaDoc - Container.setComponentZOrder
oracle.com - Mixing heavy and light components
Sounds strange to add mutiple JPanels and use z-order. I would suggest you either simple add ONE JPanel with the paintComponent(Graphics g) method overwritten, where you draw the correct image according to your events.
Or use the CardLayout, and add JLabels (with different images), then when your event triggers, use
CardLayout cl = (CardLayout)getLayout();
cl.show(this, "card3");
to show the correct JLabel.
The JLayeredPane is designed for just this purpose. It allows you to control the Z-order of components.
I was thinking I would use a JPanel to
draw the background image in, add it
at to my JFrame the start of program,
Sounds reasonable.
and then add other JPanels on top of
that upon certain events. The problem
is Swing gives the JPanels added first
the highest Z index, so what should be
my background is showing up on top of
everything.
Adding a panel to a panel is just like adding another component to the panel. The child component will be painted on top of the parent panel. There is no need to play around with Z-Order.
The key is to set a layout manager on the panel with the image. Then each panel you add to the image panel must be made non-opaque so that the background image will be painted.
Maybe the Background Panel can help you out. It automatically makes any component added directly to the panel non-opaque.

Categories