Results 1 to 1 of 1
- 03-13-2010, 12:20 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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
Moderator Edit: Code tags addedJava 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()); } }Last edited by Fubarable; 03-13-2010 at 12:35 PM. Reason: Moderator Edit: Code tags added
Similar Threads
-
Java System Calls
By jonnytabpni in forum Advanced JavaReplies: 3Last Post: 03-03-2010, 07:04 PM -
How to avoid this problem...
By pbaudru in forum AWT / SwingReplies: 6Last Post: 01-21-2010, 01:20 PM -
How to avoid focus()..?
By ehochedez in forum NetBeansReplies: 9Last Post: 08-27-2009, 11:32 AM -
how to avoid input errors?
By Sinnergy in forum New To JavaReplies: 5Last Post: 02-02-2009, 11:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks