Results 1 to 10 of 10
  1. #1
    SuperWoman's Avatar
    SuperWoman is offline Member
    Join Date
    Sep 2014
    Posts
    12
    Rep Power
    0

    Question Initial repaint() fails to work, have to resize window to kickstart things?

    Hello,

    I am learning drawing, so I have a very basic program (see below) wherein a button click adds a rectangle to an array that is looped in the paintComponent() method of the custom JPanel.

    The issue I'm having is that this morning (oh yes) somehow clicking on the button doesn't draw it (repaint doesn't work it seems) but if I resize the main application window then it starts kicking in, the drawing resumes, and any subsequent button click promptly results in another rectangle being drawn.

    You'll see that I added repaint() in places where they're probably not necessary, sorry about that, I was shooting in the dark

    You probably encountered this, its sort of a gotcha I'm not privy to yet, I guess...

    Thanks!

    Java Code:
    package views;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    import javax.swing.*;
    import java.awt.Toolkit;
    
    
    
    public class appMainWindow extends JFrame
    {
    	class PdfLocation
    	{
    		public double xPos;
    		public double yPos; 
    	}
    	
    	class DrawCanvas extends JPanel
    	{
    	      @Override
    	      public void paintComponent(Graphics g) 
    	      {
    	         super.paintComponent(g);
    	         for (PdfLocation p : PdfLocations)
    	         {
    	        	 g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
    	        	 repaint();
    	         }
    	      }
    	}
    	
    	public void AddRectangle()
    	{
    		PdfImagesCount++;
    		lblPdfcount.setText(Integer.toString(PdfImagesCount));
    		
    		PdfLocation rect = new PdfLocation();
    		
    		if (PdfLocations.isEmpty() == false)
    		{
    			PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
    			rect.xPos = spot.xPos + 45;
    			rect.yPos = 10;
    		}	
    		else
    		{
    			rect.xPos = 10;
    			rect.yPos = 10;			
    		}
    		PdfLocations.add(rect);	
    		repaint();
    	}
    
    	private JFrame frame;
    	public ArrayList<PdfLocation> PdfLocations = new  ArrayList<PdfLocation>();
    	public int PdfImagesCount = 0;
    
    	
    	public static final int CANVAS_HEIGHT = 700;
    	public static final int CANVAS_WIDTH = 1000;
    	
    	private DrawCanvas canvas;
    	private JLabel lblPdfcount;
    	
    	public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
    
    	public appMainWindow() 
    	{ 
    		
    		// Set up a custom drawing JPanel
    	    canvas = new DrawCanvas();
    	    canvas.setBackground(Color.WHITE);
    	    canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT);
    		initialize(); 
    	}
    
    	private void initialize() 
    	{
    		frame = new JFrame();
    		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(appMainWindow.class.getResource("/icons/appIcon2.png")));
    		frame.setBounds(100, 100, 1200, 728);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(true);
    		frame.setTitle("Shaareable Apps Presents ~ Pdf Imposition Monster");
    		
    		frame.getContentPane().setLayout(null);
    		
    		JButton btnAddARectangle = new JButton("Add a rectangle");
    		btnAddARectangle.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) 
    			{
    				AddRectangle();
    				repaint();
    			}
    		});
    		btnAddARectangle.setBounds(6, 219, 151, 29);
    		frame.getContentPane().add(btnAddARectangle);
    		
    		
    		frame.getContentPane().add(canvas);
    		
    		lblPdfcount = new JLabel("PdfCount");
    		lblPdfcount.setBounds(27, 260, 61, 16);
    		frame.getContentPane().add(lblPdfcount);
    	}
    
    }

  2. #2
    SuperWoman's Avatar
    SuperWoman is offline Member
    Join Date
    Sep 2014
    Posts
    12
    Rep Power
    0

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    On a side note, in eclipse, I also lost the Design button that shows me the ui preview (using WindowsBuilder) anyone knows how to get it back?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    I haven't looked at this in depth but you are extending JFrame (which you shouldn't do)
    and also creating a JFrame (which you should do). Don't extend JFrame, but extend JPanel.
    Add the panel instance (via this) to your JFrame. Other tweaks may be required.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    SuperWoman's Avatar
    SuperWoman is offline Member
    Join Date
    Sep 2014
    Posts
    12
    Rep Power
    0

    Question Re: Initial repaint() fails to work, have to resize window to kickstart things?

    Hello Jim,

    Thank you for your suggestion. I changed it. Unless I resize the whole window, the custom JPanel doesn't do its repaint(). Once I resize the window, it starts working, i.e, everytime I click the button, a rectangle is added to the custom JPanel. But when the application loads, clicking the button doesn't repaint(). Say I click 5 five times, nothing changes, then I resize the window, then I'll see right away the five rectangles, and from then on, each clicks generates a rectangle as intended.

    here's the code:

    Java Code:
    package views;
    
    import java.awt.EventQueue;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import java.awt.Color;
    
    import javax.swing.JButton;
    
    import views.appMainWindow.PdfLocation;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.ArrayList;
    import javax.swing.JLabel;
    
    public class Sandbox extends JPanel
    {
    	public ArrayList<PdfLocation> PdfLocations = new  ArrayList<PdfLocation>();
    	public int PdfImagesCount = 0;
    	
    	class PdfLocation
    	{
    		public double xPos;
    		public double yPos; 
    	}
    	
    	class DrawingCanvas extends JPanel
    	{
    		@Override
    	  	public void paintComponent(Graphics g) 
    		{
    			super.paintComponent(g);
    	        for (PdfLocation p : PdfLocations)
    	        {
    	        	g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
    	        	repaint();
    	        }
    	     }		
    	}
    
    	public void AddRectangle()
    	{
    		PdfImagesCount++;
    		lblPdfcount.setText(Integer.toString(PdfImagesCount));
    		
    		PdfLocation rect = new PdfLocation();
    		
    		if (PdfLocations.isEmpty() == false)
    		{
    			PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
    			rect.xPos = spot.xPos + 45;
    			rect.yPos = 10;
    		}	
    		else
    		{
    			rect.xPos = 10;
    			rect.yPos = 10;			
    		}
    		PdfLocations.add(rect);	
    		repaint();
    	}
    	private JFrame frame;
    	private JLabel lblPdfcount;
    	private JButton btnAdd;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Sandbox window = new Sandbox();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public Sandbox() 
    	{
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 826, 301);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		
    		DrawingCanvas canvas = new DrawingCanvas();
    		canvas.setBackground(Color.WHITE);
    		canvas.setBounds(109, 11, 691, 240);
    		frame.getContentPane().add(canvas);
    		
    		btnAdd = new JButton("Add");
    		btnAdd.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) 
    			{
    				AddRectangle();
    				repaint();
    			}
    		});
    		btnAdd.setBounds(10, 11, 89, 23);
    		frame.getContentPane().add(btnAdd);
    		
    		lblPdfcount = new JLabel("PdfCount");
    		lblPdfcount.setBounds(10, 45, 46, 14);
    		frame.getContentPane().add(lblPdfcount);
    	}
    }

  5. #5
    SuperWoman's Avatar
    SuperWoman is offline Member
    Join Date
    Sep 2014
    Posts
    12
    Rep Power
    0

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    I fixed it by adding frame.repaint() after calling AddRectangle() in my button event.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    Like I said, I didn't look that closely at it and it needed to be tweaked. So since you are doing your painting on a canvas,
    no need for Sandbox to extend JPanel (mea culpa). However, you need to remove the repaint from within paintComponent()
    otherwise, it will never end and overflow the call stack.

    And change repaint() in the addRectangle to frame.repaint().

    Also, here is a tip. JFrame.add() method is a helper method that adds the component to the content pane. So you can do it that
    way (or your way if you want).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    SuperWoman's Avatar
    SuperWoman is offline Member
    Join Date
    Sep 2014
    Posts
    12
    Rep Power
    0

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    Hi Jim and thanks for the tip. Would you know by any chance if Eclipse/Java EE/WindowsBuilder has a way to see in the design view our custom components?

    For example in this code the DrawingCanvas extends a JPanel, but for an unknown(to me) reason I can't see it in the design view.

    I wish I could, it'd be much easier to position etc....

    Thank you again Jim

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    I use Eclipse but I am unfamiliar with what you refer to as the design view. If you are talking about a GUI designer, for placement
    of components then I recommend you use layout managers instead. GUI designers tend to use absolute positioning which
    can make your app less flexible.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    Quote Originally Posted by SuperWoman View Post
    Hi Jim and thanks for the tip. Would you know by any chance if Eclipse/Java EE/WindowsBuilder has a way to see in the design view our custom components?
    ... why mention Java EE I wonder? Possibly only because you are using the version of Eclipse called 'Eclipse for Java EE Developers' ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    SuperWoman's Avatar
    SuperWoman is offline Member
    Join Date
    Sep 2014
    Posts
    12
    Rep Power
    0

    Default Re: Initial repaint() fails to work, have to resize window to kickstart things?

    Quote Originally Posted by gimbal2 View Post
    ... why mention Java EE I wonder? Possibly only because you are using the version of Eclipse called 'Eclipse for Java EE Developers' ?
    Hi Gimbal, That is correct.

Similar Threads

  1. gridbaglayout does not resize with the window
    By Marcoita_88 in forum AWT / Swing
    Replies: 5
    Last Post: 08-06-2011, 10:31 AM
  2. Window resize icon...
    By pele in forum SWT / JFace
    Replies: 3
    Last Post: 06-09-2008, 08:31 AM
  3. Replies: 1
    Last Post: 02-16-2008, 11:43 PM
  4. Repaint fails when using threads
    By rjevans2000 in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-21-2007, 11:22 PM
  5. Repaint fails when using threads
    By rjevans2000 in forum AWT / Swing
    Replies: 3
    Last Post: 08-15-2007, 05:42 PM

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
  •