-1

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

1 Answers1

1

Normally when there are so many things wrong in a post's code, I tend to stay away, as the answer to problem requires too many fixes, and involve breaking the code down myself and refactoring everything. I'm not going to do that. I'll leave that to you. I'll just point out the things I notice, and leave it up to you to try and fix.

  1. Get rid of all the while loops. You are including way too much logic in your paint method. I really don't have any idea either what you are trying to accomplish with that logic, but it just looks completely wrong.

  2. What you can do is just keep a List or Rectangles. Add a new Rectangle to the List when you want another one. Only one of them will animate at a time. All the rest will be stationed at a the last point. Use an index for the List In the timer you will check for that index, and only if that index is equal, will it animate. You should just loop through the List in the paint method and draw all of them. The point is to keep your logic separate from your painting.

  3. Don't override paint, but instead paintComponent

  4. Use Key Bindings instead of KeyListener

  5. if(x == 0 & y == 400 ){ Is that what you really want? Maybe ||. If it is what you want, you want to be using && not &

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • The while loops are there to display a grid. Thanks for your post, looking into it now – The unknown Apr 20 '14 at 01:01
  • If you want to display a grid see [this answer](http://stackoverflow.com/a/22728604/2587435). It uses for loops instead of a bunch of while loops. – Paul Samsotha Apr 20 '14 at 01:09