Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Paint rectangle won't stay displayed!  RSS feed

 
Bob Mazz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Graphics g = designPane.getGraphics( ); to draw a graphic to a JPanel (designPane) in another class. When I run my program the rectangle flashes up for a split second on the JPanel then dissappears. Im using g.fillRect(100, 100, 100, 100); and g.drawRect(100, 100, 100, 100);. Im using designPane.repaint(); and have also setColour so it s not that that is the problem. Also read somewhere I may have to invalidate(); then validate(); my JPanel but that doesn't seem to work either. I really can't find the problem. Any hints welcome! Thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to put all painting in the paintComponent( Graphics g ) method of your designPane JPanel (or in methods that get called from this method). This method gets called for every repaint of the component. What you are currently doing is temporarily painting to the component, then, next time it repaints, it runs its paintComponent() method and clears out what you just painted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!