How to pause & resume Java applet only when user clicks a button?
Hello,
I am developing animation program using Java applets. I have used Thread.start() to start the applet. In this case, when thread is started, whole program will execute and then it will stop. This is working as required.
Now what I am trying to do is: thread will advance only when user clicks a Button called "step in". I want to do this to allow user to do a step by step execution. I see that resume() and suspend() methods are deprecated now.
Can anyone guide me how can I achieve this or point me to some reference?
Thank you.
Re: How to pause & resume Java applet only when user clicks a button?
Another way would be to have the code to be paused be written so it stops execution when told to.
You might be able to do this with wait() and notify()
Re: How to pause & resume Java applet only when user clicks a button?
Thank you for your reply.
I tried to understand how to use wait() and notify(), but I am not sure how to do it.
I have a Start button, when user clicks Start, thread is created and started. In run() method I have a do while loop where the whole applet code is running. Code looks like:
private class ExeAlgorithm extends Thread{
public ExeAlgorithm(){
}
@Override
public void run(){
do
{
clk++;
//other lines of coding.................
//.......................
}while(queue2.size() < totalsize);
}
So when loop executes once, applet should wait() and when user clicks again on start, applet should notify() and resume execution.
If Start is clicked again then it will throw error as thread can be started only once.
Can you pls guide how to fit wait() and notify() here?
Thank you.
Re: How to pause & resume Java applet only when user clicks a button?
Have you read the API doc the the wait() and notify() methods in the Object class?
Also search the forum for sample code that uses those methods.
Quote:
throw error as thread can be started only once.
A thread can't be used twice. You will have to create a new thread if the current one ends.
I'm done for tonight. Back tomorrow.
Re: How to pause & resume Java applet only when user clicks a button?
For animation in an applet, I would recommend using JApplet, all Swing (not AWT) components, and a Swing Timer -- which has start/stop/restart methods, and does its waiting on a background thread that you as the application programmer do not have to create or manage.
Check out the tutorials linked from the relevant APIs.
db
Re: How to pause & resume Java applet only when user clicks a button?
try my java pause button:
Code:
package drawFramePackage;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Milliseconds2 implements ActionListener, MouseListener{
JFrame j;
Timer t;
Integer onesAndZeros, time, time2, placeHolder2;
Boolean hasFired;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Milliseconds2();
}
public Milliseconds2(){
j = new JFrame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(new Dimension(300, 300));
j.setVisible(true);
j.addMouseListener(this);
onesAndZeros = new Integer(0);
time = new Integer(0);
time2 = new Integer(0);
placeHolder2 = new Integer(0);
hasFired = new Boolean(true);
t = new Timer(2400, this);
time = (int) System.currentTimeMillis();
t.start();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (onesAndZeros.equals(0)){
t.stop();
if (hasFired){
time2 = t.getDelay() - ((int) System.currentTimeMillis() - time);
}
else{
time2 -= (int) System.currentTimeMillis() - placeHolder2;
}
if (hasFired){
hasFired = false;
}
onesAndZeros = -1;
}
if (onesAndZeros.equals(1)){
//System.out.println(time2);
t.setInitialDelay(time2);
t.start();
placeHolder2 = (int) System.currentTimeMillis();
onesAndZeros = 0;
}
if (onesAndZeros.equals(-1)){
onesAndZeros = 1;
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
time = (int) System.currentTimeMillis();
hasFired = true;
System.out.println("Message");
}
}
Re: How to pause & resume Java applet only when user clicks a button?
Quote:
Originally Posted by
natdizzle
try my java pause button:
Stop posting to old dead threads, here and elsewhere. If all you want is to share your code, start a blog. Bumping old threads to the top of the forum listing doesn't help anyone.
db
THREAD CLOSED