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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Getting Pictures to Spin

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Getting Pictures to Spin

    Hi,

    i have two programs where i want the pictures to spin. On one program the pictures spin and on the other they dont. The code is the same so im not sure whats going on.

    Program where pictures do spin

     if (e.getSource() == btnStopSpin )
            stopTimer();
     
          if (e.getSource() == btnSpin ){
            counter = 0;
            timer = new Timer(100, this);
            timer.start() ;
          }
     
          if (e.getSource() == timer ){
            setIcon() ;
          }
     
      }// end of Action Performed
     
        public void stopTimer(){
        	timer.stop();
        }
     
     
     
     
        private void setIcon(){
     
        	switch (k) {
        		case 0:
        			btnSpinPictures.setIcon(iconPic1);
        			k=1;
        			counter++;
        			break;
        		case 1:
        			btnSpinPictures.setIcon(iconPic2);
        			k=2;
        			counter++;
        			break;
        		case 2:
        			btnSpinPictures.setIcon(iconPic3);
        			k=0;
        			counter++;
        			break;
     
        	}
     
           	if (counter >20)
        		stopTimer();
     
     
        }
     
     
    }

    Program where the pictures don spin

     if(event.getSource()==btnStart){
    			counter = 0;
    			timer = new Timer(10,this);
    			timer.start();
     
    		}
     
    		if(event.getSource()==timer) {
    			setIcon ();
    		}
    		if(event.getSource()==timer){
    			timer.stop();
    		}
    }
     
    }
     
    		public void stopTimer(){
    			timer.stop();
     
    	}
     
     
     
     
     
    public void setIcon(){
     
    			one = random.nextInt(8);
     
     
    		switch (one){
     
    		case 1:
    			lblDisplayPic1.setIcon(iconPic1);
    			one = 1;
    			counter ++;
    			break;
     
    		case 2:
    			lblDisplayPic1.setIcon(iconPic2);
    			one = 2;
    			counter ++;
    			break;
     
    		case 3:
    			lblDisplayPic1.setIcon(iconPic3);
    			one = 3;
    			counter ++;
    			break;
     
    		case 4:
    			lblDisplayPic1.setIcon(iconPic4);
    			one = 4;
    			counter ++;
    			break;
     
    		case 5:
    			lblDisplayPic1.setIcon(iconPic5);
    			one = 5;
    			counter ++;
    			break;
     
    		case 6:
    			lblDisplayPic1.setIcon(iconPic6);
    			one = 6;
    			counter ++;
    			break;
     
    		case 7:
    			lblDisplayPic1.setIcon(iconPic7);
    			one = 7;
    			counter ++;
    			break;
     
    		case 8:
    			lblDisplayPic1.setIcon(iconPic8);
    			one = 8;
    			counter ++;
    			break;
    		}
     
    		two = random.nextInt(8);
     
    switch (two){
     
    		case 1:
    			lblDisplayPic2.setIcon(iconPic1);
    			two = 1;
    			counter ++;
    			break;
     
    		case 2:
    			lblDisplayPic2.setIcon(iconPic2);
    			two = 2;
    			counter ++;
    			break;
     
    		case 3:
    			lblDisplayPic2.setIcon(iconPic3);
    			two = 3;
    			counter ++;
    			break;
     
    		case 4:
    			lblDisplayPic2.setIcon(iconPic4);
    			two = 4;
    			counter ++;
    			break;
     
    		case 5:
    			lblDisplayPic2.setIcon(iconPic5);
    			two = 5;
    			counter ++;
    			break;
     
    		case 6:
    			lblDisplayPic2.setIcon(iconPic6);
    			two = 6;
    			counter ++;
    			break;
     
    		case 7:
    			lblDisplayPic2.setIcon(iconPic7);
    			two = 7;
    			counter ++;
    			break;
     
    		case 8:
    			lblDisplayPic2.setIcon(iconPic8);
    			two = 8;
    			counter ++;
    			break;
     
    }
    three = random.nextInt(8);
     
    switch (three){
     
    		case 1:
    			lblDisplayPic3.setIcon(iconPic1);
    			three = 1;
    			counter ++;
    			break;
     
    		case 2:
    			lblDisplayPic3.setIcon(iconPic2);
    			three = 2;
    			counter ++;
    			break;
     
    		case 3:
    			lblDisplayPic3.setIcon(iconPic3);
    			three = 3;
    			counter ++;
    			break;
     
    		case 4:
    			lblDisplayPic3.setIcon(iconPic4);
    			three = 4;
    			counter ++;
    			break;
     
    		case 5:
    			lblDisplayPic3.setIcon(iconPic5);
    			three = 5;
    			counter ++;
    			break;
     
    		case 6:
    			lblDisplayPic3.setIcon(iconPic6);
    			three = 6;
    			counter ++;
    			break;
     
    		case 7:
    			lblDisplayPic3.setIcon(iconPic7);
    			three = 7;
    			counter ++;
    			break;
     
    		case 8:
    			lblDisplayPic3.setIcon(iconPic8);
    			three = 8;
    			counter ++;
    			break;
     
     
    			}
    checkWin();  
     
    if(counter >20)
    	stopTimer();
     
    }


    Id appreciate any help

    Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Can you make a small, complete program that compiles, executes and shows the problem?

    What do you mean by "picture spin"?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    Like you see the reels spin on a fruit machine.
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class SpinSimulator extends JFrame implements ActionListener{
      private int k = 0;
      private int counter;
      private JButton btnSpinPictures,btnSpin, btnStopSpin, btnExit;
      private Timer timer;
      private Icon iconPic1, iconPic2, iconPic3;
     
     
      public static void main(String[] args) {
     
    	  SpinSimulator spinApp = new SpinSimulator();
          spinApp.setVisible(true) ;
      }
      public SpinSimulator(){
     
    	 super(".... Spinning Simulator....");
     
          btnSpinPictures = new JButton("");
          btnSpinPictures.setPreferredSize(new Dimension(100, 100)) ;
          btnSpinPictures.setBackground(Color.yellow );
          btnExit = new JButton("Quit?");
          btnExit.setBackground(Color.cyan ) ;
          btnSpin = new JButton("Spin");
          btnSpin.setBackground(Color.green ) ;
          iconPic1 = new ImageIcon("bin/Pic1.jpg");
          iconPic2 = new ImageIcon("bin/Pic4.jpg");
          iconPic3 = new ImageIcon("bin/Pic5.jpg");
     
          setBackground(Color.pink) ;
          setLayout(new FlowLayout()) ;
          add(btnSpinPictures) ;
          add(btnSpin) ;
          add(btnExit) ;
     
          btnSpin.addActionListener(this) ;
          btnExit.addActionListener(this) ;
     
          setSize(200, 250);
     
      }
     
     
        public void actionPerformed( ActionEvent e){
     
          if (e.getSource() == btnExit){
            JOptionPane.showMessageDialog(null, "Quiting Program :-)") ;
            System.exit(0) ;
          }
     
     
     
     
          if (e.getSource() == btnSpin ){
            counter = 0;
            timer = new Timer(100, this);
            timer.start() ;
          }
     
          if (e.getSource() == timer ){
            setIcon() ;
          }
     
      }
     
        public void stopTimer(){
        	timer.stop();
        }
     
        private void setIcon(){
     
     
        	switch (k) {
        		case 0:
        			btnSpinPictures.setIcon(iconPic1);
        			k=1;
        			counter++;
        			break;
        		case 1:
        			btnSpinPictures.setIcon(iconPic2);
        			k=2;
        			counter++;
        			break;
        		case 2:
        			btnSpinPictures.setIcon(iconPic3);
        			k=0;
        			counter++;
        			break;
     
        	}
     
           	if (counter >20)
        		stopTimer();
     
     
        }
     
     
    }

    thats the basic program. To see how it runs you would have to add the pics.

    is it because its a Jbutton where the pictures are shown?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    By spinning do you mean showing different images over time? Like a slideshow?


    What is shown when the posted program is executed? What is the problem with the posted code?
    Last edited by Norm; August 28th, 2012 at 10:13 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    i suppose so yeah. So it would flicker through each of the pictures eventually stopping on a random one

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    What is shown when the posted program is executed? What is the problem with the posted code?
    When the Spin button is pressed, It shows some images and then stops
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    Sorry, the code i just posted was for the program that works. What the program does is what i would like my other program to do but for some reason that i dont know of it wont. Instead of showing some images and stopping, it just shows one random image.

    The code for my other program is:

     import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.Icon;
    import javax.swing.JTextField;
     
    import javax.swing.Timer;
     
     
     
    public class ElectronicSlot8 extends JFrame implements ActionListener
     
    {
     
    	private Icon iconPic1, iconPic2, iconPic3, iconPic4, iconPic5, iconPic6, iconPic7, iconPic8;
    	private JLabel lblBalance, lblWinnings;
    	private JTextField txtBalance, txtWinnings; 
    	private JButton btnBye, btnHoldLeft, btnHoldRight, btnHoldMiddle;
    	private Timer timer; 
    	private JButton btnStart, btnMoney, btnCashout, btnTransfer;
    	private int one, two, three, counter;
    	private int balance, winnings;
     
     
     
     
    	private Random random;
    	private JButton btnSpinPicture1, btnSpinPicture2, btnSpinPicture3;
     
     
     
     
    	public static void main(String[] args) 
     
    	{
    		ElectronicSlot8 electonicslot8 = new ElectronicSlot8();
    		electonicslot8.setVisible(true);
    	}
    		public ElectronicSlot8()
     
    {
     
    			balance = 0;
    			random = new Random();
     
    		setLayout(new FlowLayout());
     
    		btnSpinPicture1 = new JButton();
    		btnSpinPicture1.setPreferredSize(new Dimension (150,150));
    		btnSpinPicture1.setBackground (Color.gray);
     
    		btnSpinPicture2 = new JButton();
    		btnSpinPicture2.setPreferredSize(new Dimension (150,150));
    		btnSpinPicture2.setBackground (Color.gray);
     
    		btnSpinPicture3 = new JButton();
    		btnSpinPicture3.setPreferredSize(new Dimension (150,150));
    		btnSpinPicture3.setBackground (Color.gray);
     
     
    		txtBalance = new JTextField(3);
    		txtBalance.setFont(new Font("Arial", Font.BOLD, 16));
    		txtBalance.setBackground(Color.white);
    		txtBalance.setForeground(Color.blue);
    		txtBalance.setText(balance + "");
     
    		txtWinnings = new JTextField(3);
    		txtWinnings.setFont(new Font("Arial", Font.BOLD, 16));
    		txtWinnings.setBackground(Color.white);
    		txtWinnings.setForeground(Color.blue);
    		txtWinnings.setText(balance + "");
     
    		btnTransfer = new JButton("Add to Balance");
    		btnTransfer.setEnabled(false);
    		btnMoney = new JButton("Add 1");
    		btnStart = new JButton("Play");
    		btnStart.setEnabled(false);
    		btnBye = new JButton("Exit");
    		btnCashout = new JButton("Withdraw");
     
    		btnHoldLeft = new JButton("Hold");
    		btnHoldLeft.setPreferredSize(new Dimension(150,25));
    		btnHoldLeft.setEnabled(false);
     
    		btnHoldRight = new JButton("Hold");
    		btnHoldRight.setPreferredSize(new Dimension(150,25));
    		btnHoldRight.setEnabled(false);
     
    		btnHoldMiddle = new JButton("Hold");
    		btnHoldMiddle.setPreferredSize(new Dimension(150,25));
    		btnHoldMiddle.setEnabled(false);
     
     
    		lblBalance = new JLabel("Balance");
    		lblBalance.setPreferredSize(new Dimension(100,50));
    		lblBalance.setBackground(Color.black);
     
    		lblWinnings = new JLabel("Winnings");
    		lblWinnings.setPreferredSize(new Dimension(100,50));
    		lblWinnings.setBackground(Color.black);
     
     
    		add(txtBalance);
    		add(lblBalance);
    		add(txtWinnings);
    		add(lblWinnings);
    		add(btnStart);
    		add(btnBye);
    		add(btnSpinPicture1);
    		add(btnSpinPicture2);
    		add(btnSpinPicture3);
    		add(btnHoldLeft);
    		add(btnHoldMiddle);
    		add(btnHoldRight);
    		add(btnMoney);
    		add(btnCashout);
    		add(btnTransfer);
     
     
    		btnMoney.addActionListener(this);
    		btnStart.addActionListener(this);
    		btnTransfer.addActionListener(this);
    		btnBye.addActionListener(this);
    		btnCashout.addActionListener(this);
    		btnHoldLeft.addActionListener(this);
    		btnHoldMiddle.addActionListener(this);
    		btnHoldRight.addActionListener(this);
     
    		iconPic1 = new ImageIcon("bin/Pic1.jpg");
    		iconPic2 = new ImageIcon("bin/Pic2.jpg");
    		iconPic3 = new ImageIcon("bin/Pic3.jpg");
    		iconPic4 = new ImageIcon("bin/Pic4.jpg");
    		iconPic5 = new ImageIcon("bin/Pic5.jpg");
    		iconPic6 = new ImageIcon("bin/Pic6.jpg");
    		iconPic7 = new ImageIcon("bin/Pic7.jpg");
    		iconPic8 = new ImageIcon("bin/Pic8.jpg");
     
    		random = new Random();
     
    		setTitle("Electronic Bandit");
    		setSize(500,320);
     
     
    }	
     
     
    	public void actionPerformed (ActionEvent event)
     
    {
     
    		if(event.getSource()==btnBye)
    		{
    			JOptionPane.showMessageDialog(null,"Thank You For Playing....Bye!!!");
     
    			System.exit(0);
    		}
     
    		if(event.getSource()==btnCashout)
    		{
    		JOptionPane.showMessageDialog(null, "You have collected  " +winnings+"p, Bye!!!");
     
    			System.exit(0);
     
    		}
     
    		if (event.getSource()==btnMoney){
    			txtBalance.setForeground(Color.black);
    			balance = balance + 100;
    			txtBalance.setText(balance + "");
     
    		}
     
    		if (event.getSource() ==btnMoney)
    		{
    		btnStart.setEnabled(true);
    		}
     
    		if (event.getSource()==btnTransfer){
    			balance = winnings + balance;
    			txtBalance.setText(balance +"");
    			JOptionPane.showMessageDialog(null,"You have transferred  " +winnings+"p");
    		}
     
    		if (event.getSource()==btnTransfer){
    			txtWinnings.setText("0");
    		}
     
    		if (event.getSource()==btnTransfer){
    			winnings =0;
    			btnTransfer.setEnabled(false);
    			JOptionPane.showMessageDialog(null,"You now have no winnings to transfer!!!");
     
    		}
    		if(winnings >=20){
    			btnTransfer.setEnabled(true);
    			}
     
     
    		if (event.getSource() ==btnStart)
    		{
    			btnHoldLeft.setEnabled(true);
    			btnHoldMiddle.setEnabled(true);
    			btnHoldRight.setEnabled(true);
    		}
     
     
     
    			if (event.getSource() ==btnStart){
    				balance = balance - 20;
    				txtBalance.setText(balance +"");
    			}
    			if (balance>=20){
    				btnStart.setEnabled(true); 
     
    		}
     
    			if(balance <20){
    				balance = 0;
     
    				btnStart.setEnabled(false);
     
    				JOptionPane.showMessageDialog(null,"After this spin you will not have enough Credit. Please insert more Money!!!");
    			}
     
     
     
    			if(event.getSource()==btnStart){
    			counter = 0;
    			timer = new Timer(100,this);
    			timer.start();
     
    		}
     
    		if(event.getSource()==timer) {
    			setIcon ();
    		}
    		if(event.getSource()==timer){
    			timer.stop();
    		}
    }
     
     
     
    		public void stopTimer(){
    			timer.stop();
     
    	}
     
     
     
     
     
    public void setIcon(){
     
    			one = random.nextInt(8);
     
     
    		switch (one){
     
    		case 0:
    			btnSpinPicture1.setIcon(iconPic1);
    			one = 1;
    			counter ++;
    			break;
     
    		case 1:
    			btnSpinPicture1.setIcon(iconPic2);
    			one = 2;
    			counter ++;
    			break;
     
    		case 2:
    			btnSpinPicture1.setIcon(iconPic3);
    			one = 3;
    			counter ++;
    			break;
     
    		case 3:
    			btnSpinPicture1.setIcon(iconPic4);
    			one = 4;
    			counter ++;
    			break;
     
    		case 4:
    			btnSpinPicture1.setIcon(iconPic5);
    			one = 5;
    			counter ++;
    			break;
     
    		case 5:
    			btnSpinPicture1.setIcon(iconPic6);
    			one = 6;
    			counter ++;
    			break;
     
    		case 6:
    			btnSpinPicture1.setIcon(iconPic7);
    			one = 7;
    			counter ++;
    			break;
     
    		case 7:
    			btnSpinPicture1.setIcon(iconPic8);
    			one = 0;
    			counter ++;
    			break;
    		}
     
    		two = random.nextInt(8);
     
    switch (two){
     
    		case 0:
    			btnSpinPicture2.setIcon(iconPic1);
    			two = 1;
    			counter ++;
    			break;
     
    		case 1:
    			btnSpinPicture2.setIcon(iconPic2);
    			two = 2;
    			counter ++;
    			break;
     
    		case 2:
    			btnSpinPicture2.setIcon(iconPic3);
    			two = 3;
    			counter ++;
    			break;
     
    		case 3:
    			btnSpinPicture2.setIcon(iconPic4);
    			two = 4;
    			counter ++;
    			break;
     
    		case 4:
    			btnSpinPicture2.setIcon(iconPic5);
    			two = 5;
    			counter ++;
    			break;
     
    		case 5:
    			btnSpinPicture2.setIcon(iconPic6);
    			two = 6;
    			counter ++;
    			break;
     
    		case 6:
    			btnSpinPicture2.setIcon(iconPic7);
    			two = 7;
    			counter ++;
    			break;
     
    		case 7:
    			btnSpinPicture2.setIcon(iconPic8);
    			two = 0;
    			counter ++;
    			break;
     
    }
    three = random.nextInt(8);
     
    switch (three){
     
    		case 0:
    			btnSpinPicture3.setIcon(iconPic1);
    			three = 1;
    			counter ++;
    			break;
     
    		case 1:
    			btnSpinPicture3.setIcon(iconPic2);
    			three = 2;
    			counter ++;
    			break;
     
    		case 2:
    			btnSpinPicture3.setIcon(iconPic3);
    			three = 3;
    			counter ++;
    			break;
     
    		case 3:
    			btnSpinPicture3.setIcon(iconPic4);
    			three = 4;
    			counter ++;
    			break;
     
    		case 4:
    			btnSpinPicture3.setIcon(iconPic5);
    			three = 5;
    			counter ++;
    			break;
     
    		case 5:
    			btnSpinPicture3.setIcon(iconPic6);
    			three = 6;
    			counter ++;
    			break;
     
    		case 6:
    			btnSpinPicture3.setIcon(iconPic7);
    			three = 7;
    			counter ++;
    			break;
     
    		case 7:
    			btnSpinPicture3.setIcon(iconPic8);
    			three = 0;
    			counter ++;
    			break;
     
     
    			}
    checkWin();  
     
    if(counter >20)
    	stopTimer();
     
    }
     
    public void checkWin(){
     
    	if (one == two && two == three){
    	winnings = winnings + 100;
    	txtWinnings.setText(winnings + "");
    	JOptionPane.showMessageDialog(null, "Winner 1.00");
    	}
    	else if ( one == two || one == three || two == three){
    	winnings = winnings + 50;
    	txtWinnings.setText(winnings + "");
    	JOptionPane.showMessageDialog(null, "50p Win");
    	}
    	else if (three == 1){
    	winnings = winnings + 20;
    	txtWinnings.setText(winnings + "");
    	JOptionPane.showMessageDialog(null, "20p Win");
    	}
    	}
     
    	}

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    sorry that there is alot of code in there.

    the code that is meant to spin the pictures is:

     
    if(event.getSource()==btnStart){
    			counter = 0;
    			timer = new Timer(100,this);
    			timer.start();
     
    		}
     
    		if(event.getSource()==timer) {
    			setIcon ();
    		}
    		if(event.getSource()==timer){
    			timer.stop();
    		}
    }
     
     
     
    		public void stopTimer(){
    			timer.stop();
     
    	}
     
     
     
     
     
    public void setIcon(){
     
    			one = random.nextInt(8);
     
     
    		switch (one){
     
    		case 0:
    			btnSpinPicture1.setIcon(iconPic1);
    			one = 1;
    			counter ++;
    			break;
     
    		case 1:
    			btnSpinPicture1.setIcon(iconPic2);
    			one = 2;
    			counter ++;
    			break;
     
    		case 2:
    			btnSpinPicture1.setIcon(iconPic3);
    			one = 3;
    			counter ++;
    			break;
     
    		case 3:
    			btnSpinPicture1.setIcon(iconPic4);
    			one = 4;
    			counter ++;
    			break;
     
    		case 4:
    			btnSpinPicture1.setIcon(iconPic5);
    			one = 5;
    			counter ++;
    			break;
     
    		case 5:
    			btnSpinPicture1.setIcon(iconPic6);
    			one = 6;
    			counter ++;
    			break;
     
    		case 6:
    			btnSpinPicture1.setIcon(iconPic7);
    			one = 7;
    			counter ++;
    			break;
     
    		case 7:
    			btnSpinPicture1.setIcon(iconPic8);
    			one = 0;
    			counter ++;
    			break;
    		}
     
    		two = random.nextInt(8);
     
    switch (two){
     
    		case 0:
    			btnSpinPicture2.setIcon(iconPic1);
    			two = 1;
    			counter ++;
    			break;
     
    		case 1:
    			btnSpinPicture2.setIcon(iconPic2);
    			two = 2;
    			counter ++;
    			break;
     
    		case 2:
    			btnSpinPicture2.setIcon(iconPic3);
    			two = 3;
    			counter ++;
    			break;
     
    		case 3:
    			btnSpinPicture2.setIcon(iconPic4);
    			two = 4;
    			counter ++;
    			break;
     
    		case 4:
    			btnSpinPicture2.setIcon(iconPic5);
    			two = 5;
    			counter ++;
    			break;
     
    		case 5:
    			btnSpinPicture2.setIcon(iconPic6);
    			two = 6;
    			counter ++;
    			break;
     
    		case 6:
    			btnSpinPicture2.setIcon(iconPic7);
    			two = 7;
    			counter ++;
    			break;
     
    		case 7:
    			btnSpinPicture2.setIcon(iconPic8);
    			two = 0;
    			counter ++;
    			break;
     
    }
    three = random.nextInt(8);
     
    switch (three){
     
    		case 0:
    			btnSpinPicture3.setIcon(iconPic1);
    			three = 1;
    			counter ++;
    			break;
     
    		case 1:
    			btnSpinPicture3.setIcon(iconPic2);
    			three = 2;
    			counter ++;
    			break;
     
    		case 2:
    			btnSpinPicture3.setIcon(iconPic3);
    			three = 3;
    			counter ++;
    			break;
     
    		case 3:
    			btnSpinPicture3.setIcon(iconPic4);
    			three = 4;
    			counter ++;
    			break;
     
    		case 4:
    			btnSpinPicture3.setIcon(iconPic5);
    			three = 5;
    			counter ++;
    			break;
     
    		case 5:
    			btnSpinPicture3.setIcon(iconPic6);
    			three = 6;
    			counter ++;
    			break;
     
    		case 6:
    			btnSpinPicture3.setIcon(iconPic7);
    			three = 7;
    			counter ++;
    			break;
     
    		case 7:
    			btnSpinPicture3.setIcon(iconPic8);
    			three = 0;
    			counter ++;
    			break;
     
     
    			}
    checkWin();  
     
    if(counter >20)
    	stopTimer();
     
    }
    Last edited by usherlad; August 28th, 2012 at 10:22 AM. Reason: missed the code out

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Have you tried debugging the code by adding lots of println statements to print out the values of variables used to control execution flow and to show the execution flow. That will be how I debug the code.


    Please explain what the program does that is not what you want?



    One problem I see is the "Hold" button doesn't do what you'd expect.
    Last edited by Norm; August 28th, 2012 at 10:33 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    No i havn't. Stupid question here but how do i do that?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Do you know how to code a println? Here's an example:
               System.out.println("obj="+object);

    The code has lots of logic like if statements and switch statements, print out the values of the variables used in those statements so you can see what the program sees as it executes.

    Please explain what the program does that is not what you want?



    One problem I see is the "Hold" button doesn't do what you'd expect. The image above the button changes when Play is pressed after the Hold button beneath it is pressed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    i have tried what you said on timer and i got the following response:

    javax.swing.Timer@a3bcc1

    is that correct or not?

    I have given up on the hold buttons for now cause they've been making me go mad for days, so i thought id get this spin part to work since im nearly there

  13. #13
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    Everything works as how it should. I don't know whether the response i got from the timer was correct or not

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    What is there about the Timer you want to know? What was printed: javax.swing.Timer@a3bcc1 is the String returned by the TImer class's toString() method (really the Object class's method): the name of the class followed by the object's hashcode. That shouldn't be any use for you to know in your code.

    Comment on the code:
    in the setIcon() method. All those switch statements look like a lot of repeated code. When I see that I think: Here is a place for an array.

    In the actionPerformed() method, there are a lot of if tests for the same values. For example:
    		if (event.getSource()==btnTransfer){    // first of many <<<<<<<<<
    			balance = winnings + balance;
    			txtBalance.setText(balance +"");
    			JOptionPane.showMessageDialog(null,"You have transferred  " +winnings+"p");
    		}
     
    		if (event.getSource()==btnTransfer){       //?????????????????
    			txtWinnings.setText("0");
    		}
     
    		if (event.getSource()==btnTransfer){       //???????????????????
    			winnings =0;
    			btnTransfer.setEnabled(false);
    			JOptionPane.showMessageDialog(null,"You now have no winnings to transfer!!!");
      ...

    Why not merge those into a single if?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Everything works as how it should
    Is your problem solved now?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    sorry i meant everything but the timer. I don't know whether im blind or what but im confused as to why the pictures dont act the same as the pictures do in the SpinSimulator program.

    Yeah i have been trying to make it a bit tidier and will try to do the array. For the moment it works so im trying to concentrate on the things that arn't. then im going to sort that out

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    why the pictures dont act the same as the pictures do in the SpinSimulator program.
    Can you explain what you mean by "act the same"?
    What does the code do now?
    What do you want it to do differently?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    on the SpinSimulator program, when you click the button "Spin", it flicks through three pictures giving the effect that its spinning. When you click the button "Play" on the ElectronicSlot8 program the pictures dont give that effect. They only display one random image instead of flicking through all of them.

    Does that make sense? im useless at explaining things.

    All i want to do is to get the ElectronicSlot8 program to have the same effect as the SpinSimulator program when the "Play" button is clicked. The code is the same but i'm not getting the same outcome

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Do you want the three image display boxes to display a "slideshow" of images before stopping on an image? The SpinSimulator program has logic to chose an image, display it, wait some, chose another image, display it,etc
    The ElectronicSlot8 program has logic to chose an image and display it. It needs logic like the other program to: select, display, wait many times in a loop.


    What about the repeated if tests in the actionPerformed() method?
    See post#14
    Did you merge them?
    Last edited by Norm; August 28th, 2012 at 01:01 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    yeah i would like the three image displays to do that. Just like the SpinSimulator.

    oh i thought the coding for them was both the same and thought they would both have done that.

    Could you please tell me what am i actually missing?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    First you need to fix this: What about the repeated if tests in the actionPerformed() method?
    See post#14
    Did you merge them?

    Then do some debugging by adding println statements to show when the code is executing and what it is doing as it executes. For example print a message for each of the three display boxes each time a new image is displayed in the box.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    I have managed to merge the if statements so it looks more tidy.

    I cant really make an array because i havnt learnt about these yet and its only testing us on what we have already learnt.

    I have tried to do some debugging and it only shows that one picture has been shown in each box. Not sure what else im meant to get out of this

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Not sure what else im meant to get out of this
    If you expect more than one image to be shown in a box, what code will make that happen? Is the code doing what you want it to do? By printing out messages showing where the code is executing and what the values of variables are as it executes, you can see what the computer sees and hopefully find out why the code is not doing what you expect.

    The merging of the if statements was to correct some possible logic errors. Not just to make the code "tidy".

    Can you post the current version of the code?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Feb 2012
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting Pictures to Spin

    public void actionPerformed (ActionEvent event)
     
    {
     
    		if(event.getSource()==btnBye){
    			JOptionPane.showMessageDialog(null,"Thank You For Playing....Bye!!!");
    				System.exit(0);
    		}
     
    		if(event.getSource()==btnCashout){
    			JOptionPane.showMessageDialog(null, "You have collected  " +winnings+"p, Bye!!!");
    				System.exit(0);
    		}
     
    		if (event.getSource()==btnMoney){
    			txtBalance.setForeground(Color.black);
    			balance = balance + 100;
    			txtBalance.setText(balance + "");
    			btnStart.setEnabled(true);
    		}
     
     
    		if (event.getSource()==btnTransfer){
    			balance = winnings + balance;
    			txtBalance.setText(balance +"");
    			txtWinnings.setText("0");
    			JOptionPane.showMessageDialog(null,"You have transferred  " +winnings+"p");
    			winnings =0;
    			btnTransfer.setEnabled(false);
    			JOptionPane.showMessageDialog(null,"You now have no winnings to transfer!!!");
    		}
     
    		if(winnings >=20){
    			btnTransfer.setEnabled(true);
    			}
     
     
    		if (event.getSource() ==btnStart){
    			btnHoldLeft.setEnabled(true);
    			btnHoldMiddle.setEnabled(true);
    			btnHoldRight.setEnabled(true);
    			balance = balance - 20;
    			txtBalance.setText(balance +"");
    		}
     
    			if (balance>=20){
    					btnStart.setEnabled(true); 
    			}else{
    					btnStart.setEnabled(false);
    						JOptionPane.showMessageDialog(null,"After this spin you will not have enough Credit. Please insert more Money!!!");
    			}
     
    			if(event.getSource()==btnStart){
    			counter = 0;
    			timer = new Timer(100,this);
    			timer.start();
     
    		}
     
    		if(event.getSource()==timer) {
    			setIcon ();
    			timer.stop();
     
    		}
    }
     
    		public void stopTimer(){
    			timer.stop();
     
    	}

    thats the code that i sorted out. I keep trying different things cause im kinda of lost at the moment.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Getting Pictures to Spin

    Please post all of the code to be sure we using the same code as you are.

    I still see repeated if tests that should be merged.
    If you don't understand my answer, don't ignore it, ask a question.

  26. The Following User Says Thank You to Norm For This Useful Post:

    usherlad (August 28th, 2012)

Page 1 of 2 12 LastLast

Similar Threads

  1. How Do i add pictures to a jframe?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 21st, 2012, 12:34 PM
  2. Slot machine, animating the reels to spin
    By LukeDavison in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2011, 10:59 AM
  3. Importing gif/pictures
    By javanerd in forum Java Theory & Questions
    Replies: 2
    Last Post: November 1st, 2010, 12:58 AM
  4. New Forum Avatar Pictures
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: February 14th, 2009, 12:15 PM
  5. How to animate pictures using JCreator?
    By bil_Imma in forum AWT / Java Swing
    Replies: 6
    Last Post: February 13th, 2009, 07:00 AM