Results 1 to 4 of 4
  1. #1
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default paint and paintcomponent

    I am sort of confused about paint and paintcomponent. I set up a small program that will help me to understand.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class test extends JFrame
    {
    	public test()
    	{
    		super();
    		add(new mypanel());
    		setVisible(true);
    		setDefaultCloseOperation(3);
    		setSize(500, 500);
    	}
    	public static void main(String args[])
    	{
    		test test = new test();
    	}
    	public class mypanel extends JPanel
    	{
    		public void paint(Graphics g)
    		{
    			Graphics2D g2 = (Graphics2D)g;
    			Rectangle rect = new Rectangle(50,50, 50, 50);
    			GradientPaint paint = new GradientPaint(50, 50, Color.cyan, 100, 100, Color.red);
    			g2.setPaint(paint);
    			super.paint(g);
    		}
    		public void paintComponent(Graphics g)
    		{
    			Graphics2D g2 = (Graphics2D)g;
    			Rectangle rect = new Rectangle(50,50, 50, 50);
    			g2.draw(rect);
    			g2.fill(rect);
    		}
    	}
    }
    Why doesn't the circle draw with the gradientpaint defined in the paint method?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: paint and paintcomponent

    The circle does draw but is erased by the super.paint(g) method which paints the default background for the JPanel over the previous drawings. Usually super.paint(g) is called first. This should all be done in paintComponent though, by the way.

  3. #3
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: paint and paintcomponent

    when i take the super.paint(g) away it still wont paint the rectangle. and also when should the paint method be used?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: paint and paintcomponent

    Quote Originally Posted by wired-in=p View Post
    when i take the super.paint(g) away it still wont paint the rectangle.
    It doesn't work if you call super.paint(g) at the beginning of paint and super.paintComponent(g) at the beginning of paintComponent?

    ..and also when should the paint method be used?
    Usually never. You'll use it if you override painting of child components and borders, something that is a rare occurance.

Similar Threads

  1. geting value from paintComponent
    By gedas in forum New To Java
    Replies: 3
    Last Post: 03-21-2011, 07:56 PM
  2. Help with paintComponent!
    By joeyea in forum Java 2D
    Replies: 6
    Last Post: 12-27-2010, 01:59 PM
  3. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 AM
  4. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 04:59 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

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
  •