Reputation: 49
I am trying to create a simple platformer game and I am on the part where I'm trying to put the character onto the JPanel. And I came into a problem. I can't add the character to the JPanel (Note that the character is in the form of a JLabel containing an imageicon) without moving the, already placed, tiles (the grass, the sky, ect.).
The code I use to place the blocks is:
static void drawScreen() throws IOException {
panel.removeAll();
int tile = 0;
int line = 0;
for (int i = 0; i < t.length; i++, tile++) {
boolean tD = tile % 32 == 0;
if (tD) {
tile = 0;
line++;
}
if (t[i] == 0) {
File f = new File(sPath);
BufferedImage s = ImageIO.read(f);
JLabel l = new JLabel(new ImageIcon(s));
c.gridx = tile;
c.gridy = line;
c.insets = new Insets(0, 0, 0, 0);
panel.add(l, c);
}
if (t[i] == 1) {
File f = new File(gPath);
BufferedImage g = ImageIO.read(f);
JLabel l = new JLabel(new ImageIcon(g));
c.gridx = tile;
c.gridy = line;
c.insets = new Insets(0, 0, 0, 0);
panel.add(l, c);
}
if (t[i] == 2) {
File f = new File(dPath);
BufferedImage d = ImageIO.read(f);
JLabel l = new JLabel(new ImageIcon(d));
c.gridx = tile;
c.gridy = line;
c.insets = new Insets(0, 0, 0, 0);
panel.add(l, c);
}
}
frame.revalidate();
frame.repaint();
}
The array t is containing all the tile ids. It contains 672 integers mostly of 0.
Can anyone let me know how I should add the character at a specific coordinate without moving the other tiles.
How I am currently adding it is with:
static void addChar() throws IOException {
File f = new File(cPath);
BufferedImage c1 = ImageIO.read(f);
BufferedImage c = runResize(c1, 50, 76);
JLabel l = new JLabel(new ImageIcon(c));
l.setOpaque(false);
panel.add(l);
frame.revalidate();
frame.repaint();
}
And when I run it, it outputs with this: (Excuse my bad art)
Image Of The Output:
If you have any questions please let me know.
Upvotes: 0
Views: 38
Reputation: 324128
I can't add the character to the JPanel (Note that the character is in the form of a JLabel containing an imageicon) without moving the, already placed, tiles (the grass, the sky, ect.).
If you are going to use real components to represent the grass, sky on a panel, then you can't add any more components to that panel.
Instead you can try using a JLayeredPane
. This will allow you to paint multiple components on top of one another. So one layer would contain your panel with the grass and sky. The next layer would contain the "character" you want to add. Read the section from the Swing tutorial on How to Use LayeredPanes for more information and examples.
the other option is to customize the paintComponent()
method of your panel to "paint" the grass and sky using your images. Then you can add your "character" component to the panel since it will now be the only component on the panel. This is probably the better approach.
Upvotes: 1
Reputation: 2222
I think you need to create a new instance of it with needed coordinates.
At first, you create a new JLabel
instance:
JLabel new_l = new JLabel(new ImageIcon(c));
new_l.setOpaque(false);
...
// Now you need to calculate the coordinates here and check them
// When your coordinates were covered by `panel`
// you can put it down on the panel:
panel.add(new_l);
In another case, you need to choose layer for your new character from other components.
Upvotes: 0
Reputation: 13375
In case of placing elements in place where you want to have them (exactly) think about using Absoulute Layout:
https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Upvotes: 0