2

I'm trying to create a simple paint program, but every time I call myDrawingPanel2.repaint(), the program simply erases the old dot that was drawn, and replaces it with a dot at the new location. I'm running this on Mac OS X 10.8 with the latest release of the Java JDK(u15) and the latest release of Netbeans. I want the program to continue drawing dots without erasing the previous one.

Here is the code for my application form:

private void myDrawingPanel2MouseClicked(java.awt.event.MouseEvent evt) {                                             
    myDrawingPanel2.setxC(evt.getX());
    myDrawingPanel2.setyC(evt.getY());

    myDrawingPanel2.repaint();
}                                            

private void myDrawingPanel2MouseDragged(java.awt.event.MouseEvent evt) {                                             
    myDrawingPanel2.setxC(evt.getX());
    myDrawingPanel2.setyC(evt.getY());

    myDrawingPanel2.repaint();
}                                            

private void upActionPerformed(java.awt.event.ActionEvent evt) {                                   
    myDrawingPanel2.setyC(myDrawingPanel2.getyC()-5);

    myDrawingPanel2.repaint();
}                                  

private void downActionPerformed(java.awt.event.ActionEvent evt) {                                     
    myDrawingPanel2.setyC(myDrawingPanel2.getyC()+5);

    myDrawingPanel2.repaint();
}                                    

private void leftActionPerformed(java.awt.event.ActionEvent evt) {                                     
    myDrawingPanel2.setxC(myDrawingPanel2.getxC()-5);

    myDrawingPanel2.repaint();
}                                    

private void rightActionPerformed(java.awt.event.ActionEvent evt) {                                      
    myDrawingPanel2.setxC(myDrawingPanel2.getxC()+5);

    myDrawingPanel2.repaint();
}                                     

private void upKeyPressed(java.awt.event.KeyEvent evt) {                              
    if(evt.getKeyCode() == 38) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()-5);
    }
    if(evt.getKeyCode() == 40) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()+5);
    }
    if(evt.getKeyCode() == 39) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()+5);
    }
    if(evt.getKeyCode() == 37) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()-5);
    }
    myDrawingPanel2.repaint();
}                             

private void leftKeyPressed(java.awt.event.KeyEvent evt) {                                
    if(evt.getKeyCode() == 38) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()-5);
    }
    if(evt.getKeyCode() == 40) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()+5);
    }
    if(evt.getKeyCode() == 39) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()+5);
    }
    if(evt.getKeyCode() == 37) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()-5);
    }
    myDrawingPanel2.repaint();
}                               

private void downKeyPressed(java.awt.event.KeyEvent evt) {                                
    if(evt.getKeyCode() == 38) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()-5);
    }
    if(evt.getKeyCode() == 40) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()+5);
    }
    if(evt.getKeyCode() == 39) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()+5);
    }
    if(evt.getKeyCode() == 37) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()-5);
    }
    myDrawingPanel2.repaint();
}                               

private void rightKeyPressed(java.awt.event.KeyEvent evt) {                                 
    if(evt.getKeyCode() == 38) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()-5);
    }
    if(evt.getKeyCode() == 40) {
        myDrawingPanel2.setyC(myDrawingPanel2.getyC()+5);
    }
    if(evt.getKeyCode() == 39) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()+5);
    }
    if(evt.getKeyCode() == 37) {
        myDrawingPanel2.setxC(myDrawingPanel2.getxC()-5);
    }
    myDrawingPanel2.repaint();
} 

And here is the code for my JPanel:

package drawingdemo;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;


public class MyDrawingPanel extends JPanel{

int xC = 150;
int yC = 100;
int diameter = 25;

public int getxC() {
    return xC;
}

public void setxC(int xC) {
    this.xC = xC;
}

public int getyC() {
    return yC;
}

public void setyC(int yC) {
    this.yC = yC;
}

public int getDiameter() {
    return diameter;
}

public void setDiameter(int diameter) {
    this.diameter = diameter;
}

@Override
public void paintComponent(Graphics g){

    //super.paintComponent(g);

    g.setColor(Color.RED);
    g.fillOval(xC - (diameter/2), yC - (diameter/2), diameter, diameter);

}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
rafro4
  • 65
  • 7
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org/). BTW - `//super.paintComponent(g);` remove the part that is `//`. – Andrew Thompson Oct 01 '13 at 17:08
  • this is because of `g` variable scope, this is called and created for each call. you would either get the g with `MyDrawingPanel` creation and draw with it for each call, or keep(track) the each paint call. –  Oct 01 '13 at 19:03
  • See the examples cited [here](http://stackoverflow.com/a/17537829/230513). – trashgod Oct 01 '13 at 23:22
  • How do I post an SSCCE? – rafro4 Oct 02 '13 at 16:40

1 Answers1

1

The program does what you told it to do. The paintComponent is intended for painting the entire component and your implementation paints a single dot. If you want your component to show more than one dot you have provide a paintComponent implementation that draws more than one dot. This implies that you have to remember the location of all dots.

There are some tricks to force Swing into not clearing the background but these would be kludges as you are not implementing the correct semantic. The visible on-screen state may be lost at any time, e.g. when another window overlaps yours or if the user minimizes and restores the window or the display settings change. So you have to provide a paintComponent implementation that is able to restore the entire intended display contents anyway.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • Could you give me some example code that would allow me to save each of the objects in an array list? That way I can render the objects from the array off-screen and then push them onto the screen – rafro4 Oct 02 '13 at 16:39