skytreader
skytreader

Reputation: 11707

Update a BufferedImage in a JFrame

I have a BufferedImage displayed in a JFrame through my own class. I opted to display the BufferedImage using my own class so I can scale it. My paintComponent and update

public class MyBuffIm{
public void paintComponent(Graphics canvas) {
    if (bi == null) {
    } else {
        //bi, maxWidth, and maxHeight were passed to constructor
        canvas.drawImage(bi, 0, 0, maxWidth, maxHeight, null);
    }
}

public void update(Graphics canvas) {
    super.update(canvas);
    if(bi != null){
        //Got this from some tutorial in the net.
        //Done out of desperation :|
        paintComponent(bi.getGraphics());
    }
}
}

I overrode update since the docs are saying something like "If this component is not a lightweight component, the AWT calls the update method in response to a call to repaint". I'm not exactly sure if my component is lightweight or not.

In any case, I have the following code in my Runnable (does not work as I expect it to):

BufferedImage p = SomeMyBuffIm.getBuffIm();
Vector<Point> randomPixels = getRandomPixels(500);
int limit = randomPixels.size()
for (i = 0; i < limit; i++) {
    Point rp = randomPixels.get(i)
    p.setRGB(rp.x, rp.y, Color.red.getRGB());
}
SomeMyBuffIm.repaint();
mainFrame.repaint(); //JFrame call to repaint

I'd like to think that, since I'm scaling my image, I just can't discern the difference between the new and old images. But I've tried the largest values for getRandomPixels still to no effect. My test image, by the way, is just a white sheet so red pixels should stand out in it.

Anything wrong I'm doing?

Upvotes: 1

Views: 3625

Answers (1)

camickr
camickr

Reputation: 324118

I overrode update since the docs are saying something like "If this component is not a lightweight component, the AWT calls the update method in response to a call to repaint". I'm not exactly sure if my component is lightweight or not.

No you should NOT override update(). You would do that with AWT but not with Swing.

If you update the BufferedImage then all you need to do is invoke repaint() on your instance of the MyBuffin class.

If you need more help than post your SSCCE that demonstrates the problem.

Upvotes: 3

Related Questions