Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By rru96

Thread: JComponent wont repaint entire area

  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    91
    Rep Power
    0

    Default JComponent wont repaint entire area

    So, I am trying to make a little color picker app and have a few small issues I have run into. The major one that I am really stumped on is that my JLabels are not updating as expected. It seems that the text is never leaving and just simply staying there. It never erases the old value, instead just repaints() on top. Is there something I am missing, or do I need to go look into the docs a little more?
    Java Code:
    package com.cc.ryan;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.JTextArea;
    
    public class DisplayPanel extends JPanel{
    	private Font font = new Font("Arial", Font.BOLD, 16);
    	private int red, green, blue;
    	private JLabel rLabel, gLabel, bLabel;
    	private JSlider rSlider, gSlider, bSlider;
    	private JTextArea hexCode;
    	
    	// CONSTRUCTOR
    	public DisplayPanel(){
    		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		// RED
    		c.gridx = 0;
    		c.gridy = 0;
    		rLabel = new JLabel("RED");
    		rLabel.setFont(font);
    		add(rLabel, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		rSlider = new JSlider(0,255,0);
    		add(rSlider, c);
    		
    		//GREEN
    		c.gridx = 0;
    		c.gridy = 2;
    		gLabel = new JLabel("GREEN");
    		gLabel.setFont(font);
    		add(gLabel, c);
    		
    		c.gridx = 0;
    		c.gridy = 3;
    		gSlider = new JSlider(0,255,0);
    		add(gSlider, c);
    		
    		// BLUE
    		c.gridx = 0;
    		c.gridy = 4;
    		bLabel = new JLabel("BLUE");
    		bLabel.setFont(font);
    		add(bLabel, c);
    		
    		c.gridx = 0;
    		c.gridy = 5;
    		bSlider = new JSlider(0,255,0);
    		add(bSlider, c);
    		
    		//---END BLUE
    		
    		c.gridx = 0;
    		c.gridy = 6;
    		hexCode = new JTextArea("000000");
    		hexCode.setFont(font);
    		add(hexCode, c);
    		
    	}
    	
    	// PAINT METHOD
    	public void paintComponent(Graphics g){
    		setRed();
    		setGreen();
    		setBlue();
    		g.setColor(new Color(red, green, blue));
    		g.fillRect(0, 0, 300, 100);
    		g.fillRect(0, 250, 300, 200);
    		hexCode.setText(Integer.toHexString(red)+Integer.toHexString(green)+Integer.toHexString(blue));
    		rLabel.setText("RED " + Integer.toString(getRed()));
    		gLabel.setText("GREEN " + Integer.toString(getGreen()));
    		bLabel.setText("BLUE " + Integer.toString(getBlue()));
    		
    		repaint();
    		
    		
    	}
    	
    	//  GETTERS AND SETTERS
    	public void setRed(){
    		red = rSlider.getValue();
    
    	}
    	public void setGreen(){
    		green = gSlider.getValue();
    	}
    	public void setBlue(){
    		blue = bSlider.getValue();
    	}
    
    	public int getRed(){
    		return red;
    	}
    	public int getGreen(){
    		return green;
    	}
    	public int getBlue(){
    		return blue;
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JComponent wont repaint entire area

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JComponent wont repaint entire area

    Comments:
    -- Don't update the label and text field text from within a painting method override. Painting methods are for painting and painting alone. Use a listener attached to each slider to update the corresponding label and the text field and call repaint() from the listener code.
    -- Never never never call repaint() from within a painting method. Only the coalescing of pending paint events saves you from locking up your processor with an infinite loop.

    Make those changes and post back if you still have a problem.

    Also note that when posting an SSCCE you should leave out anything that isn't relevant to the stated problem. Here, the Font setting is totally irrelevant.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    91
    Rep Power
    0

    Default Re: JComponent wont repaint entire area

    Thank you DB. I looked around in the Docs and tried implementing a ChangeListener. While I was able to remove non-drawing code from my paint method, I still seem to be getting the same issue. Is there a method I am not seeing that can erase a previously painted on component so that I dont get the leftovers of prior repaints()?

    Java Code:
    package com.cc.ryan;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.JTextArea;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class DisplayPanel extends JPanel{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private Font font = new Font("Arial", Font.BOLD, 16);
    	private int red, green, blue;
    	private JLabel rLabel, gLabel, bLabel;
    	private JSlider rSlider, gSlider, bSlider;
    	private JTextArea hexCode;
    	
    	// CONSTRUCTOR
    	public DisplayPanel(){
    		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		// RED
    		c.gridx = 0;
    		c.gridy = 0;
                    rLabel = new JLabel("RED");
    		add(rLabel, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		rSlider = new JSlider(0,255,0);
    		rSlider.addChangeListener(new ChangeListener(){
    			public void stateChanged(ChangeEvent ce){
    				rLabel.setText("RED " + getRed());
    				hexCode.setText(Integer.toHexString(red) + Integer.toHexString(green) + Integer.toHexString(blue));
    				repaint();
    			}
    		});
    		add(rSlider, c);
    		
    		//GREEN
    		c.gridx = 0;
    		c.gridy = 2;
    		gLabel = new JLabel("GREEN");
    		add(gLabel, c);
    		
    		c.gridx = 0;
    		c.gridy = 3;
    		gSlider = new JSlider(0,255,0);
    		gSlider.addChangeListener(new ChangeListener(){
    			public void stateChanged(ChangeEvent ce){
    				gLabel.setText("GREEN " + getGreen());
    				hexCode.setText(Integer.toHexString(red) + Integer.toHexString(green) + Integer.toHexString(blue));
    				repaint();
    			}			
    		});
    		add(gSlider, c);
    		
    		// BLUE
    		c.gridx = 0;
    		c.gridy = 4;
    		bLabel = new JLabel("BLUE");
    		add(bLabel, c);
    		
    		c.gridx = 0;
    		c.gridy = 5;
    		bSlider = new JSlider(0,255,0);
    		bSlider.addChangeListener(new ChangeListener(){
    			public void stateChanged(ChangeEvent ce){
    				bLabel.setText("BLUE " + getBlue());
    				hexCode.setText(Integer.toHexString(red) + Integer.toHexString(green) + Integer.toHexString(blue));
    				repaint();
    			}
    		});
    		add(bSlider, c);
    		
    		//---END BLUE
    		
    		c.gridx = 0;
    		c.gridy = 6;
    		hexCode = new JTextArea("000000");
    		hexCode.setFont(font);
    		add(hexCode, c);
    		
    	}
    	
    	// PAINT METHOD
    	public void paintComponent(Graphics g){
    		setRed();
    		setGreen();
    		setBlue();
    		g.setColor(new Color(red, green, blue));
    		g.fillRect(0, 0, 300, 100);
    		g.fillRect(0, 250, 300, 200);
    		
    	}
    	
    	//  GETTERS AND SETTERS
    	public void setRed(){
    		red = rSlider.getValue();
    	}
    	public void setGreen(){
    		green = gSlider.getValue();
    	}
    	public void setBlue(){
    		blue = bSlider.getValue();
    	}
    
    	public int getRed(){
    		return red;
    	}
    	public int getGreen(){
    		return green;
    	}
    	public int getBlue(){
    		return blue;
    	}
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JComponent wont repaint entire area

    Invoke the super implementation to clear the background.

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    91
    Rep Power
    0

    Default Re: JComponent wont repaint entire area

    Worked perfectly,
    Java Code:
    super.paintComponent(g);
    Now to figure out exactly what is taking place...

    Thank you so much.. Now off to work on my next issue
    DarrylBurke likes this.

Similar Threads

  1. panel wont update when repaint is called
    By yemista in forum AWT / Swing
    Replies: 3
    Last Post: 10-25-2011, 06:58 PM
  2. wont show entire JLabel
    By Billaguana in forum AWT / Swing
    Replies: 6
    Last Post: 02-03-2011, 10:18 PM
  3. Replies: 2
    Last Post: 10-01-2010, 07:18 PM
  4. Repaint the entire JFrame (GroupLayout)
    By Willi in forum AWT / Swing
    Replies: 13
    Last Post: 12-19-2009, 10:11 PM
  5. Looped repaint of display area
    By gligor_kot in forum Java 2D
    Replies: 3
    Last Post: 07-28-2009, 12:53 PM

Posting Permissions

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