I have a shape that is controlled via the keypad and is associated with a timer. When the shape reaches a certain coordinate point, it stops the timer. So how would I go about resetting the shape back to it's original starting position while leaving a painted shape back where the timer stopped?
public class ForStack extends JPanel implements KeyListener,ActionListener{
Timer t = new Timer(800, this);
int count = 0;
double x = 0, y = 0, velX = 0, velY = 0;
public ForStack (){
t.start();
addKeyListener(this);
setFocusable(true);
}
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rectangle = new Rectangle2D.Double(x,y,50, 50);
g2.setColor(Color.BLACK);
g2.fill(rectangle);
int counter = 0;int x = 0; int y = 0;
g.setColor(Color.RED);
if (counter == 0){
do{
g.drawRect(x,y,50,50);
x += 50;
counter++;
}while(counter != 6);
}
x=0;
y=0;
if (counter == 6){
do{
g.drawRect(x,50,50,50);
x += 50;
counter++;
}while(counter != 12);
}
x=0;
y=0;
if (counter == 12){
do{
g.drawRect(x,100,50,50);
x += 50;
counter++;
}while(counter != 18);
}
x=0;
y=0;
if (counter == 18){
do{
g.drawRect(x,150,50,50);
x += 50;
counter++;
}while(counter != 24);
}
x=0;
y=0;
if (counter == 24){
do{
g.drawRect(x,200,50,50);
x += 50;
counter++;
}while(counter != 30);
}
x=0;
y=0;
if (counter == 30){
do{
g.drawRect(x,250,50,50);
x += 50;
counter++;
}while(counter != 36);
}
x=0;
y=0;
if (counter == 36){
do{
g.drawRect(x,300,50,50);
x += 50;
counter++;
}while(counter != 42);
}
x=0;
y=0;
if (counter == 42){
do{
g.drawRect(x,350,50,50);
x += 50;
counter++;
}while(counter != 48);
}
x=0;
y=0;
if (counter == 48){
do{
g.drawRect(x,400,50,50);
x += 50;
counter++;
}while(counter != 54);
}
x=0;
y=0;
if (counter == 54){
do{
g.drawRect(x,450,50,50);
x += 50;
counter++;
}while(counter != 60);
}
}
@Override
public void actionPerformed(ActionEvent e){
if(x == 0 & y == 400 ){
t.stop();
}
repaint();
x += velX;
y += velY;
}
@Override
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if (code == KeyEvent.VK_DOWN){
velY += 50;
velX += 0;
}
else if (code == KeyEvent.VK_LEFT){
velX -= 50;
velY = 0;
}
else if (code == KeyEvent.VK_RIGHT){
velX += 50;
velY = 0;
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e){}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new ForStack());
frame.setVisible(true);
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // end of main
} // end of class