Results 1 to 1 of 1
  1. #1
    Shadowdude04 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default avoid calls to paint() on resize?

    Hi,
    i'm using swing and i've encountered a problem.
    When my JFrame gets resize the paint method is called (serveral times), the problem is my paint method is quite large and takes times to calculate. I've heard that i could use a BufferedImage to save the "frame" and then paint it again when my Frame has finished resizing (when componentResized() is called). Although i can't get it to work - therefore i've made a little exampel and was hoping someone could write some code, that saved the image (as a BufferedImage) and when my frame gets rezied nothing happens.

    - Thanks in advance

    Java Code:
    import java.awt.Graphics;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class MainClass extends JFrame{
    
    	draw d;
    	int count = 0;
    	public MainClass() {
    		super("!");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300, 200);
    		setVisible(true);
    		d = new draw();
    		add(d); // adds the JPanel
    		addComponentListener(new SizeHandler());
    	}
    
    	public static void main(String[] args) {
    		new MainClass();
    	}
    
    	private class SizeHandler implements ComponentListener {
    		@Override
    		public void componentHidden(ComponentEvent arg0) {
    		}
    
    		@Override
    		public void componentMoved(ComponentEvent arg0) {
    		}
    
    		@Override
    		public void componentResized(ComponentEvent arg0) {
    			System.out.println("paintLine!");
    			d.paintLine();
    		}
    
    		@Override
    		public void componentShown(ComponentEvent arg0) {
    		}
    
    	}
    
    }
    
    class draw extends JPanel{
    	 
    	public void paintLine()
    	{
    		repaint();
    		System.out.println("invoked");
    	}
    	@Override
    	public void paintComponent(Graphics g) {
    			System.out.println("paints");
    			g.drawLine(0, 0, getWidth(), getHeight());
    		
    	}
    	
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-13-2010 at 01:35 PM. Reason: Moderator Edit: Code tags added

Similar Threads

  1. Java System Calls
    By jonnytabpni in forum Advanced Java
    Replies: 3
    Last Post: 03-03-2010, 08:04 PM
  2. How to avoid this problem...
    By pbaudru in forum AWT / Swing
    Replies: 6
    Last Post: 01-21-2010, 02:20 PM
  3. How to avoid focus()..?
    By ehochedez in forum NetBeans
    Replies: 9
    Last Post: 08-27-2009, 12:32 PM
  4. how to avoid input errors?
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •