Pack Method In JLayeredPane - java

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.

Related

How to get the color beneath an opaque JPanel?

Is there is a way to get the color underneath an opaque JPanel?
I have two JPanels, a red one and a blue one (they are animated). I want to get the color of a surface according to the JPanel that passes over it.
In following images, the small borderd square is an opaque JPanel, and I want to retrieve the color of the JPanel beneath.
Not really sure what you are asking. But assuming you know the x/y location of the rectangle you could use:
The getComponentAt(...) method of the Container class. Once you get the parent panel you use the getBackground()` method to get the Color
Or, you can use the Robot class and the getPixelColor(...)` method.

Advise on Java GUI Component Layout strategy

I am hoping someone can offer a strategy for the following Java GUI:
I am implementing a Scrabble-like algorithm and I would like to write a GUI where a user can compete with the algorithm. My experience with GUIs is limited and I am trying to avoid a big learning curve if someone can suggest a useful subset of components upon which I can focus.
I would like to display two playing areas side by side. Each playing area contains a board and a tile holder. Tiles from the holder can be dragged and dropped onto the board.
Obviously, the basic component is the tile, which is just a square displaying a letter. The tile holder and the board are both tile containers (grids of different sizes). The playing areas each hold the two tile containers and allow dragging and dropping from one container to the other.
The layout of the two playing areas is: the board on top, the tile holder on the bottom, and some neutral space between them.
The layout for the window is just to display the two playing areas side by side.
Perhaps naively, it seems to me I don't need much. But finding the right pieces is proving to be quite a task.
Any advice is appreciated.
Top level JPanel with a GridLayout, 1 row, 2 columns, will create for you a panel with two evenly split areas. After that you can add another JPanel for each side (just sequentially add them), and these two JPanels can have a BorderLayout. For these two BorderLayout JPanels, you can add to each the following:
A JPanel with a FlowLayout of ImageIcons that hold a pictures of a scrabble tile, and this JPanel will be added to the south position on the BorderLayout JPanel.
Another JPanel with a custom class you'll make called ScrabbleBoard extends JComponent. Here you can extend paintComponent and draw the board how you wish, probably making use of obtaining the width and height so you can draw a board that scales to how much space you have available for the component. This custom JComponent will be added to the center position of the BorderLayout JPanel.
A component added to the center position of a BorderLayout tries to be greedy and takes up as much room as possible, while components on the sides do not, so it's often a good choice for UI's that require a small navigation or menu control area, and a larger area to view what is considered the main graphics.

Make Graphics from JPanel visible outside JPanel

I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
This my paint method:
public void paintComponent (Graphics g) {
g.setColor(Color.red);
g.fillRect(dist, 0, 10, 10);
dist++;
}
Update:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
Is there a better solution for this problem?
You state:
I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
If the JPanel is drawing it, the short answer is: "you can't".
The longer answer will depend on just where you're trying to draw the JPanel and how the rest of your GUI is set up.
Edit
You now state:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
If I coded the way you were doing it, I wouldn't have these local JPanels draw the car but rather would have the car be its own sprite that exists on a different layer from the streets, perhaps using a JLayeredPane. It could exist in its own JPanel that encompasses your entire map, as long as this JPanel is not opaque. Then you could move the car any which way you'd like.
As said before you can't but if you want it to occupy and larger area you should either make the JPanel bigger or put the paintComponent in the parent component.

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.

Adding JPanel to Canvas

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

Categories