Adding JPanel to Canvas - java

Since JPanel and Canvas are both same-level components, the solution would probably be some sort of a 'hack'. This question says that you won't be able to add the lightweight component to the heavyweight canvas (I want JPanel transparent).
If this isn't posible, then would putting a transparent Component work? Also, is it feasible to add Swing components to the Component (it just has to work, even if it's bad). And how would I go about actually putting it over the canvas (since they are both same-level components)?
Note: I would never do something like this in a real app, I just need it in this case

One possiblity is to add the JPanel to whatever container the Canvas is on, then setting the color of the JPanel to have an alpha of 0. This should add it over the old one, without blocking out the Canvas. Is this what you want?
EDIT: Thinking about it, the JPanel's default color is transparent... You should just be able to add the JPanel the Canvas's parent, and lay it over it

Related

You don't need a Canvas or JPanel to draw?

I have been under the impression that a Canvas or a JPanel were necessary to put graphics on a JFrame, however I saw a video earlier in which the person was using paint(graphics g)while extending JFrame and hadn't made a panel or a canvas. If this is a case, why do people bother with making a Canvas or a JPanel?
JFrame extends from Frame, which extends from Window, which extends from Container, extends from Component which defines paint.
If this is a case, why do people bother with making a Canvas or a JPanel?
To answer that question, you need to have a better understanding of JFrame (and window based classes).
Complicated
JFrame is actually a composite component, that is, it's made of a number of other components which provide the core functionality of the window
What this means is, if you override paint and perform custom painting on the frame, it's very possible that you will paint over the child components or the child components will paint over it, and because of the way the paint subsystem works, will do so any time without the frame's paint method been called.
Decorations
Frame's include the window decorations within their available area. This means that the "viewable" area is actually smaller then the defined area of the frame.
This is also why it's recommend to use pack instead of setSize
This means that if you override paint you could actually paint under the window decorations (yes, this happens all the time, and we're tired of answering it)
Screen shot from How can I set in the midst?
The contentPane of JFrame takes care of this, as it's laid out within the viewable area.
Double buffering
Top level containers, like JFrame aren't double buffered, so, even if you overcome all of the above, you will get updates which flicker. Sure you could "devise" you're own double buffering algorithm, but in Swing (ie JPanel), it's free, so why bother
Locked in
As a general recommendation, we generally discourage extending from JFrame (or other top level containers) as it locks you into a single use case and prevents the possibility of re-use.
On the other hand, if you use a JPanel, you can add it to what ever container you want, when ever you want

Pack Method In JLayeredPane

I am creating a snake game and I am in need of using a JLayeredPane which will hold 2 Jpanels, this JLayeredPane will then be inserted into a JFrame. The reason I am doing this is because I need to have the base layer -- the game, and on top a layer which will spawn fruit onto the board. The reason I am not doing this directly onto the board is because the board is constantly being repainted, I need a method to randomly change the colour of the fruit for every time the player collects the fruit. The fruit however, will continuously change colour as the board is being repainted and a new random colour is generated based upon an array.
The problem with simply using a JLayeredPane is that I need the frame to fit perfectly with the components inside of it, setPreferredSize does not seem to do this as it does not take insets into account. When setResize(false), the insets do not match with the actual inset values, so I cannot simply add the insets.
So my question, how do I get the JLayeredPane to fit the JPanel's where the components within these JPanels have dimensions and then put this JLayeredPane into the JFrame.
As shown in How to Use Layered Panes: Setting a Component's Position Within Its Depth, use setBounds() to position components within a JLayeredPane.
Alternatively, override paintComponent() in a JCOmponent or JPanel and render the fruit layer beneath the snake layer. If no resampling is required, drawImage() is quite fast in this context.

How to solve graphical glitching with JDialog, JPanel?

So the intial setup for this issue is that there's a JDialog, and inside that I've placed a JPanel that would house the rest of the components (since painting the JDialog itself is apparently a bad idea). This JPanel has an overriden paintComponent(Graphics g) method that only paints the background and adds a faint border for aesthetic purposes.
Now inside that is a series of JPanels that categorize the contained form components, and each JPanel has an overridden paintComponent(Graphics g) as well, painting a semi-transparent background.
Inside each of those JPanels is where I start to have some issues, presumably with transparency. I have JTextFields, JCheckBoxes, JLabels, JSliders, etc inside these panels, and when you interact with one (hover, click, etc), the background goes from transparent to opaque, with an occassional ghosted image from another field (which appears slightly random sometimes). I'm using a custom LAF called Web, but I tested with other built-in LAFs and the same thing happens.
Is this a glitch with Java or did I mess something up? If so, how can I patch this up? I can paste code fragments later if necessary, but I've used several custom classes and nine-patch style image stitching which may make the code fragments hard to follow. Thanks in advance!
If you are painting components with a transparent background it is very important that the component is marked as transparent (setOpaue(false)) so that the repaint manager knows that it must paint the components below it.
It is also very important that when you are performing custom painting that you call super.paintComponent first.
This is especially important in the case of transparent components, as this prepares the Graphics context for painting.
Graphics is a shared resource. All the components painted in your window will share the same Graphics object, meaning that if you don't allow paintComponent to first prepare it, then you will see what was previously painted on it.

Java Layer JPanels

I need to layer JPanels but I don't know how. I have 3 panels, the first is for the background, the second is for a Character/Sprite to move around and layers the first panel(background, and the third is a bar off to the side (Used for buttons and has nothing to do with they layers). How do I layer panel 1 and panel 2?
edit: The background is made up of a grid of 25x25 labels with an icon in each.
Some options:
Use a JLayeredPane which can layer components using a z-order Integer constant. Remember that when doing this, you are also essentially using a null layout, and so you will be fully responsible for setting the size and position of all components added to the JLayeredPane.
If all the background is doing is painting an image, you could use a single JPanel, and then simply paint the image as a BufferedImage that is displayed in the JPanel's paintComponent method. The sprite would also be painted but its location would vary.
See How to Use Layered Panes.
Don't forget to use:
panel.setOpaque(false);
Or you don't need to layer panels. You can just paint a background image on the panel. See Background Panel for an example of this.
We just recently worked on a top-down video game for my CSC class. All we did was draw the background and then all the sprites after it in the paint() method on the JPanel. We also used a Timer and an ActionListener to constantly update the JPanel.

permanent background image on a JPanel

I am building a swing interface for drawing overlays over a particular image. I have found several examples of how to draw an image on a JPanel, and I am able to do this without difficulty. However, I would like to keep this image completely static while dynamically drawing overlays on top of it. With that in mind, it would be better if I could set the image as a permanent background of the panel, rather than having paintComponent() redraw the image every time. This would be particularly useful, as there will frequently be situations where I want to removeAll() graphics currently on the panel and redraw new ones.
So my question is this: is there a way to set a permanent background image in JPanel, or do I have to redraw the image every time paintComponent() is called?
put Image as Icon / ImageIcon to the JLabel
use this JLabel instread of JPanel
JLabel hasn't implemented any LayoutManager in compare with JFrame(BorderLayout) or JPanel(FlowLayout)
you have to define the proper LayoutManager, that accepting PreferredSize came its child(s)
then there are accesible (almost nearly) all methods as for JPanel, as container works
maybe you can try putting the Jpanel inside another Jpanel. You draw the image on the container JPanel. then make your inner JPanel a transparent one. this way, when there are no objects on it, you see the background of the parent JPanel which did not change.

Categories