CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    60

    Flickering during resizing.

    When I attempt to rescale a JFrame, which has no listeners, and has two JPanels in it, the layout takes a bit to resize itself. It's not slowness due to long calculations, since there's nothing that would take that long. I've stripped out everything that has to do with the underlying simulation it's supposed to display (and replaced it with plain colors), and it still has that small lag. EDIT: Kinda forgot the main point. How can this be stopped?

    Main window:
    Code:
    package graphics;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    
    /**
     * Represents the top-level window for the GUI of the logic toolkit
     * 
     * @author Henry Swanson
     * @version 1.0 Apr 3, 2011
     */
    public class MainWindow extends JFrame {
    	
    	/** Serial ID. What is this? */
    	private static final long serialVersionUID = -3880026026104218593L;
    	
    	/**
    	 * Creates a {@link MainWindow} and the {@link WorkspacePanel} and
    	 * {@link ActionPanel} within.
    	 */
    	public MainWindow() {
    		super("Logic Toolkit");
    		WorkspacePanel w = new WorkspacePanel();
    		ActionPanel a = new ActionPanel();
    		add(w, BorderLayout.CENTER);
    		add(a, BorderLayout.LINE_END);
    		setMinimumSize(new Dimension(400, 300));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pack();
    		setVisible(true);
    	}
    	
    }
    Workspace panel:
    Code:
    package graphics;
    
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JPanel;
    
    /**
     * The panel where all the gates are shown.
     * 
     * @author Henry Swanson
     * @version 1.0 Apr 3, 2011
     */
    public class WorkspacePanel extends JPanel {
    	
    	/** Serial ID. What is this? */
    	private static final long serialVersionUID = 6634247456369380795L;
    	
    	/**
    	 * Creates a {@link WorkspacePanel}.
    	 */
    	public WorkspacePanel() {
    		super(null);
    		setPreferredSize(new Dimension(400, 300));
    	}
    	
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponents(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setColor(Color.GREEN);
    		g2d.fillRect(0, 0, 1024, 1024);
    	}
    }
    Action panel:
    Code:
    package graphics;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JPanel;
    
    /**
     * The panel where buttons for actions such as adding and removing gates are.
     * 
     * @author Henry Swanson
     * @version 1.0 Apr 3, 2011
     */
    public class ActionPanel extends JPanel {
    	
    	/** Serial ID. what is this? */
    	private static final long serialVersionUID = 9139567862742794039L;
    	
    	/**
    	 * Creates a {@link ActionPanel}.
    	 */
    	public ActionPanel() {
    		super(null);
    		setPreferredSize(new Dimension(100, 300));
    	}
    	
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponents(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setColor(Color.CYAN);
    		g2d.fillRect(0, 0, 1024, 1024);
    	}
    }
    Last edited by henryswanson; April 7th, 2011 at 06:31 PM.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Flickering during resizing.

    If you mean the delay before repainting when the frame is resized rapidly, I see what you mean - paintComponent seems to be called more often than the graphics painting is actually done - the painting seems to be deferred while resizing is in progress. The frame border resizes smoothly, but that's handled in the frame peer, which isn't accessible.

    I tried just setting the background colour of the panels rather than hand-painting the panel colours, which works, but gives exactly the same effect.

    I can't think of a simple way to fix this, it seems to be handled by the Graphics library (btw, you don't need Graphics2D for fillRect), which presumably calls the frame peer to update itself.

    You have to honor failure, because failure is just the negative space around success...
    R. Nelson
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Oct 2010
    Posts
    60

    Re: Flickering during resizing.

    Alright. I suppose I can just ignore the problem, and hope the user won't be bothered by it. :P

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured