How to overlap two (or more) JPanels in a JFrame - java

i'm trying to overlap two different JPanels on a single JFrame. How can i do that? I have a GamePanel where i draw my game, and a BorderPanel, that draws a border for my game. BorderPanel is to expose the enemies and other objects of the game just after crossing the border. The border then must always be on the surface. Is it possible?

Related

Do I add MouseListeners to the Canvas or to JFrame

I was trying to add a mouseListener and a mouseMotionListener to my game and noticed that I can add them to the Canvas or to the JFrame. Do I add it to both or one of them?
What I recommend is using the Canvas. When you are using a listener of any kind, think of where the actions will occur. Do all of your updates happen on the frame or on the canvas? If it is the latter, use the canvas to handle all of your action listener objects.
Another way to think of it is that the JFrame is just a window holding the implementation of your game. Your graphic updates, keyboard inputs, mouse inputs, and any other functionality is done through the canvas.
For example, compare the JFrame and canvas to this image of Skyrim. The window on the outside (A JFrame Object) has a close/minimize feature and the window holds the game screen (A Canvas Object).
Attach it to the Canvas
You should add the mouse listener to the canvas, there is one reason for it:
Coordinates.
If you attach mouse listener to the frame the 0-point of coordinates will be on the left-up corner of JFrame border. It will be hard to calculate the coordinates relative to canvas.
Instead of you can attach mouse listener to Canvas. The coordinates will be better with this. But do not forget to gain focus on canvas after adding listener:
canvas.addMouseMotionListener(motionListener);
canvas.requestFocus();

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.

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.

Categories