[SOLVED] drawImage() causes endless calls to paintComponent()
I found a solution a few hours after posting this. Instead of this --
-- do this:
I don't know why the original code had the problem, however. I'm leaving the remainder of this message alone in case anyone can learn from (or explain) the mistake.
The program compiles without errors or warnings. When it runs, paintComponent() is called repeatedly, about a thousand times per second, even though no events seem to be occurring that would make repainting necessary. If drawImage() is commented out, so that only drawLine() is called, there is no such repetition of paintComponent(); so the problem seems to involve a difference between drawImage() and drawLine(). This program doesn't do anything useful other than exhibit the problem; it's the result of simplifying a larger program. (Note: drawImage() has no visible effect since the pixels are all zeros, but that's not the problem. The problem is that paintComponent() is called too much.)
Code Java:
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public Test() {}
public static TestWin w;
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
Test Test = new Test();
try {
w = new TestWin();
}
catch (Exception e) {
System.out.println("Ouch: " + e);
}
}
};
EventQueue.invokeLater(runner);
}
static void testImage(Graphics g) {
int pixels[] = new int[400];
if (g != null && pixels != null) {
Image image = w.createImage(new MemoryImageSource(20, 20, pixels, 0, 20));
if (image != null) {
g.drawLine(10, 10, 20, 20);
g.drawImage(image, 20, 20, w);
}
}
}
}
class TestWin extends JFrame {
public TestPanel panel;
public TestWin() {
panel = new TestPanel();
getContentPane().add(panel);
setBounds(100, 100, 300, 300);
setVisible(true);
toFront();
}
public void handleMouseEvent(MouseEvent m) {}
public void handleActionEvent(ActionEvent ae, final int command) {}
public void handleResizeEvent(ComponentEvent e) {}
public void handleWindowEvent(WindowEvent e) {}
protected class TestPanel extends JPanel {
public TestPanel() {}
public int paintComponentCount;
public void paintComponent(Graphics g) {
System.out.println("paintComponent " + paintComponentCount++);
super.paintComponent(g);
Test.testImage(g);
}
}
}
Thanks in advance for any help!
Re: drawImage() causes endless calls to paintComponent()
I'm not sure that the original createImage() does what you think it does. Apparently it causes a repaint(). I'm not quite sure why, but I've never seen anybody try to create an image that way. What are you actually trying to accomplish?
Re: drawImage() causes endless calls to paintComponent()
Quote:
Originally Posted by
KevinWorkman
I'm not sure that the original createImage() does what you think it does. Apparently it causes a repaint(). I'm not quite sure why, but I've never seen anybody try to create an image that way. What are you actually trying to accomplish?
The goal is to create an image and display it in a window (in a program that also does a lot of other things). It works fine now with "new BufferedImage()" and "image.setRGB()".
It's only an academic question now, why createImage() plus drawImage() causes endless paintComponent(). The reason for using createImage() in the first place was that it was probably the first method of creating an image that I found, and it appeared to work correctly, other than the strange side effect. Maybe createImage() is designed for some completely different purpose, I don't know.
Re: drawImage() causes endless calls to paintComponent()
I think that your original createImage() method is designed for double buffering, which might explain why it calls repaint(). I don't have time to track down what that method is actually doing behind the scenes, but I'm satisfied with the assumption that mysterious double buffering things are happening, which is calling repaint, which calls your paintComponent, which is calling the mysterious double buffering, etc. If you want to track it down, you might try digging through the method's source, or you could try throwing in some stack traces or using a debugger to figure out what's calling repaint().