I am new to Swing components. I am trying a scenario where in using a loop, I have an object(ball), which moves from one end to another end (source -- destination). Accordingiy, I want to keep a slider to view/control the movement of ball(just like a progress bar). Below is my code. The ball is moving well and there is no movement in the slider. I have used slider.setValue() method but is not working for me. Am I missing any thing? Any help would be highly appreciated.
Thanks Maneesh. Your code is working. I tried to implement the same in my code. After further R&D, I found out that JFrame is a top level container and cannot be added like JPanel.
If I extend the class with JPanel, the code is not working(slider movement) but with JFrame, the code is working.
I have come to a conclusion that if at all we are doing some GUI, then its better to add all the JPanel's in a single JFrame class. Please let me know whether I have perceived correctly or not and whether it is a good practice.
The basic problem with your original code was the repaint call. You forgot that call repaints all the children!
In general, try and adhere to the following pattern.
1) One base container (typically JFrame)
2) JFrame has a content pane (again a container) to which you should be adding all the child components
3) Try to use simple components instead of complex components when you are playing with the graphics.
You are on the right path. With a little bit of more practice you should get the hang of it.
Thanks a lot Maneesh for your help. One last query. I have created a button and implemented ActionListener for this button, so that when I click on this button, the ball should start moving(slider anyways is moving in sync with ball). Some how I am not getting the desired result. I have checked in debug mode, and surprisingly paintComponent() is not getting called even after the execution of repaint() method. I did try with this.repaint(), revalidate() and this.revalidate() methods. But is not working for me. Any clue to get the desired result would be of great help.
Yes Maneesh. I am using the below code. TestPanel.java remain the same (the one that you gave earlier).
K Kiran Kumar
posted 9 years ago
After much struggle and R&D I found out a workaround for my issue. If I use Threads, then I will get the required output. this is because When an event is fired, EDT(EventDrivenThread) will be executing(taken from MainThread) and during this time, there won't be any updation on the screen. Once this thread ends, then paintGraphics() method is called and we can only view the starting and ending frame of our graphics. The changes that I made in my code are given below.
However in most of the articles, it is recommended to use Timer than Threads.
Well, since this week this is also a JavaFX forum, I thought I would show all of you the sample Swing code implemented in JavaFX. I did add an "easeout" interpolator that slows down the movement at the end. If you remove the tween Interpolator.EASEOUT, then you'll get normal linear interpolation. I resisted the urge to add drop shadows to the circle, linear gradients to the background, etc.
The code does show the scene graph metaphor well. You can see that you "set things up" by defining your nodes (circles, rectangles, slider) and then it just goes! No painting, no threading, no runnables.
posted 9 years ago
And, perhaps more elegant, but not exactly the same behavior is using this slider and completely removing the second timeline (the 100ms update timeline).
With a second slider you could reflect/control movement of the ball in the y direction.