Results 1 to 9 of 9
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Intermittently stepping through method using pause

    I am trying to pause through a method, stopping at numerous IF statements (card combinations) to determine if it scores. If so, I want to place a Border, Pause for X seconds, Remove Border, then Resume on to next IF statement. My first attempt didnt work out as it did in me head. I set a Swing Timer in my IF statement to fire if true. In the ActionPerformed I called a method that created and started a Thread. Which in this Thread, I had a Swing Timer loop for 10 seconds. I guess I could have also just set InitialDelay to 10000 and fire-once still set to true. Either way, things didn't work. I have an issue on grasping the schematics behind using Threads and the EDT.

    If someone out there can point me in a general direction of possible ideas that are not so confusing as to what I tried to do already, I would greatly appreciate it.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Intermittently stepping through method using pause

    Are you doing a repaint() to ensure the border appears? Each time you change the color you
    should probably do a repaint(). In some cases it may be done by the other event code
    if a property is changed.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermittently stepping through method using pause

    Well I can get the Borders to show. And I can get the borders to set back to null. I follow along with what you're saying about the repaint(). My issue is the pause step in between. I tried a Swing Timer, that triggered a Thread, that ran a Swing Timer and that was dismal.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Intermittently stepping through method using pause

    What you want to do is something like the following:

    1. create two borders of two different colors
    2. Assign active border to common variable
    3. Set active border to first border
    4. repaint().
    5. set your timer.
    6. when time goes off, set active border to second border.
    7. repaint().

    The border you are painting is the active border. You could also do this
    with flags to choose which border to paint.

    I have not tried this so it may need tweaking. If you are having
    problems post a small runnable example for us to look at.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermittently stepping through method using pause

    First off, earlier I said I could get the borders on and off. That was just knowing how to set them. I hadn't tried with a Swing Timer. With that said, I made this which is kind of what I was going for, but as you see not working.
    Java Code:
    public class TimedBorderExample {
    	
    	private JFrame frame;
    	private Dimension dim = new Dimension(200,200);
    	
    	public TimedBorderExample(){
    		frame = new JFrame("Setting Delay for Border Show");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setPreferredSize(dim);
    		frame.setSize(dim);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		
    		Example example = new Example();
    		frame.add(example);		
    		frame.pack();
    		
    		frame.setVisible(true);
    		
    	}
    	
    	private class Example extends JComponent{
    
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    		private JPanel myPanel;
    		private JLabel myLabel;
    		private Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 3);
    		private Border yellowBorder = BorderFactory.createLineBorder(Color.YELLOW, 13);
    		private boolean isTripped;
    		private Timer timer;
    		
    		public Example(){
    			myPanel = new JPanel();
    			myPanel.setSize(dim);
    			myPanel.setBackground(Color.BLACK);
    			myPanel.setLocation(0, 0);
    			
    			myLabel = new JLabel("BORDER SWAP");
    			myLabel.setBorder(greenBorder);
    			myLabel.addMouseListener(new MouseAdapter() {
    				public void mousePressed(MouseEvent e){
    					ifTests();
    				}
    			});
    			myPanel.add(myLabel);
    			frame.add(myPanel);
    			
    		}
    		
    		public JLabel getMyLabel(){
    			return myLabel;
    		}
    		
    		public void setIsTripped(boolean isTripped){
    			this.isTripped = isTripped;
    		}
    		public void ifTests(){
    			boolean scored = true;
    			pauseHere();
    			if(scored){
    				myLabel.setBorder(yellowBorder);
    				myLabel.revalidate();
    				myLabel.repaint();
    				pauseHere();
    				myLabel.setBorder(greenBorder);
    				myLabel.revalidate();
    				myLabel.repaint();
    			}			
    			pauseHere();
    			
    			if(scored){
    				myLabel.setBorder(yellowBorder);
    				myLabel.revalidate();
    				myLabel.repaint();
    				pauseHere();
    				myLabel.setBorder(greenBorder);
    				myLabel.revalidate();
    				myLabel.repaint();
    			}
    			
    			pauseHere();
    			
    			if(scored){
    				getMyLabel().setBorder(yellowBorder);
    				myLabel.revalidate();
    				myLabel.repaint();
    				pauseHere();
    				myLabel.setBorder(greenBorder);
    				myLabel.revalidate();
    				myLabel.repaint();
    			}			
    		}			
    	
    		public void pauseHere(){
    			isTripped = false;
    			timer = new Timer(0, new ActionListener() {
    				
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					try {
    						System.out.println("Pause for 2 seconds here");
    						wait(2000);
    						setIsTripped(true);
    						timer.stop();
    					} catch (InterruptedException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
    				}
    			});
    			timer.setRepeats(false);
    			timer.start();
    			int count = 0;
    			do{
    				System.out.println(count + " sheep");
    				count++;
    			}while(!isTripped);
    		}
    	}	
    	
    	public static void main(String args[]){
    		EventQueue.invokeLater(new Runnable(){
    
    			@Override
    			public void run() {
    				new TimedBorderExample();
    			}			
    		});
    	}
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Intermittently stepping through method using pause

    I believe you're taking the wrong approach. I presume you want to flash the border when an something occurs. So let the timer to the work.
    In your actionListener, let the timer switch the borders every other time the timer goes off. You can create the timer and the borders
    at the beginning. Probably in an inner class, passing the label in the constructor. But there are other ways to do it. Take advantage
    of the Timer.setRepeat() method. You may want to put the timer in a thread if you want to continue the game while the border is flashing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermittently stepping through method using pause

    I think putting the timer in a thread was my mistake. I am still working on an outcome, but what I am wanting to do in essence is NOT continue the game for x amount of time. From my understanding, won't the EDT create a Thread and keep on going. I need to pause the EDT, so I figure my work is just making a Timer. But trying to make the timer wait() gave the following:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException
    	at java.lang.Object.wait(Native Method)
    	at example.ryan.TimedBorderExample$Example$1.actionPerformed(TimedBorderExample.java:58)
    	at javax.swing.Timer.fireActionPerformed(Unknown Source)
    	at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$400(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Big Picture:
    Card Game (Cribbage)
    Method - Counts all possible card combinations
    I want each card to originally have no border. If in the Method, it reaches an if statement in which a combination of cards adds to 15, then I want the non-existent border to be replaced by a yellow border (only around the cards involved), have the game pause for a second or so to allow someone to read the updated scoreboard. But also allow them to quickly see each scoring combination they had. Maybe this helps clear up a little of how I am using this.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Intermittently stepping through method using pause

    Quote Originally Posted by rru96 View Post
    From my understanding, won't the EDT create a Thread and keep on going.
    Yep. The last sentence of my previous post was simply incorrect. So if you simply want to pause the entire game
    why not just change the border and do a Thread.sleep() and then change it back and sleep some more. I haven't tried
    that but it seems like it should work. Timers are more for asynchronous activities.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermittently stepping through method using pause

    Working great in my application. Now I just need to edit the method for other reasons. The border isn't applying correctly, but I think I know where that issue may be. Thank you for helping out Jim!

Similar Threads

  1. Replies: 0
    Last Post: 10-21-2013, 11:29 AM
  2. stepping through numbered variable names
    By PuppetJacks in forum New To Java
    Replies: 3
    Last Post: 11-04-2011, 08:37 AM
  3. Move and pause method
    By Learning Java in forum New To Java
    Replies: 20
    Last Post: 05-14-2011, 01:10 PM
  4. Applet intermittently not loading in browser
    By amycwhitfield in forum Java Applets
    Replies: 2
    Last Post: 09-09-2010, 02:09 PM
  5. Replies: 0
    Last Post: 06-30-2010, 11:39 AM

Tags for this Thread

Posting Permissions

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