Reputation: 79
I am really having problems understanding how overriding the paintComponent(Graphics g)
method works.
I have the following class:
public class DisplayDocs2 extends JPanel {
ArrayList <BufferedImage> docList;
BufferedImage imageCount;
public DisplayDocs2(ArrayList docList){
this.docList = docList;
}
public JPanel DisplayImage(){
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
for (int i = 0; i < docList.size(); i++){
imageCount = docList.get(i);
}
return this;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageCount, 0, 0, this);
}
}
Whatever I try, if I have more than one image then the first image is always covered by the last one.
I want to use BufferedImage
s because I want to be able to manipulate them afterwards.
Upvotes: 0
Views: 153
Reputation: 51565
I am really having problems understanding how overriding the paintComponent(Graphics g) method works.
It's pretty simple. Every time the system or you call for a repaint of the JPanel, the code in the paintComponent method is executed.
In the paintComponent method, you paint. Period. Full stop. You do nothing else in the paintComponent method but paint (draw).
The first thing you do is create a Java object that holds a BufferedImage and a java.awt.Point to hold the x, y position of the BufferedImage.
package com.ggl.testing;
import java.awt.Point;
import java.awt.image.BufferedImage;
public class ImageLocation {
private final BufferedImage image;
private Point imageLocation;
public ImageLocation(BufferedImage image, Point imageLocation) {
this.image = image;
this.imageLocation = imageLocation;
}
public Point getImageLocation() {
return imageLocation;
}
public void setImageLocation(Point imageLocation) {
this.imageLocation = imageLocation;
}
public BufferedImage getImage() {
return image;
}
}
Next, you pass a List of ImageLocation instances to your drawing panel.
Finally, your paintComponent method would look something like this:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (ImageLocation imageLocation : imageLocations) {
Point p = imageLocation.getImageLocation();
g.drawImage(imageLocation.getImage(), p.x, p.y, this);
}
}
You create the List of ImageLocation instances somewhere else in your code. Preferably in a GUI model class.
Upvotes: 3
Reputation: 126
The behaviour you experiencing is the result of your code. The part:
for (int i = 0; i < docList.size(); i++) {
imageCount = docList.get(i);
}
Sets the imageCount
reference to the last element of the list, which the paintComponent()
method draws.
So your first image is not covered, it is not painted at all.
If you want to draw all the images,
for
cycle above and the imageCount
attribute, they are unnecessary.Put a code like this into your paintComponent()
:
super.paintComponent(g);
int x = 0;
for (BufferedImage img : docList) {
g.drawImage(img, x, 0, this);
x += img.getWidth();
}
This will paint all your images next to each other.
(Note that the code will throw NullPointerException if the method DisplayDocs2
is never called with a non-null parameter.
Upvotes: 2
Reputation: 14238
You can actually use jlabel instead of paintComponent.
for (int i = 0; i < docList.size(); i++)
{
imageCount = docList.get(i);
JLabel label = new JLabel( new ImageIcon( imageCount ) );
this.add ( label );
}
Upvotes: 0
Reputation: 649
Because of this :
g.drawImage(imageCount, 0, 0, this);
Try to do something as :
g.drawImage(imageCount, x, y, this); // x, y should have dynamic values.
Hope this Helps.
Upvotes: 0