Results 1 to 7 of 7
- 10-02-2012, 01:38 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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.
- 10-02-2012, 02:54 AM #2
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()If you don't understand my response, don't ignore it, ask a question.
- 10-02-2012, 04:56 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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.
- 10-02-2012, 05:10 AM #4
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.
throw error as thread can be started only once.
I'm done for tonight. Back tomorrow.If you don't understand my response, don't ignore it, ask a question.
- 10-02-2012, 06:59 AM #5
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 12-15-2012, 08:19 PM #6
Re: How to pause & resume Java applet only when user clicks a button?
try my java pause button:
Java 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"); } }
- 12-16-2012, 03:26 AM #7
Re: How to pause & resume Java applet only when user clicks a button?
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Differentiating user clicks and doClick clicks?
By jiffi in forum New To JavaReplies: 19Last Post: 01-13-2012, 08:30 AM -
How do I check if user clicks on an image?
By rajkobie in forum New To JavaReplies: 10Last Post: 05-04-2011, 04:45 AM -
pause threads on button click
By Saran185 in forum Threads and SynchronizationReplies: 1Last Post: 03-07-2011, 01:45 PM -
Draw rectangle in applet with coordinates of two mouse clicks
By cselic in forum Java AppletsReplies: 7Last Post: 04-10-2010, 07:07 PM -
The trouble with handling the button clicks
By Borneq in forum New To JavaReplies: 3Last Post: 01-10-2010, 02:57 PM
Bookmarks