Java GUI, frame and 3 cards displayed randomly

Java GUI, frame and 3 cards displayed randomly

Page 1 of 1

3 Replies - 19872 Views - Last Post: 26 September 2010 - 06:39 PM Rate Topic: -----

#1 eg_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-September 10

Java GUI, frame and 3 cards displayed randomly

Posted 26 September 2010 - 06:23 PM

-Display a frame that contains 3 labels
-Each label is a card as shown in the attachment below
-the card files are named 1.png, 2.png, ... 54.png.
-All three cards are distinct and selected randomly.

This is what I have so far and it displays three cards. But I cant make them display randomly.
import javax.swing.*;
import java.awt.*;

public class Cards extends JFrame{
    public static void main (String[] args){
    Cards frame = new Cards();
    frame.setTitle("Cards");
    frame.setSize(300,200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}




    private ImageIcon Card1 = new ImageIcon("C:/Users/Shantall/Documents/1.png");
    private ImageIcon Card2 = new ImageIcon("C:/Users/Shantall/Documents/38.png");
    private ImageIcon Card3 = new ImageIcon("C:/Users/Shantall/Documents/16.png");

public Cards(){
        setLayout(new GridLayout(1,4,5,5));
        add(new JLabel(Card1));
        add(new JLabel(Card2));
        add(new JLabel(Card3));
    }

}



Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Java GUI, frame and 3 cards displayed randomly

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java GUI, frame and 3 cards displayed randomly

Posted 26 September 2010 - 06:31 PM

They won't be randomly displayed the way your code is done
There are many way to achieve what you want to do.. the easiest way is to put your card in an ArrayList and remove them randomly

ArrayList<ImageIcon> al = new ArrayList<ImageIcon>();
al.add(Card1);
al.add(Card2);
al.add(Card3);
Random ran = new Random();
while(al.size() != 0) {
  int next = ran.nextInt(al.size);
  add(new JLabel(al.remove(next));
}


Was This Post Helpful? 0
  • +
  • -

#3 eg_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-September 10

Re: Java GUI, frame and 3 cards displayed randomly

Posted 26 September 2010 - 06:34 PM

Thanks I will try this ASAP.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java GUI, frame and 3 cards displayed randomly

Posted 26 September 2010 - 06:39 PM

View Posteg_90, on 26 September 2010 - 07:34 PM, said:

Thanks I will try this ASAP.

I'll bet it will work :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1