Reputation: 1656
I'm developing an application that has to draw a map.
I have a JPanel
grid that contains a 2D array of JPanel
cells.
I'm using this:
public class myCell extends JPanel {
...
@Override
public void paint(Graphics g){
super.paint(g);
if (imageForeground != null) {
g.drawImage(imageForeground, 0, 0, this);
}
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageBackground, 0, 0, this);
}
}
This draws my background image, and my foreground image on top of it.
I want to draw an image in a cell, that will be displayed on TOP of the OTHER cells, ie, draw an image bigger than the cell, FROM the cell, something like:
@Override
public void paint(Graphics g){
super.paint(g);
super.drawImage(imageForeground, 0, 0, this);
or
g.drawImage(imageForeground, 0, 0, this, overflowFromPanel == true);
}
or if it's possible, tell the image that it CAN be bigger than my Cell.
For more informations:
The background cells are all 1*1 in size. The foreground image must be drawn over ALL the background images it spread over.
For example, if I draw a 3*3 hours at the [1,1] square, it must be over the [1,1], [1,2], [1,3], [2,1], etc...
Upvotes: 0
Views: 184
Reputation: 324147
Custom painting is done by overriding the paintComponent() method, not paint().
I'm developing an application that has to draw a map.
So the background image is the map and covers the entire panel. The house would then be painted on top of the map.
So in the parent component you override paintComponent()
and paint the background image
Then in the CellPanel
, you override paintComponent()
and paint the foreground image. Although, why don't you just use a JLabel with an ImageIcon so you don't need to do custom painting?
if I draw a 3*3 hours at the [1,1] square,
Then you need to add your house image to 9 cells .
The other option is to forget about defining your map into cells. Instead you create a custom object with two properties:
Then you add this custom Object to an ArrayList. Then in the paintCompont() method of the main panel you iterate through this ArrayList and paint each image at its specified location.
@Overide
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(...); // paint the background
for (each item in the ArrayList)
{
Point p = item.getPoint();
Image image = item.getImage();
g.drawImage(...);
}
}
Edit:
On a side note: the background is a different image for every cell.
Why are you using cells? It sounds like you have a fixed size grid. So you can just paint the individual cell images to a larger BufferedImage that makes a single background image for all the cells. Then you can just paint houses over top of the background.
Another option is to use the OverlayLayout
. It allows you to stack two panels on top of one another.
So the logic would be something like:
JPanel panel = new JPanel();
panel.setLayout( new OverlayLayout(panel) );
panel.add( foregroundPanel );
panel.add( backgroundPanel );
You would need to:
paintComponent(...)
method to do the custom painting.Or you could use a JLayeredPane
.
Upvotes: 3