Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: can someone show me how to change speed of an java engine.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default can someone show me how to change speed of an java engine.

    I'm writing a program about pasture with entities on it.Here is the engine:
    import java.util.*;
    import javax.swing.Timer;
    import java.awt.event.*;
     
    /**
     * The simulation is run by an internal timer that sends out a 'tick'
     * with a given interval. One tick from the timer means that each
     * entity in the pasture should obtain a tick. When an entity obtains
     * a tick, this entity is allowed to carry out their tasks according
     * to what kind they are. This could mean moving the entity, making
     * the entity starve from hunger, or producing a new offspring.
     */
     
    public class Engine implements ActionListener {
     
        private final int  SPEED_REFERENCE = 1000; /* 1000 */
        private static int speed   = 500;
        private Timer      timer   = new Timer(SPEED_REFERENCE/speed,this);
        private int        time    = 0;
     
        private Pasture pasture; 
        Engine (Pasture pasture) {
            this.pasture = pasture;
            this.speed = speed;
        }
        public void increaseSpeed(){speed += 20;}
    	public void decreaseSpeed(){
    	    if(speed > 20)
    		    speed -= 20;
    		else
    		    return;
    	}
        public void actionPerformed(ActionEvent event) {
     
            List<Entity> queue = pasture.getEntities();
            for (Entity e : queue) {		
    			e.tick();		
            }
            pasture.refresh();
            //System.out.println("\n(action in engine)size now is : "+pasture.getWorld().size());
            //System.out.println("\n      END ONE TICK().");		
            time++;
        }
     
     
        public void setSpeed(int speed) {
            timer.setDelay(SPEED_REFERENCE/speed);
        }
     
        public void start() {
            setSpeed(speed);
            timer.start();
        }
     
     
        public void stop() {
            timer.stop();
        }
     
        public int getTime () {
            return time;
        }
     
    }
    Anyone know how to change speed of the simulation while it is running ?
    I appreciate your help very much !!!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    You already do change the speed of the Timer, in the setSpeed() method. What's your question?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Load Microsoft Word on your machine :-)

    Seriously, though, you need to read up on multithreading.

    You need to have a controlling application and then launch your simulation on a separate thread, so that the controller and simulator are running in parallel.

    Essentially you need an object of type Runnable passed to a Thread constructor, and then to call Thread.start();

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    Load Microsoft Word on your machine :-)

    Seriously, though, you need to read up on multithreading.

    You need to have a controlling application and then launch your simulation on a separate thread, so that the controller and simulator are running in parallel.

    Essentially you need an object of type Runnable passed to a Thread constructor, and then to call Thread.start();
    I absolutely disagree with this advice. Using a Swing Timer is the way to go here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    codenoob (December 13th, 2011)

  6. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Thanks a lot ,Kevin. Your advices is really helpful (especially the debug link ).My teacher told us that the lecture was about OOP not java.Things like threads,GUI... is only taught very little and now we have to do this.Btw,do you know how to create a browser in a java program.I mean the program will load files in window (like loading maps in warcraft) not to open using buffer reader or something like that. I finished the homework and just wonder if that is possible in java.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: can someone show me how to change speed of an java engine.

    Do you mean like the JFileChooser?

  8. #7
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by KevinWorkman View Post
    I absolutely disagree with this advice. Using a Swing Timer is the way to go here.
    Swing Timer is using the same principal behind the scenes. However,...

    ...although I can understand that you advise against reinventing the wheel, I think you have assumed that the speed changes come from the user only, through a GUI. If the timer changes are coming from any asynchronous source, including automatic ones, I don't think Swing Timer gives you much advantage.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    Swing Timer is using the same principal behind the scenes. However,...

    ...although I can understand that you advise against reinventing the wheel, I think you have assumed that the speed changes come from the user only, through a GUI. If the timer changes are coming from any asynchronous source, including automatic ones, I don't think Swing Timer gives you much advantage.
    The advantage of the Swing Timer is that you don't have to worry about things like the EDT, and actions such as pausing between steps are done at a higher level. I don't know what you mean about timer changes coming from any asynchronous source, but I really don't see it being a problem.

    For stuff like this, Timer is the way to go.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by KevinWorkman View Post
    The advantage of the Swing Timer is that you don't have to worry about things like the EDT, and actions such as pausing between steps are done at a higher level. I don't know what you mean about timer changes coming from any asynchronous source, but I really don't see it being a problem.

    For stuff like this, Timer is the way to go.
    Of course you need a Timer. The OP is using Timer. That is not the issue. The issue is where the Timer sits -- in which thread?

    The Timer MUST sit in a separate thread. Using a Swing GUI solves this only because the Swing GUI runs in its own thread. But this is assuming that the user is sitting there dictating when the timer speeds up and slows down.

    What if the controlling information is on a set of 100 test files that are supposed to be read automatically, one after the other, and each contains information like -- start, run at speed A for twenty minutes, slow down to speed B for 30 minute, etc. then stop? You'd want this to run in batch mode. Nothing to do with a GUI.

    So rather than harnessing the threading capabilities of a Swing GUI, you have to spawn the thread yourself.

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    Of course you need a Timer. The OP is using Timer. That is not the issue. The issue is where the Timer sits -- in which thread?

    The Timer MUST sit in a separate thread. Using a Swing GUI solves this only because the Swing GUI runs in its own thread. But this is assuming that the user is sitting there dictating when the timer speeds up and slows down.

    What if the controlling information is on a set of 100 test files that are supposed to be read automatically, one after the other, and each contains information like -- start, run at speed A for twenty minutes, slow down to speed B for 30 minute, etc. then stop? You'd want this to run in batch mode. Nothing to do with a GUI.

    So rather than harnessing the threading capabilities of a Swing GUI, you have to spawn the thread yourself.
    Umm okay, but that's not this case. I said a Timer was the way to go here, for stuff like this, in this case. Threading has its own merits, but not here. And note that a Swing Timer does not have to run in a separate Thread- in fact, that wouldn't really change anything. It signals its ActionListener on the EDT. End of story. I'm not really sure what your point is here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by KevinWorkman View Post
    Umm okay, but that's not this case. I said a Timer was the way to go here, for stuff like this, in this case.
    The OP didn't specify the case. You assumed it. Your assumption may, of course be right. I didn't assume.

    Quote Originally Posted by KevinWorkman View Post
    Threading has its own merits, but not here. And note that a Swing Timer does not have to run in a separate Thread-
    This is wrong. It does automatically run in it's own thread. That is one of its advantages. Obviously the animation and the timer shouldn't run in the same thread!!! You don't want them synchronous, lol!!!

    But I think you are misunderstanding the problem. You have an animation, a timer and a controller (a process that is controlling the animation). The timer would normally be synchronous with the controller and should be asynchronous with the animation. So the controller would send an instruction to the animation, set the timer and wait for it to return before setting it for the next instruction. The animation, of course shouldn't wait (freeze) for the timer to return!!!

    Quote Originally Posted by KevinWorkman View Post
    in fact, that wouldn't really change anything. It signals its ActionListener on the EDT. End of story. I'm not really sure what your point is here.
    I hope you can see now?

  13. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    The OP didn't specify the case. You assumed it. Your assumption may, of course be right. I didn't assume.
    Actually, he specified the case in the very first post. Also, if you look at the other posts he has made, you'll see exactly what he's trying to accomplish. I did the research. That's not making an assumption.

    Quote Originally Posted by 2by4 View Post
    This is wrong. It does automatically run in it's own thread. That is one of its advantages. Obviously the animation and the timer shouldn't run in the same thread!!! You don't want them synchronous, lol!!!
    Wrong. I'll repeat- a Swing Timer's ActionListener is fired on the EDT. That is not its own thread. In fact, that IS the same thread on which the animation, all painting, all events, and all GUI work is done. That's how it's supposed to work. I really suggest you do some reading on the EDT if you're going to make incorrect suggestions based on your misunderstanding.

    Quote Originally Posted by 2by4 View Post
    But I think you are misunderstanding the problem. You have an animation, a timer and a controller (a process that is controlling the animation). The timer would normally be synchronous with the controller and should be asynchronous with the animation. So the controller would send an instruction to the animation, set the timer and wait for it to return before setting it for the next instruction. The animation, of course shouldn't wait (freeze) for the timer to return!!!
    Wrong. For things like this, using a swing Timer and doing almost everything on the EDT is fine.

    Quote Originally Posted by 2by4 View Post
    I hope you can see now?
    I hope YOU can see now. Please do research before arguing. Write a simple program that includes a Swing Timer that controls an animation on a GUI. Print out the name of the current thread in the Timer, in paintComponent, and in an event. I guarantee you they're all on the EDT. Timers are not "on their own thread automatically". And using a Timer is fine for this kind of thing.

    You're introducing incorrect advice, which will only serve to confuse the OP and any other visitors in the future. Please stop. Consider this a friendly warning. If you have more questions about how the EDT works, I suggest you create the program I just described and post any questions in your own thread.
    Last edited by KevinWorkman; December 14th, 2011 at 09:21 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #13
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by KevinWorkman View Post
    Actually, he specified the case in the very first post.
    No. Go and read it again. Nowhere does he say that a USER will change the speed. He just asks how the speed can be changed. You have ASSUMED that it is a USER and not another program.



    Wrong. I'll repeat- a Swing Timer's ActionListener is fired on the EDT. That is not its own thread. In fact, that IS the same thread on which the animation, all painting, all events, and all GUI work is done. That's how it's supposed to work. I really suggest you do some reading on the EDT if you're going to make incorrect suggestions based on your misunderstanding.
    Tell me, while the animation is running on EDT, what is the Timer doing?

    And tell me, while the animation is keeping the EDT busy, do you think the "Start Button", the "Speed Up" button, the "Slow Down" button, the "Stop Button" are going to be responsive?

    Take a look at this..

    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    In general, we recommend using Swing timers rather than general-purpose timers for GUI-related tasks because Swing timers all share the same, pre-existing timer thread and the GUI-related task automatically executes on the event-dispatch thread.
    Does that sound to you like the same thread?

    And this..

    Let's look at an example of using a timer to periodically update a component. The TumbleItem applet uses a timer to update its display at regular intervals. (To see this applet running, go to How to Make Applets. This applet begins by creating and starting a timer:

    timer = new Timer(speed, this);
    timer.setInitialDelay(pause);
    timer.start();

    The speed and pause variables represent applet parameters; as configured on the other page, these are 100 and 1900 respectively, so that the first timer event will occur in approximately 1.9 seconds, and recur every 0.1 seconds. By specifying this as the second argument to the Timer constructor, TumbleItem specifies that it is the action listener for timer events.

    After starting the timer, TumbleItem begins loading a series of images in a background thread.
    Now go and think carefully why the animation images are loaded in a background thread.

  15. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    No. Go and read it again. Nowhere does he say that a USER will change the speed. He just asks how the speed can be changed. You have ASSUMED that it is a USER and not another program.
    That doesn't matter. What do you see as the important difference between a user changing the speed and the program changing the speed? There is no reason to use extra threads in either case.

    Quote Originally Posted by 2by4 View Post
    Tell me, while the animation is running on EDT, what is the Timer doing?
    What's your point? I said that Timers trigger their ActionListener on the EDT. The underlying implementation of a Timer is irrelevant to this conversation. You telling the OP that he has to use threading is not helpful.


    Quote Originally Posted by 2by4 View Post
    And tell me, while the animation is keeping the EDT busy, do you think the "Start Button", the "Speed Up" button, the "Slow Down" button, the "Stop Button" are going to be responsive?
    You seem to be under the impression that these animations are going to take forever. Simply changing a position or an image and calling repaint() are't going to take long at all. For longer tasks, such as file IO, you shouldn't use the EDT- but for things like this problem, using Timers is fine.

    Quote Originally Posted by 2by4 View Post
    Yes! The EDT is a single thread, and the timer "runs" on that thread by triggering its ActionListener on it. What the Timer does in between firings is of no concern to the OP. You seem to be insisting that the OP needs to implement multiple threads for this problem, and he simply doesn't! A Timer will work just fine.

    Quote Originally Posted by 2by4 View Post
    And this..
    Now go and think carefully why the animation images are loaded in a background thread.
    Sigh. That's fine. That's not this case. I said that threading has a place, and loading things in background threads is one of those places. However, for this problem, in this discussion, threads are not necessary and only introduce unneeded complications. Using a Timer is fine, and only the EDT will be used (unless you want to be pedantic, in which case many threads are working under the hood, but that absolutely is not relevant to this discussion).

    Like I said, if you want to continue this conversation, start your own thread.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #15
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by KevinWorkman View Post
    You seem to be under the impression that these animations are going to take forever. Simply changing a position or an image and calling repaint() are't going to take long at all. For longer tasks, such as file IO, you shouldn't use the EDT- but for things like this problem, using Timers is fine.
    You really don't understand do you? He is trying to change the speed of the animation WHILE IT IS RUNNING. Not wait for it to finish and then press the "Speed Up" button!!!

    LOL, look, when you've understood the problem and read through the tutorial, we can it up from there.

  17. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    You really don't understand do you? He is trying to change the speed of the animation WHILE IT IS RUNNING. Not wait for it to finish and then press the "Speed Up" button!!!

    LOL, look, when you've understood the problem and read through the tutorial, we can it up from there.
    Wrong. He's trying to change the speed of the animation WHILE THE SIMULATION IS RUNNING, as opposed to having to stop the simulation, change a value, recompile, and restart the simulation. That can be done by a user, from a GUI component's listener (on the EDT), or by the program, from the timer's ActionListener itself (also on the EDT). No user-defined threads are necessary. Like I said, these animations are not going to be taking very long, so "waiting for one to finish" will be invisible to the user. Please stop introducing unnecessary complications to the problem, and please stop hijacking the OP's thread. I've asked you nicely a few times now.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #17
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by KevinWorkman View Post
    Wrong. He's trying to change the speed of the animation WHILE THE SIMULATION IS RUNNING, as opposed to having to stop the simulation, change a value, recompile, and restart the simulation. That can be done by a user, from a GUI component's listener (on the EDT), or by the program, from the timer's ActionListener itself (also on the EDT). No user-defined threads are necessary. Like I said, these animations are not going to be taking very long, so "waiting for one to finish" will be invisible to the user. Please stop introducing unnecessary complications to the problem, and please stop hijacking the OP's thread. I've asked you nicely a few times now.
    As I said WHILE THE ANIMATION IS RUNNING. I don't know why you have repeated it?

    If the action listener for the speed up button is in the same thread, it won't respond WHILE THE ANIMATION IS RUNNING. That is why the tutorial says that the image loading happens in a background thread.

    I don't know what you don't get. But I can see you are heading for moderator mode and trying to claim I am off topic. Don't worry, I will make my point. Do what you want.
    Last edited by 2by4; December 14th, 2011 at 10:26 AM. Reason: typos

  19. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Here is a program that adjusts the speed of an animation while the simulation is running. It also prints out the name of the current thread during painting, in the timer, and in the gui code.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.Timer;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
     
    public class TimerTest extends JPanel {
     
     
    	int ballX = 250;
    	boolean goingRight = true;
     
    	Timer timer;
     
    	public TimerTest(){
     
     
    		timer = new Timer(50, new ActionListener()
    		{
    			public void actionPerformed(ActionEvent event)
    			{
     
    				System.out.println("Timer, thread: " + Thread.currentThread().getName());
     
    				if(goingRight){
    					ballX++;
    				}
    				else{
    					ballX--;
    				}
     
    				if(ballX >= getWidth()){
    					goingRight = false;
    					timer.setDelay((int) (Math.random() * 100));
    				}
    				else if(ballX <= 0){
    					goingRight = true;
    					timer.setDelay((int) (Math.random() * 100));
    				}
     
    				repaint();
    			}
    		});
    		timer.setRepeats(true);
     
    		timer.start();
     
    		final JSlider timerSlider = new JSlider();
    		timerSlider.setMinimum(0);
    		timerSlider.setMaximum(100);
    		timerSlider.addChangeListener(new ChangeListener(){
     
    			@Override
    			public void stateChanged(ChangeEvent e) {
    				System.out.println("GUI, thread: " + Thread.currentThread().getName());
    				timer.setDelay(timerSlider.getValue());
    			}
    		});
     
    		JFrame frame = new JFrame("Timer Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		frame.add(this);
    		frame.add(timerSlider, BorderLayout.SOUTH);
    		frame.setSize(500, 500);
    		frame.setVisible(true);
     
    	}
     
    	public static void main(String... args){
    		new TimerTest();
    	}
     
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
     
    		System.out.println("Painting, thread: " + Thread.currentThread().getName());
     
    		g.setColor(Color.RED);
    		g.fillOval(ballX, getHeight()/2, 50, 50);
    	}
     
    }


    The program prints out:

    Timer, thread: AWT-EventQueue-0
    Painting, thread: AWT-EventQueue-0
    GUI, thread: AWT-EventQueue-0

    ...which shows that only a single thread is used, the EDT. The animation sets its own speed every time the ball hits a side, or the user can specify it by adjusting the slider.

    If you still have questions, post them in your own thread.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #19
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: can someone show me how to change speed of an java engine.

    OK, I won't say much more. Note that timer running in its own thread is not the same as its event listener running in its own thread. So that was a red herring from you.

    The difference here, is that the controller (slider) is in the same thread as the animation. I am sure you know that such an implementation can freeze or lose critical responsiveness especially where speed is required, for precisely the reasons in my earlier posts. May work for a small ball on the screen. No good for a scientific application with substantial data.

    Peace. I say no more on the subject :-)

  21. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: can someone show me how to change speed of an java engine.

    Quote Originally Posted by 2by4 View Post
    OK, I won't say much more. Note that timer running in its own thread is not the same as its event listener running in its own thread. So that was a red herring from you.
    Like I said, the underlying implementation details of a Timer don't really matter in this context. It was not a red herring, because it was my counter-point to your insistence that threads were necessary here. They aren't, since a timer "runs" in the EDT by firing an event on the EDT periodically. You seemed to think this would tie up the EDT and make the GUI unresponsive, which I've shown is not the case here.

    Quote Originally Posted by 2by4 View Post
    The difference here, is that the controller (slider) is in the same thread as the animation. I am sure you know that such an implementation can freeze or lose critical responsiveness especially where speed is required, for precisely the reasons in my earlier posts. May work for a small ball on the screen. No good for a scientific application with substantial data.
    That's fine, and I conceded that point outright. But we aren't talking about a scientific application with substantial data here; we're talking about a simple animation. It's true that long processes should never be done on the EDT. But simple animations and whatnot are absolutely fine on the EDT. A Timer works perfectly in this context. Threads are more trouble than they're worth in this instance. Telling the OP to use threads instead of a Timer was bad advice. That's okay, I've been known to give bad advice from time to time too.

    Quote Originally Posted by 2by4 View Post
    Peace. I say no more on the subject :-)
    Sounds reasonable to me. OP, sorry we hijacked your thread with our bickering. But hopefully you can use my example program to figure out what you could be doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to speed up the performance of the java application
    By Sathiyan in forum Java Theory & Questions
    Replies: 4
    Last Post: December 2nd, 2011, 02:09 AM
  2. Trying to show change on Image based on pixel values
    By javaGurl in forum Algorithms & Recursion
    Replies: 1
    Last Post: September 20th, 2011, 04:49 PM
  3. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 03:43 PM
  4. Need...more...speed!
    By nPeep in forum Java Networking
    Replies: 8
    Last Post: September 6th, 2010, 12:07 PM
  5. Has anybody used Java Search Engine?
    By jayab in forum Member Introductions
    Replies: 2
    Last Post: April 26th, 2010, 05:23 AM