1

My approach was to use 'reprint()' inside a loop was successful to move an image that means updating its position without overlapping but now I want to see the image moving and I used 'thread.sleep()' to give time gaps between the repaint()s but it doesn't seem to work

import java.awt.*;
import javax.swing.*;

public class jp extends JPanel implements ActionListener{
Timer t;
JPanel jl=new JPanel();
int x,y;
jp(){
    x=10;
    //y=10;
    t=new Timer(5,this);
    t.start();
}
public void actionPerformed(ActionEvent e){
    x++;
    y++;
    if(x>500){
        x=0;
        y=0;
    }
    repaint();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.black);
    g.setColor(Color.blue);
    g.fillRect(x,20,50,50);
}

}

public class Jpanel  extends JFrame{


public static void main(String[] args) {
    jp p=new jp();

    JFrame j=new JFrame("TEST_CASE-1");
    j.add(p);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setSize(700,500);
    j.setVisible(true);
  }

}
ryhn
  • 15
  • 5
  • 1
    1) `for(int i=0;i<200;i++){ g.fillRect(25+i,25,50,50); }` This is not the way to go about animation. 2) *"I tried using repaint() but it doesn't seem to work"* repaint is called nowhere in the code seen above. 3) Tying both those comments together, use a Swing based `Timer` to adjust the value of `i` then call repaint. – Andrew Thompson Aug 12 '17 at 19:04
  • It worked. But how can I see the image moving? I tried using 'Thread.sleep' but it doesn't seem to work. I used 'repaint()' inside of a loop instead of using timer – ryhn Aug 13 '17 at 07:15
  • 1) Show an [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) of the current attempt as an [edit] to the question. 2) *"tried using 'Thread.sleep' but it doesn't seem to work"* It isn't needed for this approach (and will more likely mess things up). – Andrew Thompson Aug 13 '17 at 07:30
  • Done. Please check now. – ryhn Aug 13 '17 at 10:58
  • Where is there any attempt to use a Swing `Timer`? – Andrew Thompson Aug 13 '17 at 12:10
  • Actually I have never used swing "Timer" before so I was trying to avoid it. And I tried using timer seeing other example but couldn't understand totally. – ryhn Aug 14 '17 at 08:50
  • *"Actually I have never used swing "Timer" before so I was trying to avoid it."* If you're not willing to try new things (which in this case are doing things the *right* way) then this problem will remain unsolved. Let me know when you're serious about solving the problem, rather than trying to 'correct' a fundamentally broken approach. Otherwise, I'm done here. – Andrew Thompson Aug 14 '17 at 10:35
  • 1
    You win, I was wrong using "Thread.sleep()". It just worked fine after using "Swing Timer'. Thanks for the push. – ryhn Aug 14 '17 at 17:25
  • *"You win, .. It just worked fine.."* We **both** win. Glad you got it working. :) – Andrew Thompson Aug 14 '17 at 17:33

1 Answers1

0

If you are hoping to get an animation by having multiple drawings in paintComponent() such as using a for-loop like what you did, you are going to be disappointed.

It will only perform all the drawings and show it to you all at once. Instead of concurrently drawing 200 rectangles, you only need to draw 1 rectangle and update its position.

To perform an animation, you need either a timer (You may try javax.swing.timer) or a loop if you are planning to do something more sophisticated. You can implement an infinite loop and perform the rendering in the loop while updating the position of your rectangle.

Ultimately, no matter which approach you adopt. You will need store the position of your rectangle. Update and render it in such as way:

update position
render
update position
render
update position
render

and not

update position
update position
update position
render
render
render

Example of animation with timer: Repainting/refreshing the JFrame

Example animating using loop:

while(running){
    update();     //update position of your rect
    repaint();    //redraw your rect (and all other stuff)
}
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • It worked.But how can I see the image moving?I tried using "Thread.sleep()" but it doesn't seem to work. I used 'repaint()' inside a loop instead of using a timer. – ryhn Aug 13 '17 at 07:16
  • You use Thread.sleep so your thread can give a chance for other threads to run. Setting a longer sleep time creates a slower animation. If it is animating too quickly, you can try something like thread.sleep(10). Certainly, 10 can be replaced with a variable to control the frame rate. – user3437460 Aug 13 '17 at 13:53
  • @ryhn If this solution solves your problem, you may accept the solution by clicking on the hollow looking tick. You get+2 rep in return. – user3437460 Aug 13 '17 at 20:32
  • "Thread.sleep()" is working but the "reapint()" method isn't working. So its just incrementing 'i' and I am just getting the final position instead of an animation. Could you tell me how can I make the "repaint()" method work or where I am doing it wrong? – ryhn Aug 14 '17 at 08:54