Results 1 to 7 of 7
  1. #1
    user4321 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default 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.

  3. #3
    user4321 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default 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.
    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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Arrow 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");
    	}
    }

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: How to pause & resume Java applet only when user clicks a button?

    Quote Originally Posted by natdizzle View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Differentiating user clicks and doClick clicks?
    By jiffi in forum New To Java
    Replies: 19
    Last Post: 01-13-2012, 08:30 AM
  2. How do I check if user clicks on an image?
    By rajkobie in forum New To Java
    Replies: 10
    Last Post: 05-04-2011, 04:45 AM
  3. pause threads on button click
    By Saran185 in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-07-2011, 01:45 PM
  4. Replies: 7
    Last Post: 04-10-2010, 07:07 PM
  5. The trouble with handling the button clicks
    By Borneq in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 02:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •