1 Replies - 482 Views - Last Post: 06 March 2013 - 07:08 PM Rate Topic: -----

#1 ric1989   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 28-February 12

Math random implementation

Posted 06 March 2013 - 06:36 PM

How do i use a timer and math random to display random images for label 1, label2, label3 ?
this is only partial code and wanted to know if my actionPerformed make sense aswell where when a user
click ok it should stop the random images?

public void addComponentsToPane(final Container pane){
        final JPanel compsToExperiment = new JPanel();
        final JPanel compsExtras = new JPanel();        
        compsToExperiment.setLayout(experimentLayout);
        compsExtras.setLayout(BeginEnd);
        
        //gridLayout to divide into 3 buttons and 3 images
        JPanel sample = new JPanel();
        sample.setLayout(new GridLayout(2,3));
        
        //add my pictures as labels
        ImageIcon icon = new ImageIcon("banana.png");
        label1 = new JLabel(icon);
        ImageIcon icon2 = new ImageIcon("heart.png");
        label2 = new JLabel(icon2);
        ImageIcon icon3 = new ImageIcon("star.png");
        label3 = new JLabel(icon3);
      
        //Add buttons to experiment with Grid Layout
        compsToExperiment.add(new JButton("ok"));
        compsToExperiment.add(new JButton("ok"));
        compsToExperiment.add(new JButton("ok"));
        compsExtras.add(new JButton("start"));
        compsExtras.add(new JButton("STOP ALL"));
   
        pane.add(new JLabel(icon), BorderLayout.WEST); 
        pane.add(new JLabel(icon2), BorderLayout.EAST);
        pane.add(new JLabel(icon3), BorderLayout.CENTER);
        pane.add(compsExtras, BorderLayout.NORTH); 
        pane.add(compsToExperiment, BorderLayout.SOUTH);   
}
 
public void actionPerformed(ActionEvent e){//create 5 action listener classes for each buttons 5 
startButton.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
endButton.addActionListener(this);

if (e.getSource()== startButton){  //do stuff;
   stop[0] = false;
}
    
if (e.getSource() == button1){
    stop[1] = true;
}

if (e.getSource() == button2){
   stop[2] = true;
}

if (e.getSource() == button3){
  stop[3] = true;
}

if (e.getSource() == endButton){
 //stop[] = true;
}
}

public void createIcon(){
        rg = new Random();
        pictures = new ImageIcon[Arraysize]; //initialize and three pictures set as arrays
        pictures[0] = new ImageIcon("banana.png");
        pictures[1] = new ImageIcon("heart.png");
        pictures[2] = new ImageIcon("star.png");
}



Is This A Good Question/Topic? 0
  • +

Replies To: Math random implementation

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Math random implementation

Posted 06 March 2013 - 07:08 PM

Is this your one armed bandit code again?

You have the array of images. You have a button to pull the arm that calls start() on the timer.

You set up a timer:

Timer timer = new Timer(delay, actionListener); //delay could be 0 as this only runs once.
timer.setRepeats(false);



In your ActionPerformed you just call setIcon of a random

Random rand = new Random();

jLabel.setIcon(pictures[rand.nextInt(3)]);




I don't know. I have to think about it more, but it isn't really interesting, so I can't. :)

This post has been edited by farrell2k: 06 March 2013 - 07:09 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1