Animation paint problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 9
Reputation: ChaosTheory is an unknown quantity at this point 
Solved Threads: 0
ChaosTheory ChaosTheory is offline Offline
Newbie Poster

Animation paint problem

 
0
  #1
Feb 26th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Animation paint problem

 
0
  #2
Feb 26th, 2009
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.
  1. while (running) {
  2. updateCars();
  3. repaint();
  4. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 9
Reputation: ChaosTheory is an unknown quantity at this point 
Solved Threads: 0
ChaosTheory ChaosTheory is offline Offline
Newbie Poster

Re: Animation paint problem

 
0
  #3
Mar 1st, 2009
  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. }
  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Animation paint problem

 
0
  #4
Mar 2nd, 2009
Inside Timer3 you can change all parameters of a_timer, vehicle.
Attached Files
File Type: zip traffic.zip (8.1 KB, 2 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC