I’m literally been looking around for this and reading tutorials for hours, nearly all day and I haven’t been able to find exactly what I’m looking for. I’m trying to make my game world by using a LayeredPane with different layers representing the different graphical elements (the background, characters, etc). I initially used JLabels as components and had their icons perform this task but realized with BufferedImages and Graphics2D I could manipulate the images and so I’m trying to make the conversion.
Currently I’m running into trouble and don’t really know where to begin. I’ve decided to draw directly onto JPanels and add those as Components to the LayeredPane but I’m running into some trouble. Specifically, I don’t know all the methods needed to accomplish this. So far, all I have in my my constructor (pertaining to the BufferedImages) is:
JPanel background, character;
Graphics2D bgGraph, charGraph;
BufferedImage bg, chara; // Already contain the correct images
public mainConstructor() {
background=new JPanel();
character=new JPanel();
// In order to draw the images on their respective JPanels
bgGraph = (Graphics2D)background.getGraphics();
charGraph = (Graphics2D)character.getGraphics();
bgGraph.drawImage(bg,0,0,null);
charGraph.drawImage(chara, xpos,ypos,null);
// Other code, specifically code adding JPanels to LayeredPane
}
However this doesn’t seem to work. I’ve looked around and there’s code of people overwriting JPanels paintComponent() method and some other draw methods. I don’t really know which are relevant and which other methods would be needed to allow me to repaint/draw the specific images that need updating