-1

Each time we run this code it displays 3 random cards out of 54. I want to call the cards1(); method(that displays 3 random cards) on mouse click. each time I click in frame 3 random cards should be displayed. Could any body please help?

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.*;



public class Cards extends JFrame  {

    public static void main(String[] args) {
        JFrame frame = new Cards();

        frame.setTitle("Cards");

        frame.setSize(300, 200);

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

    public Cards() {

        // Create array for cards

        cards1();
    }

    public void cards1() {
        ImageIcon[] images = new ImageIcon[54];
        for (int i = 1; i < images.length; i++) {

            images[i] = new ImageIcon("Drawables//Images//" + i + ".png");

        }

        // Get random number between 1 & 54... three times

        int[] threeRandoms = new int[3];
        Random ran = new Random();

        for (int i = 0; i < threeRandoms.length; i++) {

            threeRandoms[i] = ran.nextInt(54);

        }

        // Labels with gridLayout
        setLayout(new GridLayout(1, 4, 5, 5));

        add(new JLabel(images[threeRandoms[0]]));

        add(new JLabel(images[threeRandoms[1]]));

        add(new JLabel(images[threeRandoms[2]]));

    }


}
Usman Malik
  • 27
  • 1
  • 6

4 Answers4

0

Implement MouseListener to your class :

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.*;



public class Cards extends JFrame  implements MouseListener{

public static void main(String[] args) {
    JFrame frame = new Cards();

    frame.setTitle("Cards");

    frame.setSize(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

}

public Cards() {

    // Create array for cards

    cards1();
}
public void mouseClicked(MouseEvent arg0) {
 // HERE YOU CALL YOUR  cards1 to generate random cards: 
 cards1();
}

public void mouseExited(MouseEvent arg0) {

}

public void mouseEntered(MouseEvent arg0) {

}
.
.
. // TO THE END OF YOUR CLASS
yahya el fakir
  • 562
  • 2
  • 12
0

Do it this way

import java.awt.event.*;
import javax.swing.JFrame;

public class Cards implements MouseListener {

    public Cards() {
        JFrame frame = new JFrame("Cards");

         frame.setTitle("Cards");
         frame.setSize(300, 200);
         frame.setLocationRelativeTo(null);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         frame.addMouseListener(this);

         frame.setVisible(true);

    }

    public void cards1() {
        ImageIcon[] images = new ImageIcon[54];
        for (int i = 1; i < images.length; i++) {

             images[i] = new ImageIcon("Drawables//Images//" + i + ".png");

         }

        // Get random number between 1 & 54... three times

        int[] threeRandoms = new int[3];
        Random ran = new Random();

         for (int i = 0; i < threeRandoms.length; i++) {

             threeRandoms[i] = ran.nextInt(54);
        }

    }


    public void mouseClicked(MouseEvent e) {
        System.out.println("The frame was clicked.");
        cards1();

    }

    public void mouseEntered(MouseEvent e) {
        System.out.println("The mouse entered the frame.");

    }

    public void mouseExited(MouseEvent e) {
        System.out.println("The mouse exited the frame.");

    }

    public void mousePressed(MouseEvent e) {
        System.out.println("The left mouse button was pressed.");

    }

    public void mouseReleased(MouseEvent e) {
        System.out.println("The left mouse button was released.");

    }

    public static void main(String args[]) {
        new Cards();
    }

}
MCHAppy
  • 1,012
  • 9
  • 15
  • an error on in Main method new mouseClicked(); it say that turn it into static – Usman Malik Apr 30 '15 at 14:29
  • @UsmanMalik I test it in eclipse. It works perfectly. You can click on the frame and see the console to make sure. – MCHAppy Apr 30 '15 at 14:34
  • Console working fine as needed. I m near to achieve my goal with your assistance. only the problem is images doesn't shown. – Usman Malik Apr 30 '15 at 14:36
  • Ok I will add some images on my machine, test it again and then i will edit my answer. Give me some seconds ;) – MCHAppy Apr 30 '15 at 14:37
0
 public static void main(String[] args) {
  Cards frame = new Cards();
  frame.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
       frame.cards1();
    }
  });
...
} // end of main

Changed your frame type to Cards for calling the cards1 method from inside the mouseClicked method.

Additionals:

  • You initialize the GridLayout and the Label whenever cards1 method is clicked - should be done once. The cards1 method should only change the content.
  • Improvement: Read your images only once in the array. Let the cards1 method only access three randomly chosen images from the array
swinkler
  • 1,703
  • 10
  • 20
0

Inside your main method after you have initialised your frame variable add the code:

frame.addMouseListener(new MouseAdapter(){

   public void mouseClicked(MouseEvent e) {
      (Cards)frame.cards1();
   }

});

This is basically how you add the mouse listener.

But I see another problem. This is in the method card1 This method adds new instances of JLabel to the JFrame card a So it shall be visible as soon as you set the frame to visible. but again clicking it will add 3 new cards and so on.

Blip
  • 3,061
  • 5
  • 22
  • 50