Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 paintComponent freezes?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
joethefob




PostPosted: Sun Nov 28, 2010 12:11 am   Post subject: paintComponent freezes?

Newbie here. Very Happy

I have a GUI card program here. It is supposed to allow the user to enter the the amount of cards they wish to have, show the images of the cards, and then shuffle, add, or deal from that list. But right now, every time I click shuffle, the entire program becomes unresponsive.

My teacher and I have tracked the problem down to the paintComponent and the show method, but I still can't figure out the exact problem here. My shuffle method involves using Add and Deal, which both work separately. If I comment out one of those methods, the shuffle method works.

code:


// The "Deck" class.
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;
import java.awt.Graphics;

public class Link extends JFrame
{
    // static Console c;
    private Deck deck = new Deck (0); //Deck of cards
    private String[] suits = {"c", "d", "h", "s"}; //Array of suits
    private String[] ranks = {"a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k"}; //Array of ranks
    private JPanel window = new JPanel (); //Window
    private JComboBox suitlist = new JComboBox (suits); //Combo Box for Suits
    private JComboBox ranklist = new JComboBox (ranks); //Combo Box for Ranks
    private JComboBox numlist; //Combo box for Numbers Card Not Found
    private JButton show = new JButton ("Show"); //Show button
    private JButton shuffle = new JButton ("Shuffle"); //Shuffle button
    private JButton deal = new JButton ("Deal"); //Deal button
    private JButton draw = new JButton ("Draw"); //Draw button
    private JButton add = new JButton ("Add"); //Add button
    private JButton search = new JButton ("Search"); //Search button
    private JButton quicksort = new JButton ("Quick Sort"); //Quick Sort button
    private JButton selectionsort = new JButton ("Selection Sort"); //Selection Sort button
    private Drawing cards = new Drawing (deck); //Drawing
    private JLabel label = new JLabel (" "); //Label
    private JTextField tf = new JTextField ("Type in Size of Deck");

    public Link ()
    {
        //Set combo box for numbers
        String[] nums = new String [0];
        numlist = new JComboBox (nums);
        //Add all the JComponents
        window.add (tf);
        window.add (suitlist);
        window.add (ranklist);
        window.add (numlist);
        window.add (show);
        window.add (shuffle);
        window.add (deal);
        window.add (draw);
        window.add (add);
        window.add (search);
        window.add (quicksort);
        window.add (selectionsort);
        window.add (cards);
        window.add (label);
        //Add action listeners for all the buttons
        show.addActionListener (new showListener ());
        shuffle.addActionListener (new shuffleListener ());
        deal.addActionListener (new dealListener ());
        draw.addActionListener (new drawListener ());
        add.addActionListener (new addListener ());
        //Window
        setContentPane (window);
        setSize (1200, 200); //Set Size
        setTitle ("Card");
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo (null);
    }


    class showListener implements ActionListener //Show Listener
    {
        public void actionPerformed (ActionEvent e)
        {
            window.remove (cards);
            window.remove (label);
            String str = tf.getText ();
            str = str.trim ();
            if (!str.equals (""))
            {
                int i = Integer.parseInt (str);
                if (i >= 0)
                {
                    deck = new Deck (i);
                    cards = new Drawing (deck);
                    window.add (cards);
                    repaint ();
                    window.revalidate ();
                    for (int x = 0 ; x < deck.size () ; x++)
                        numlist.addItem (Integer.toString (x + 1));
                }
                window.add (label);
            }
        }
    }


    class shuffleListener implements ActionListener //Shuffle Listener
    {
        public void actionPerformed (ActionEvent e)
        {
            if (deck.size () > 0)
            {
                deck.shuffle (); //Shuffle
                repaint ();
            }
        }
    }


    class dealListener implements ActionListener //Deal Listener
    {
        public void actionPerformed (ActionEvent e)
        {
            if (deck.size () > 0)
            {
                Card card = deck.deal (); //Deal card
                ImageIcon img = new ImageIcon (card.toImage ()); //Convert from Image to ImageIcon
                label.setIcon (img); //Embed ImageIcon in label
                numlist.removeItemAt (numlist.getItemCount () - 1); //Remove item from list
                //Refresh
                repaint ();
                window.revalidate ();
            }
        }
    }


    class drawListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            if (numlist.getItemCount () > 0)
            {
                String num = (String) numlist.getSelectedItem (); //Selected Item
                int i = Integer.parseInt (num); //Convert from string to integer
                Card card = deck.deal (i); //Deal card
                ImageIcon img = new ImageIcon (card.toImage ()); //Convert from Image to ImageIcon
                label.setIcon (img); //Embed ImageIcon in label
                numlist.removeItemAt (numlist.getItemCount () - 1); //Remove item from list
                //Refresh
                repaint ();
                window.revalidate ();
            }
        }
    }


    class addListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            String suit = (String) suitlist.getSelectedItem (); //Selected Item from Suit List
            String rank = (String) ranklist.getSelectedItem (); //Selected Item from Rank List
            int num = numlist.getItemCount (); //Get Item Count
            Card card = new Card (suit, rank, true); //New Card
            deck.add (card); //Add card to the deck
            repaint (); //Refresh
            numlist.addItem (Integer.toString (num + 1)); //Add item to list
        }
    }

    public static void main (String[] args)
    {
        Link window = new Link ();
        window.setVisible (true);
    } // main method
}

public class Card
{
    //variables
    public Card next = null;
    private String suit;
    private String rank;
    private Image cardImage;
    private boolean back;

