Results 1 to 4 of 4
Thread: paint and paintcomponent
- 10-01-2011, 06:16 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
paint and paintcomponent
I am sort of confused about paint and paintcomponent. I set up a small program that will help me to understand.
Why doesn't the circle draw with the gradientpaint defined in the paint method?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); } } }
-
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.
- 10-01-2011, 06:33 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
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?
-
Re: paint and paintcomponent
It doesn't work if you call super.paint(g) at the beginning of paint and super.paintComponent(g) at the beginning of paintComponent?
Usually never. You'll use it if you override painting of child components and borders, something that is a rare occurance...and also when should the paint method be used?
Similar Threads
-
geting value from paintComponent
By gedas in forum New To JavaReplies: 3Last Post: 03-21-2011, 07:56 PM -
Help with paintComponent!
By joeyea in forum Java 2DReplies: 6Last Post: 12-27-2010, 01:59 PM -
paint vs paintComponent for a JPanel
By lightstream in forum AWT / SwingReplies: 4Last Post: 01-29-2009, 02:26 AM -
Problem going outside paintComponent
By Thez in forum Java 2DReplies: 9Last Post: 12-08-2007, 04:59 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks