• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need some help with an assignment  RSS feed

 
Craig Warnick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help with an assignment, I need to animate a race car so it goes across the screen and comes back when you click on the move button. Yes it is an assignment but I have now reached the frustration phase. I have tried a number of things based on my textbook but no joy. I will post some below.

Here is main and other info


package carraceproject;

import java.awt.BorderLayout;
import javax.swing.JFrame;

public class CarRaceProject {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame jf = new JFrame("The Great Race Adventure");
CarPanel car = new CarPanel();
ControlClass cc = new ControlClass();
cc.setRefToPos(car);
car.setRefCC(cc);
jf.setSize(800,300);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.add(car, BorderLayout.CENTER);
jf.add(cc,BorderLayout.SOUTH);
jf.setVisible(true);

}





 
Craig Warnick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im just looking for a tip on where the timer gets implemented I have tried an inner class. Sorry for the length of my previous post.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
timer.start() has to be here - "when you click on the move button."
the timer can be inside the button's actionListener, or outside, but the button needs to 'see' it to start it.

the timer code will relocate the car and call the car's container (CarPanel?) - container.repaint();

If I were you, I'd forget the current code for the moment, create another .java file, just a JFrame
with a panel and button. In the panel's paintComponent, just draw a small circle or square (20x20),
then add the timer code to move the circle/square back and forth. When you get it right, then incorporate
the code into the car class/s, but if you have a problem, you'll be able to post a very small program here.
 
Craig Warnick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Michael, I got it working. Thank you for your assistance and have a great night.
 
Craig Warnick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edited my post since it was so long. Thanks again for the help, I really like this forum, full of info and treat us greenhorns civily.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!