1

My problem is easy to explain: i want a/some JPanels, added to a JFrame, to paint themself with an image. sadly the last thing does not work. for info: the image path is correct and the JPanel size is the same as the image size. thx for help :P

package frames;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import world.Terrain;

public class PanelTerrain extends JPanel {
private Image img;
private int x;
private int y;
private Image imga;

    public PanelTerrain(Terrain terra, int x, int y) {
        imga = new ImageIcon(terra.getPath()).getImage();
        this.x = x;
        this.y = y;
        this.setBounds(x, y, 8, 8);
        //this.setBackground(terra.getColor());
    }

    public void changeTerrain(Terrain t)
    {
        this.setVisible(false);
        this.setBackground(t.getColor());
        this.setVisible(true);
    }

  @Override
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
        g.drawImage(imga, x, y, this);
  }                 
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    can you see the jpanel in the jframe ? what is the x,y values ?if your panel and image size is same then x,y should be 0 – Madhawa Priyashantha Sep 04 '15 at 16:25
  • 1
    Exactly as @FastSnail states: how are you using this class? How are you testing that the image is in fact being read in correctly? Have you tried to display the image as an ImageIcon displayed in a JOptionPane? If so, does it work? Consider putting a temporary border around the JPanel to visibly see its actual size. Please post an [mcve]. – Hovercraft Full Of Eels Sep 04 '15 at 16:26
  • like still presented in the code, i tested the panel using colors, so the panel works. :P i use them like: yArray[i] = new PanelTerrain(new Grass(), 150 + (i * 8) , 50 + 8 * ycord); the x / y re for the place the panel shoud appear. so, tested with colors, everything works like it should. tested the image (path) via a simple system.out.print(). but thx for the tips, will try a JOptionPane() – Simon Maas Sep 04 '15 at 16:28

2 Answers2

5

My first guess is that you're passing the wrong x and y.The x and y in g.drawImage are the coordinates of the top left corner, not the size of the image, so usually they are set directly at 0 (that means, g.draWimage(imga, x, y, this).

vivianig
  • 173
  • 11
  • thx alot, i dont now how i missed that xD i tryed to solve this problem for 2 days now wile i got similar stuff in adding layers to panels whcih works fine. but i really forgot (dont ask me why) that the coords of the image re based on the panel itself, not on the frame xD so i got a panel of the size 8 by 8, but an image starting in the lower right corner (unseeable) thx alot xD (so with this.setBounds(x, y, 8, 8); and g.drawImage(imga, 0, 0, this); it works how it should work) – Simon Maas Sep 04 '15 at 16:40
  • 2
    @SimonMaas Please consider [accepting](http://meta.stackexchange.com/a/5235) the answer that helped you the most. – Lukas Rotter Sep 04 '15 at 16:50
2

One very big problem is your possible misuse of bounds and x and y. You're calling setBounds on the JPanel (something to avoid) but then drawing the image at some x and y that in all likelihood is way beyond the bounded size of your JPanel. Don't do this. for more complete help, please create and post your Minimal, Complete, and Verifiable example.

Here's what I think that you should do instead -- assuming that you want a grid of images, some of say earth, some water, some grass, ...

  • Create ImageIcons for the base images, one Icon for grass, one for water, etc...
  • Create a JPanel that uses a GridLayout, and fill it with a grid of JLabels.
  • Place those same JLabels in a 2-dimensional array of JLabel.
  • Swap the label icons where and when you need to change the image by calling setIcon(newIcon) on the JLabel.

For example, please see this answer of mine to a similar question as well as the other answers to the same question.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373