Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Shuffling Cards - Write in Java Write a program that displays four c

ID: 3920172 • Letter: P

Question

Programming Shuffling Cards - Write in Java

Write a program that displays four cards randomly selected from a deck of 52 if the Refresh button is clicked, shown below. The card image files are named 1.png, 2.png … 52.png and stored in the image/card directory. All four cards are distinct and selected randomly. The card files are provided in Blackboard.

Hint: You can select random cards by storing the numbers 1-52 to an array list, perform a random shuffle learned in Section 11.12, and use the first four numbers in the array list as the file names for the image. The other way is to use the static shuffle method in the java.util.Collections class, for example: java.util.Collections.shuffle(list) where list is an array list.

Directions

Create a class named CardRefreshButton extends Application.

Create a new array list, then use a for loop and add() method to add 52 cards to the list.

Revoke shuffle() method for the list to shuffle the cards.

Create an instance of HBox and add four card images by using getChildren().add() method. Make sure specify image file path and use array list’s get() method to get four card image files.

Create a Button and labeled with “Refresh”

Create and register an event handler.

To handle event: once the button is clicked, call shuffle() method to shuffle the cards, then clear previous images by using getChildren.clear() method, and use add() to add four card images.

Create a new BorderPane to set up layout for the images and button.

Create a scene with a specific size.

Set title to “Shuffling cards” and display the stage.

The output should look like the screen below.

Provide appropriate Java comments.

Grading Rubric

Task

Points

Create user interface using Scene, Pane and Stage

2

Use array list and a for loop to hold 52 cards

2

Shuffle cards by using random selection or static shuffle method

2

Create a HBox and use appropriate methods to add images and setup layout

3

Create Refresh button and handler

2

Use methods to shuffle, clear and add images correctly to handle button click event

3

Use BorderPane correctly to setup layout

2

The output for Shuffling Cards is correct and looks like the sample screen

2

Total

18

Task

Points

Create user interface using Scene, Pane and Stage

2

Use array list and a for loop to hold 52 cards

2

Shuffle cards by using random selection or static shuffle method

2

Create a HBox and use appropriate methods to add images and setup layout

3

Create Refresh button and handler

2

Use methods to shuffle, clear and add images correctly to handle button click event

3

Use BorderPane correctly to setup layout

2

The output for Shuffling Cards is correct and looks like the sample screen

2

Total

18

Explanation / Answer

Program :

public class DeckOfCards extends JFrame

{

     

    private static ImageIcon image1 = new ImageIcon("images/1.png");

    private static ImageIcon image2 = new ImageIcon("images/2.png");

    private static ImageIcon image3 = new ImageIcon("images/3.png");

    private static ImageIcon image4 = new ImageIcon("images/4.png");

    private static ImageIcon image5 = new ImageIcon("images/5.png");

    private static ImageIcon image6 = new ImageIcon("images/6.png");

    private static ImageIcon image7 = new ImageIcon("images/7.png");

    private static ImageIcon image8 = new ImageIcon("images/8.png");

    private static ImageIcon image9 = new ImageIcon("images/9.png");

    private static ImageIcon image10 = new ImageIcon("images/10.png");

    private static ImageIcon image11 = new ImageIcon("images/11.png");

    private static ImageIcon image12 = new ImageIcon("images/12.png");

    private static ImageIcon image13 = new ImageIcon("images/13.png");

    private static ImageIcon image14 = new ImageIcon("images/14.png");

    private static ImageIcon image15 = new ImageIcon("images/15.png");

    private static ImageIcon image16 = new ImageIcon("images/16.png");

    private static ImageIcon image17 = new ImageIcon("images/17.png");

    private static ImageIcon image18 = new ImageIcon("images/18.png");

    private static ImageIcon image19 = new ImageIcon("images/19.png");

    private static ImageIcon image20 = new ImageIcon("images/20.png");

     

    //array to hold cards images

    ArrayList<ImageIcon> list = new ArrayList<ImageIcon>();

     

    JPanel cardsPanel = new JPanel();

    //generate numbers between 0-20

    static Random generator = new Random();

    private static int rand1 = generator.nextInt( 20 );

    private static int rand2 = generator.nextInt( 20 );

    private static int rand3 = generator.nextInt( 20 );

     

     

    private static JButton refresh = new JButton("Refresh");

     

    //constructor

    public DeckOfCards()

    {

        setLayout(new GridLayout(2, 1));

         

        add(cardsPanel);

         

        cardsPanel.setLayout(new FlowLayout(FlowLayout.LEFT,2,2));

         

        list.add(image1);

        list.add(image2);

        list.add(image3);

        list.add(image4);

        list.add(image5);

        list.add(image6);

        list.add(image7);

        list.add(image8);

        list.add(image9);

        list.add(image10);

        list.add(image11);

        list.add(image12);

        list.add(image13);

        list.add(image14);

        list.add(image15);

        list.add(image16);

        list.add(image17);

        list.add(image18);

        list.add(image19);

        list.add(image20);

         

            Collections.shuffle(list);

            cardsPanel.add(new JLabel(list.get(1)));

            Collections.shuffle(list);

             

            cardsPanel.add(new JLabel(list.get(1)));

            Collections.shuffle(list);

            cardsPanel.add(new JLabel(list.get(1)));

            Collections.shuffle(list);

            cardsPanel.add(new JLabel(list.get(1)));

             

             

        JPanel buttonPanel = new JPanel(new BorderLayout());

         

        buttonPanel.add(refresh,BorderLayout.CENTER);

        add(buttonPanel);

        refresh.addActionListener(new ButtonRefresh());

    }

    private class ButtonRefresh implements ActionListener

    {

        @Override

        public void actionPerformed(ActionEvent e) {

            // TODO Auto-generated method stub

            cardsPanel.removeAll();

            cardsPanel.add(new JLabel(list.get(1)));

            Collections.shuffle(list);

             

            cardsPanel.add(new JLabel(list.get(1)));

            Collections.shuffle(list);

            cardsPanel.add(new JLabel(list.get(1)));

            Collections.shuffle(list);

            cardsPanel.add(new JLabel(list.get(1)));

                       //test button to console

            System.out.println("aaa");

        }

         

    }

     

     

    public static void main(String[] args)

    {

        JFrame frame = new DeckOfCards();

        frame.pack();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

    }

}