Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: drawImage() causes endless calls to paintComponent()

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED] drawImage() causes endless calls to paintComponent()

    I found a solution a few hours after posting this. Instead of this --

     			Image image = w.createImage(new MemoryImageSource(20, 20, pixels, 0, 20));

    -- do this:

     			BufferedImage image = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
     
    				image.setRGB(0, 0, 20, 20, pixels, 0, 20);

    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.)

    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!
    Last edited by repaint_forever; August 10th, 2011 at 01:21 AM. Reason: solved


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawImage() causes endless calls to paintComponent()

    Quote Originally Posted by KevinWorkman View Post
    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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. problem with paintComponent
    By jasox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 04:27 PM
  2. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  3. paintComponent(Graphics g)
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2011, 08:55 AM
  4. Throwing an Exception in paintComponent
    By jmack in forum Exceptions
    Replies: 1
    Last Post: January 31st, 2011, 08:12 AM
  5. why paintComponent method is not invoked?
    By ice in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 11th, 2010, 12:30 AM