943,612 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 984
  • Java RSS
Feb 26th, 2009
0

Animation paint problem

Expand Post »
hi,

I have an application that paints a rectangle(car) and moves it across the screen using a timer when a start button is clicked.

The problem i have is i want to generate(paint) cars randomly at specific intervals (using a timer) when the cars disappear from the screen/street.

Now i cant use a paint method inside the action event (action listener of timer) and vice-versa i cant put the action event inside the paint.

So i have a vehicle class that produces a car like new rectcar(2,5,7)
I want to know where to put the code that will generate the cars randomly any ideas?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChaosTheory is offline Offline
9 posts
since Feb 2009
Feb 26th, 2009
0

Re: Animation paint problem

You didn't post any code, so I can't speak directly to your current structure, but you should be able to do all that in your current animation loop. If your paint method were to operate on a List of car objects, your animation loop can simply update/create/remove car objects in that list and call repaint.
Java Syntax (Toggle Plain Text)
  1. while (running) {
  2. updateCars();
  3. repaint();
  4. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Mar 1st, 2009
0

Re: Animation paint problem

Java Syntax (Toggle Plain Text)
  1. package traffic;
  2.  
  3.  
  4. import java.awt.Color;
  5. import java.awt.Graphics2D;
  6.  
  7.  
  8. public class vehicle {
  9.  
  10. private static final int size = 20;
  11. private Color vehicleColor;
  12. private int Xvehicle, Yvehicle, speedVehicle;
  13.  
  14. public vehicle(int x, int y, int velocity) {
  15.  
  16. vehicleColor = Color.BLUE;
  17. Xvehicle = x;
  18. Yvehicle = y;
  19. speedVehicle = velocity;
  20.  
  21.  
  22. }
  23.  
  24. public void drive() {
  25.  
  26.  
  27. Xvehicle += speedVehicle;
  28.  
  29. }
  30.  
  31.  
  32. public void draw(Graphics2D g2) {
  33.  
  34. g2.setColor(vehicleColor);
  35. g2.fillRect(Xvehicle, Yvehicle, size, size);
  36.  
  37. }
  38.  
  39.  
  40. }
Java Syntax (Toggle Plain Text)
  1. package traffic;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import javax.swing.Timer;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9.  
  10.  
  11. public class Anime extends junction {
  12.  
  13.  
  14. private vehicle vec_1 = new vehicle(80,365, 2);
  15.  
  16. private int a_interval =43;
  17. private int b_interval = 7000;
  18. private Timer a_timer, b_timer;
  19.  
  20.  
  21. public Anime() {
  22.  
  23. a_timer = new Timer(a_interval, new Timer1());
  24.  
  25. b_timer = new Timer(b_interval, new Timer2());
  26. }
  27.  
  28. public void setAnimation(boolean StartStop) {
  29. if (StartStop) {
  30. a_timer.start();
  31. b_timer.start();
  32. } else {
  33.  
  34. a_timer.stop();
  35. b_timer.stop();
  36. }
  37. }
  38.  
  39. public void paintComponent(Graphics g) {
  40. super.paintComponent(g);
  41. Graphics2D g3 = (Graphics2D) g;
  42. vec_1.draw(g3);
  43.  
  44.  
  45.  
  46. }
  47.  
  48.  
  49.  
  50. class Timer1 implements ActionListener {
  51.  
  52. public void actionPerformed(ActionEvent e) {
  53.  
  54. vec_1.drive();
  55. repaint();
  56. }
  57. }
  58.  
  59.  
  60. class Timer2 implements ActionListener {
  61.  
  62. public void actionPerformed(ActionEvent e) {
  63. // g = (Graphics2D)this.getGraphics();
  64. //create new vehicle randomly...
  65. }
  66. }
  67. }

junction class is a Jcomponent that paints the street/background.
How would i be able to generate new cars in the inner class of Timer2?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChaosTheory is offline Offline
9 posts
since Feb 2009
Mar 2nd, 2009
0

Re: Animation paint problem

Inside Timer3 you can change all parameters of a_timer, vehicle.
Attached Files
File Type: zip traffic.zip (8.1 KB, 28 views)
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help me create this MP3plaler Playlist
Next Thread in Java Forum Timeline: GUI Inventory Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC