簡単なペイント プログラムを作成しようとしていますが、myDrawingPanel2.repaint() を呼び出すたびに、プログラムは描画された古いドットを単純に消去し、新しい位置のドットに置き換えます。Java JDK(u15)の最新リリースとNetbeansの最新リリースを搭載したMac OS X 10.8でこれを実行しています。前のドットを消さずにドットを描き続けてほしい。
これが私の申請書のコードです:
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();
}
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);
}
}