Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: random card display problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default random card display problem

    hey guys im trying to get my code to display 3 random cards from my images folder ( the cards are labeled 1.png to 54.png). Im confused how i would get my program to read and display my cards.
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    public class DisplayCard extends JFrame {
    	String[] images;
    	for (int i = 1; i < 0 && i >=54;i++)
    	{
    	images[i] = i+".png";
    	}
     
    	Random rn = new Random();
    	int maximum = 54;
    	int minimum = 1;
    	int range = maximum - minimum + 1;
    	int randomNum =  rn.nextInt(range) + minimum;
     
      protected ImageIcon usIcon = new ImageIcon(images[randomNum]);
      protected ImageIcon myIcon = new ImageIcon(images[randomNum]);
      protected ImageIcon frIcon = new ImageIcon(images[randomNum]);
      protected ImageIcon ukIcon = new ImageIcon(images[randomNum]);
     
     
      public DisplayCard() {
        setLayout(new GridLayout(1, 4, 5, 5));
        add(new JLabel(usIcon));
        add(new JLabel(myIcon));
        add(new JButton(frIcon));
        add(new JButton(ukIcon));
      }
     
      /** Main method */
      public static void main(String[] args) {
    	DisplayCard frame = new DisplayCard();
        frame.setTitle("TestImageIcon");
        frame.setSize(500, 125);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }
    im confused it is giving me an error at ukicon = new imageIcon... basically its telling me to put a } after my semicolon.
    here is the error message
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at DisplayCard.main(DisplayCard.java:32)
    Appreciate the help guys.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: random card display problem

    There may be statements in the code that should be inside of a method.

    A full error message from the javac compiler could better explain what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: random card display problem

    Quote Originally Posted by Norm View Post
    There may be statements in the code that should be inside of a method.

    A full error message from the javac compiler could better explain what the problem is.
    I revised the code.Now i have everything finished but I can't get my actionlistener to randomize my cards again when I click my jbutton.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class DisplayCard extends JFrame {
    	//generate random numbers
    Random generator = new Random();
    int i = 54 - generator.nextInt(54);
    Random generator2 = new Random();
    int p = 54 - generator.nextInt(54);
    Random generator3 = new Random();
    int t = 54 - generator.nextInt(54);
    //defines images and button
    protected ImageIcon card1 = new ImageIcon("C:/Users/bearcat/Desktop/computer science/book/image/card/"+i+".png");
    protected ImageIcon card2 = new ImageIcon("C:/Users/bearcat/Desktop/computer science/book/image/card/"+p+".png");
    protected ImageIcon card3 = new ImageIcon("C:/Users/bearcat/Desktop/computer science/book/image/card/"+t+".png");
    JButton d= new JButton("Display Cards");
     
    //displays 3 cards and button
      public DisplayCard() {
        setLayout(new GridLayout(1, 4, 5, 5));
        add(new JLabel(card1));
        add(new JLabel(card2));
        add(new JLabel(card3));
        add(d);
      }
     
    public  void Button() {
    	 setSize(200,200);
    	   JButton startButton = new JButton("Start");
     
    	   add(startButton);
    	   startButton.addActionListener(new startButtonListener());
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    }
    private class startButtonListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e)
    	{
     
    	}
     
    }
     
      /** Main method */
      public static void main(String[] args) {
    	//calls method sets frame size, title
    	DisplayCard frame = new DisplayCard();
        frame.setTitle("12-9");
        frame.setSize(500, 125);
        frame.setLocationRelativeTo(null); // Center the frame
        //close and show frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
     
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: random card display problem

    I can't get my actionlistener to randomize my cards again when I click my jbutton.
    Where is the code that is supposed to do that?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Random suit and card with arrays
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 1st, 2012, 10:28 AM
  2. display the generated Math.random number
    By PsYNus in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 2nd, 2011, 10:25 AM
  3. Card Game Problem.
    By d'fitz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 07:30 AM
  4. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  5. [SOLVED] BlackJack.. Random Card Assignment?
    By Hallowed in forum Object Oriented Programming
    Replies: 5
    Last Post: April 5th, 2011, 07:25 AM