0

I am doing a project with double buffering. When I paint, it simply paints on top of the old layers, but I need to erase them. Repaint() didn't work, but I'm guessing something equally as simple is the answer.

Any ideas?

Added code, and now it disappears, but it erases the background color.

 public void paint(Graphics g)
{

super.paint(buffer);


for(Projectile p: projectiles)
drawRectImage(buffer, p.image, p.getRectangle());

}

user2956947
  • 239
  • 1
  • 2
  • 12
  • `"My code is convoluted, so I can post it, but if there's a simple fix then there isn't the need."` -- I doubt that anyone will be able to help you with this small bit of information. You've got a bug in your code, and so will need to show your code. The only exception is if you're not calling the super method in your paint (or better, paintComponent) method override. I think that you'll need to simplify your code and post the pertinent code, preferably a [minimal example program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels May 29 '14 at 20:04
  • You should draw a large rectangle at the start of the draw method. That will "clear" the screen. – BitNinja May 29 '14 at 20:04
  • 3
    Please understand that the whole reason we all recommend that you avoid making convoluted complex code is for this very reason: debugging such code is a royal PIA. So do yourself and us a favor -- refactor, simplify. So again, if you're not calling `super.paintComponent(g)` in your `paintComponent(Graphics g)` override, then put that call in there as the first method call. If you're already doing this, then post your cleaned up code. – Hovercraft Full Of Eels May 29 '14 at 20:05

1 Answers1

3

Suggestions:

  • If this is a Swing GUI, then don't override the paint method, but instead override the paintComponent method. This won't help your current problem, but will help prevent future problems including problems with painting of borders and child components.
  • If Swing (again you don't say), then make sure that your painting component extends JPanel, not JComponent, since JPanel is opaque and fills its background rectangle in its super method.
  • If it's not Swing, then you should strongly consider changing from AWT to Swing.
  • If you're still stuck, then yep, you'll want to create and post a minimal example program. Please check out the link.
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I import `java.awt.image.BufferedImage` so I assume I'm using awt for the painting even though I use swing JButtons and JPanels. I barely figured out how to use awt. How would I change awt to swing? – user2956947 Jun 01 '14 at 00:04
  • @user2956947: your assumption is incorrect. Please read: * [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html): introductory tutorial to Swing graphics * [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html): advanced tutorial on Swing graphics – Hovercraft Full Of Eels Jun 01 '14 at 20:39