This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
To start off I want to create a program that animates shapes using buttons. The
user selects a shape and the shape is outputted onto the screen. Now if the user
wants to animate the shape he will press the 'Animate shape' button. However I dont
know how to link the shape the user just selected with the animate button.
1. You are mixing AWT and Swing. I suggest you learn how to use Swing
2. Extend JApplet, not Applet 3. Custom painting is done by creating a JPanel and overriding its paintComponent() method. The first line should be super.paintComponent() to clear the panel so you don't leave the old drawing behind as you animate the shape. Then the panel is added to the applet
4. The Timer should be started when you click the button, not in the paintComponent() method.
5. Your have not set up your Timer correctly. The Timer code should be responsible for setting the new "X" coordinate to paint your shape, not the painting code.
6. You case logic is wrong. You already select which shape to paint in the case code. No need for the second "if statement".
7. You should not have repaint() logic in the painting code.
Joined: Mar 20, 2013
Thankyou Rob. I revised my code and removed the unnecessary paint codes.