Results 1 to 5 of 5
  1. #1
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Randomised buttons

    Hi all,

    I've been trying to create a grid of check boxes, 3 of which are enabled and selected randomly. I've tried at least 3 ways to do this but my code always produces either the first button to be selected only, or no buttons selected at all and I can't figure out why.

    Java Code:
    import java.util.LinkedList;
    import javax.swing.JRadioButton;
    import javax.swing.JFrame;
    import java.awt.GridLayout;
    
    public class Test extends JFrame {
    
    	private LinkedList<JRadioButton> list;
    	private JRadioButton [] [] button;
    	public static int $targets = 2;
    
    	public static void main(String[] args) {
    	
    		Test test = new Test();
    		test.start();
    	
    	}
    	
    	public void start() {
    	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		setResizable(true);
    		setLayout(new GridLayout(5, 5));
    	
    		list = new LinkedList<>();
    		button = new JRadioButton [5] [5];
    		
    		for (int a = 0; a < 5; a++) {
    			for (int b = 0; b < 5; b++) {
    			
    				button [a] [b] = new JRadioButton();
    				button [a] [b].setSelected(false);
    				button [a] [b].setEnabled(false);
    				list.add(button [a] [b]);
    				add(button [a] [b]);
     			
    			}
    		
    		}
    		
    		JRadioButton [] targetArray = new JRadioButton [$targets];
    		int i = 0;
    		
    		while ($targets > 0) {
    		
    			int x = (int) Math.random() * 5;
    			int y = (int) Math.random() * 5;
    			int index = x * y;
    		
    			targetArray [i] = (JRadioButton) list.remove(index);
    			targetArray [i].setSelected(true);
    			list.add(index, targetArray [i]);
    			
    			$targets--;
    			i++;
    		
    		}
    		
    		pack();
    	
    	}
    
    }

    I've created this class simply to test the randomising feature before I implement it into my main project. The LinkedList is necessary for the main project.

    I would really appreciate it if someone could show me what I'm doing wrong :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Randomised buttons

    Because $targets is set to 2, and you only loop while $targets is greater than 0, so your loop runs twice and randomly selects a single button each time.

    In total a maximum of 2 buttons may be selected like this.

    You need to actually loop through your whole grid and randomly select the buttons.

  3. #3
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Re: Randomised buttons

    Thanks for the reply, I'll see what I can do this time.

  4. #4
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Re: Randomised buttons

    I've revised my code and came up with a solution! It's not 100% effective as there is a small chance that one element may be selected twice but here's what I came up with:

    Java Code:
    import java.awt.GridLayout;
    import java.util.LinkedList;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    public class TargetFrame extends JPanel {
    
    	private TitledBorder border;
        private Target [] [] target;
        private static LinkedList<Target> targetList;
    
    	public TargetFrame(int numTargets) {
    			
    			super(new GridLayout(40, 40, 2, 2));
    			border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Targets");
    			border.setTitleJustification(TitledBorder.DEFAULT_JUSTIFICATION);
    			setBorder(border);
    			setVisible(true);
    			
    			int [] targetIndices = new int [numTargets];
    			targetIndices = ranIntArray(numTargets);
    			
    			target = new Target [40] [40];
    			targetList = new LinkedList<>();
    
    			for (int a = 0; a < 40; a++) {
    				for (int b = 0; b < 40; b++) {
    					
    					target [a] [b] = new Target(false);
    					targetList.add(target [a] [b]);
    					
    				}
    			}
    			
    			for (int c = 0; c < targetIndices.length; c++) {
    				
    				int index = targetIndices[c];
    				Target newActiveTarget = targetList.get(index);
    				newActiveTarget.setTarget();
    				targetList.add(index, newActiveTarget);
    				
    			}
    			
    			for (int d = 0; d < targetList.size(); d++) {
    				
    				Target targetToAdd = targetList.get(d);
    				add(targetToAdd);
    				
    			}
    			
    		   
    		} // end of TargetFrame constructor
    		
    	}
    	
    	private int [] ranIntArray(int quantity) {  
    
    		int startNum = 1600; 
    		double [] arrayD = new double [quantity];
    		int [] arrayI = new int [quantity];
    
    		for (int i = 0; i < quantity; i++) {
    
    			arrayD [i] = (startNum * Math.random());
    			arrayI [i] = (int) arrayD [i];
    
    		}
    
    		return arrayI;
    
    	} // end of ranIntArray method
    		
    }
    Thanks :D

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Randomised buttons

    why didn't you just loop through your elements like you did before?

    Java Code:
                for (int a = 0; a < 40; a++) {
                    for (int b = 0; b < 40; b++) {                   
                       //do something with target [a] [b]
                    }
                }

Similar Threads

  1. actions for buttons
    By steel in forum AWT / Swing
    Replies: 1
    Last Post: 12-02-2010, 05:42 AM
  2. Help with Buttons
    By wld4ubabay in forum New To Java
    Replies: 20
    Last Post: 05-17-2010, 08:13 AM
  3. creating buttons
    By defactor in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 02:06 PM
  4. Where To Get Pop Up Buttons
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2009, 04:05 AM
  5. How to use SWT Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 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
  •