    public Card (String temp1, String temp2, boolean b)  //Constructor
    {
        suit = temp1; //Suit
        rank = temp2; //Rank
        back = b;
        if (b == true)
        {
            if (temp1.equals ("c") || temp1.equals ("d") || temp1.equals ("h") || temp1.equals ("s")) //If the suit is club, diamond, heart, spade
            {
                if (temp2.equals ("2") || temp2.equals ("3") || temp2.equals ("4") || temp2.equals ("5") || temp2.equals ("6") || temp2.equals ("7") || temp2.equals ("8") || temp2.equals ("9") || temp2.equals ("10") || temp2.equals ("a") || temp2.equals ("j") || temp2.equals ("q") || temp2.equals ("k")) //if the ranks are from A, 2-10, J, Q, or K
                    cardImage = loadImage (temp2 + temp1 + ".gif"); //Load Image
            }
        }
        else
            cardImage = loadImage ("b.gif"); //Back Image
    }


    public Image toImage ()  //Return Image
    {
        return cardImage;
    }


    public Image flip ()  //Flip
    {
        Card card = new Card (suit, rank, false);
        return card.toImage ();
    }


    public int getSuit ()  //Return numerical value of suit
    {
        if (suit.equals ("c"))
            return 0;
        else if (suit.equals ("d"))
            return 1;
        else if (suit.equals ("h"))
            return 2;
        else
            return 3;
    }


    public int getRank ()  //Return numerical value of rank
    {
        if (rank.equals ("a"))
            return 1;
        else if (rank.equals ("j"))
            return 11;
        else if (rank.equals ("q"))
            return 12;
        else if (rank.equals ("k"))
            return 13;
        else
            return Integer.parseInt (rank);
    }


    public Image loadImage (String name)  //Load Image
    {
        Image img = null;
        try
        {
            img = ImageIO.read (new File (name));
        }
        catch (IOException e)
        {
        }

        return img;
    }
}


public class Deck //Deck
{
    // private ArrayList cards = new ArrayList ()
    private Card first;
    private Card last;
    private int size = 0;

    public Deck (int length)
    {
        for (int x = 0 ; x < length ; x++) //Add cards to the deck
        {
            int num1 = x / 13;
            int num2 = x % 13;
            String str1 = "";
            String str2 = "";
            if (num1 == 0)
                str1 = "c";
            else if (num1 == 1)
                str1 = "d";
            else if (num1 == 2)
                str1 = "h";
            else
                str1 = "s";
            if (num2 == 0)
                str2 = "a";
            else if (num2 == 10)
                str2 = "j";
            else if (num2 == 11)
                str2 = "q";
            else if (num2 == 12)
                str2 = "k";
            else
                str2 = Integer.toString (num2 + 1);

            Card card = new Card (str1, str2, true);

            if (x == 0)
                first = card;
            else
                last.next = card;

            last = card;
            size++;
        }
    }


    public int size ()
    {
        return size;
    }


    public void show (Graphics g)  //Show cards
    {
        Card card = first;
        int x = 1;
        while (card != null)
        {
            Image img = card.toImage ();
            g.drawImage (img, x, 10, null);
            x += 15;
            card = card.next;
        }
    }


    public void shuffle ()  //Shuffle
    {
        // for (int x = 0 ; x < size ; x++)
        // {
            int num = (int) (Math.random () * size);
            Card temp = deal (num);
            add (temp);
            }
    }


    public Card deal ()  //Deal the top card
    {
        if (first != null) //While the deck is not empty
        {
            Card temp = first;
            first = first.next;
            size--;
            return temp;
        }
        else
            return new Card ("c", "a", false);
    }


    public Card deal (int i)  //Deal the card at index i
    {
        if (first != null) //While the deck is not empty
        {
            if (i == 1)
            {
                Card temp = deal ();
                size--;
                return temp;
            }
            else
            {
                Card temp1 = first;
                Card temp2 = first;
                int x = 1;
                while (x < (i - 1) && first != null)
                {
                    first = first.next;
                    x++;
                }
                temp2 = first.next;
                first.next = first.next.next;
                first = temp1;
                return temp2;
            }
            //1 2 3 4 5 6 7 8
        }
        else
            return new Card ("c", "a", false);
    }


    public void add (Card card)  //Add a new card
    {
        if (first == null)
        {
            first = card;
            size++;
        }
        else if (last == null)
        {
            first.next = card;
            last = first.next;
            size++;
        }
        else
        {
            last.next = card;
            last = last.next;
            size++;
        }
    }
} // Deck class


public class Drawing extends JPanel //Drawing
{
    private Deck cards;

    public Drawing (Deck deck)  //Constructor
    {
        cards = deck;
        this.setPreferredSize (new Dimension (1000, 125)); //Size
    }


    public void paintComponent (Graphics g)
    {
        super.paintComponent (g);
        cards.show (g); //Call the deck.show method
    }
}


Thank you so much for your help! Very Happy



New Folder.zip
 Description:
Here is the list of cards.

Download
 Filename:  New Folder.zip
 Filesize:  43.64 KB
 Downloaded:  131 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
carGUY17




PostPosted: Fri Dec 03, 2010 3:14 am   Post subject: RE:paintComponent freezes?

The reason the program freezes because it goes into an endless loop, when the shuffle method is called in the Deck class it calls the add method which increments the size variable then returns to the shuffle method with the size increment by 1 so the for loop never ends.Its endless loop.

Hope that helps
Denny
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: