| | |
Animation paint problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
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?
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?
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)
while (running) { updateCars(); repaint(); }
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
package traffic; import java.awt.Color; import java.awt.Graphics2D; public class vehicle { private static final int size = 20; private Color vehicleColor; private int Xvehicle, Yvehicle, speedVehicle; public vehicle(int x, int y, int velocity) { vehicleColor = Color.BLUE; Xvehicle = x; Yvehicle = y; speedVehicle = velocity; } public void drive() { Xvehicle += speedVehicle; } public void draw(Graphics2D g2) { g2.setColor(vehicleColor); g2.fillRect(Xvehicle, Yvehicle, size, size); } }
Java Syntax (Toggle Plain Text)
package traffic; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Anime extends junction { private vehicle vec_1 = new vehicle(80,365, 2); private int a_interval =43; private int b_interval = 7000; private Timer a_timer, b_timer; public Anime() { a_timer = new Timer(a_interval, new Timer1()); b_timer = new Timer(b_interval, new Timer2()); } public void setAnimation(boolean StartStop) { if (StartStop) { a_timer.start(); b_timer.start(); } else { a_timer.stop(); b_timer.stop(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g3 = (Graphics2D) g; vec_1.draw(g3); } class Timer1 implements ActionListener { public void actionPerformed(ActionEvent e) { vec_1.drive(); repaint(); } } class Timer2 implements ActionListener { public void actionPerformed(ActionEvent e) { // g = (Graphics2D)this.getGraphics(); //create new vehicle randomly... } } }
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?
![]() |
Similar Threads
- Problem with Pong (Java)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Animation...Some Images Skipped... (Java)
- Getting the little red x instead of pictures. (Web Browsers)
- Button Question/Issue (Java)
- Reduce animation flickering in app? (Java)
- Image Array Issue..... Any ideas??? (Java)
- Graphics In Pixel: Part II (C++)
Other Threads in the Java Forum
- Previous Thread: Help me create this MP3plaler Playlist
- Next Thread: GUI Inventory Program
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






