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 4 of 4

Thread: Snakes and Ladders

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

    Question Snakes and Ladders

    Hi there,

    Basically got a small problem with my action listener for a button within a JPanel.

    I want to call a method from another class within the action listener however it does not work. Calling the function in the same way works outside of the action listener. I have tried instantiating the class containing the method however this still is not working.

    Any help would be greatly appreciated, please see code below.

    Thanks

    SnakesAndLadders Class:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
     
    public class SnakesAndLadders
    {
    	public static void main(String[] args)
    	{
    		JFrame f=new JFrame();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(450,450);
     
    		final SnakesAndLaddersGUI gui=new SnakesAndLaddersGUI();
    		f.getContentPane().add(gui);
    		f.setVisible(true);
    		final Play play = new Play();
     
    		gui.setNumberOfPlayers(1);
            final int[]pos=new int[1];
     
            for(int i=0;i<1;i++)
            {
            	pos[i]=1;
                gui.setPosition(i,0);
            }
            try
            {
    			Thread.sleep(1500);
    		}
     
    		catch(Exception e){e.printStackTrace();}
     
    		JFrame frame = new JFrame("Network");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JButton roll = new JButton("Roll");
     
    		roll.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				Play play = new Play();
    				System.out.println("Button Pressed");
    				play.roll(pos,gui);
    			}
    		});
     
    		frame.getContentPane().add(roll);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }

    Play Class:
    public class Play
    {
    	public int roll(int pos[], SnakesAndLaddersGUI gui)
    	{
    		try
    		{
    			int dice;
    			Thread.sleep(500);
    			for(int i=0;i<1;i++)
    			{
    				dice=(int)(Math.random()*6);
    				while(dice>0)
    				{
    					gui.setPosition(i,pos[i]++);
    					dice--;
    					Thread.sleep(100);
    				}
    			}
    		}
    		catch(Exception e){e.printStackTrace();}
    		return 0;
    	 }
     
     }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Snakes and Ladders

    Saying "it doesn't work" is as useful for us as us saying "then fix it" is useful to you. What actually happens?

    PS- You seem to be sleeping on the EDT, which is generally a horrible idea. You probably want a Swing Timer instead.
    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
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Snakes and Ladders

    Sorry for not being specific. I have copied the error message shown in the command window below.

    is it possible to call a method in another class from the main class i.e:

    Main
    {
          public void actionPerformed(ActionEvent e)
          {
                Play play = new Play();
                 play.roll(pos,gui);
           }
    }
    pos is an array and gui is used to access a method "setPosition" which is located in another class.


    Button Pressed
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWai
    t from the event dispatcher thread
    at java.awt.EventQueue.invokeAndWait(EventQueue.java: 981)
    at SnakesAndLaddersGUI.syncRequestRepaint(SnakesAndLa ddersGUI.java:80)
    at SnakesAndLaddersGUI.setPosition(SnakesAndLaddersGU I.java:25)
    at Play.roll(Play.java:14)
    at SnakesAndLadders$1.actionPerformed(SnakesAndLadder s.java:45)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:19
    95)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
    a:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
    .java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242
    )
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
    istener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577
    )
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)

    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
    ad.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
    java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
    ad.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)

    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    Thanks

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Snakes and Ladders

    Yeah, it sounds like you're doing something wonky on the EDT. You have to be really careful not to do too much work on the EDT, as well as not doing any GUI work off of the EDT. Also, you can't call invokeAndWait() on the EDT. I'd recommend you post an SSCCE that demonstrates what you're 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!