swing - Java simple paint code not working -


I have this simple color code that should be attracted but instead it's oval When I extract the line code> super.paintComponent (g) , this paint works and not only moves the oval, but I think we do not have this line Should remove, so what can I do to leave it, but still the desired result Get in?

  class OraclePaint increases JFrame {public static zero main (string [] args) {OraclePaint ss = new OraclePaint (); Ss.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Ss.add (new MyPanel ()); Ss.setSize (250, 200); Ss.setVisible (true); }} Category MyPanel JPanel {Private int x = -10, y = -10; Public MyPanel () {addMouseMotionListener (New MouseMotionAdapter () {Public Zero MouseDragged (Mouse Event Event) {x = event.getX (); y = event.getY (); Rangana ();}}}; // And Call AddMouseMotionListener} Public Zero Paint Component (Graphics G) {super.paintComponent (G); Gifil (X, Y, 22, 22);}}  

an overridden paintComponent super.paintComponent (g) De], and it is true that it removes the background (i.e., which was painted before it will be removed).

In the swing, Everything that you want to paint should be painted in the paintComponent method or in any way that is called from there, and the same graphics object is received.)

If you save anything you've saved "save" Do not want, then you paint everything must paint a picture (that is, Bfrd Image ), and the image of your paintComponent method.

There are some other problems with the code, but without changing a lot of the remaining code, it can be achieved almost like (!):

  import java. Awt.color; Import java.awt.Graphics; Import java.awt.event.MouseEvent; Import java.awt.event.MouseMotionAdapter; Import java.awt.image.BufferedImage; Import javax.swing.JFrame; Import javax.swing.JPanel; Class OraclePaint JFrame {public static increases zero main (string [] args {OraclePaint ss = new OraclePaint (); Ss.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Ss.add (new MyPanel ()); Ss.setSize (250, 200); Ss.setVisible (true); }} Category MyPanel JPanel {Private BufferedImage image = Faucet; Public MyPanel () {addMouseMotionListener (New MouseMotionAdapter () {@Override public void mouseDragged (MouseEvent Event) {if (image! = Null) {// picture graphics in image g = image.getGraphics (); g.setColor (Color. Black); gifil (event.getX (), event.getY (), 22, 22); g.dispose (); repaint ();}}}); // And call addMouseMotionListener} // Make sure the image is not 'null' and it is private zero authenticated image (like this panel) {if (image == null) {image = new BufferedImage ( GetWidth (), getHeight (), buffed image. TYPE_INT_ARGB); } If (image.getWidth ()! = GetWidth () || image.getHeight ()! = GetHeight ()) {Buffer image new image = new buffard image (getWidth), getHeight (), buffered image. TYPE_INT_ARGB); Graphics G = new image.grad graphics (); G.drawImage (image, 0, 0, empty); G.dispose (); Image = new image; }} @ Override Public Watch Paint Coment (Graphics G) {SuperPaintConant (G); ValidateImage (); G.drawImage (image, 0, 0, empty); }}  

Comments