5 Replies - 6862 Views - Last Post: 30 May 2011 - 02:10 PM Rate Topic: -----

#1 Omulous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-May 11

Problem: repaint() won't call paintComponent

Posted 29 May 2011 - 11:26 PM

Hi so I've been doing a school assignment but I can't seem to figure out why my repaint command isn't calling the paintComponent.

I declare an object for a class with a value of 52, which then uses an arraylist and calls another class, and in that class the data fields are assigned a value, and then repaint is called, but it won't call the paint component.

Here is bits of the code

public class FA_Card extends JFrame
{
    Cards card;
    Deck dC = new Deck(52);//calling the deck class
    
    JPanel draw = new JPanel();
    JPanel content = new JPanel ();
    JPanel north = new JPanel ();
    JPanel north2 = new JPanel();
    JPanel frame = new JPanel();
   

    public FA_Card ()
    {

        // Create a content pane
        //card = new Card (250, 500);
        content.setLayout (new BorderLayout ()); // Use FlowLayout for panel
        content.add(north,"West");
        north.setLayout (new FlowLayout ()); // Use FlowLayout for input area
        north2.setLayout (new FlowLayout());
        
        JPanel south = new JPanel ();
        south.setLayout (new FlowLayout ());
        
        draw.add (dC, BorderLayout.CENTER);
        north.add (txt, "WEST");
        txt.setEditable(false);
        content.add(draw,BorderLayout.CENTER);//adding the deck class object
        
        content.add (north, "West"); // Input area        
        setContentPane (content);
        pack ();
        setTitle ("Cards"); //sets title of the program
        setSize (550, 600);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //sets the default close operation to when you press close (X)
        setLocationRelativeTo (null);           // Center window.
        //getContentPane ().add (card, BorderLayout.CENTER);

    } // FasihAwan_ArrayList class


public static void main (String[] args)
{
    FA_Card win = new FA_Card ();
    win.setVisible (true);
    win.setLocationRelativeTo (null);
}
}
class Deck extends JPanel
{
    String suit[] = {"c", "d", "h", "s"};
    Cards card;
    public ArrayList <Cards> deck = new ArrayList <Cards> ();

    public Deck (int size)
    {
        for (int y = 1 ; y <= size ; y++)
        {
            deck.add (card = new Cards (y));
        }
    }

}


class Cards extends JPanel
{
    protected int rank, suit; //suite = c(1),d(2),h(3),s(4)
    protected boolean[] isUp = new boolean [52];
    protected Image card = null;
    public int x=10,counter = 0,y = 10;


    public Cards (int c)  //given a number
    {
        if (c > 13 && c < 27)
        {
            rank = c - 13;
            suit = 2;
            //isUp [rank * suit] = true;
        }
        else if (c > 26 && c < 40)
        {
            rank = c - 26;
            suit = 3;
            //isUp [rank * suit] = true;
        }
        else if (c > 39)
        {
            rank = c - 39;
            suit = 4;
            //isUp [rank * suit] = true;
        }
        else if (c < 14)
        {
            rank = c;
            suit = 1;
            //isUp [c] = true;
        }
        repaint();
        
    }


    public void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        g.setColor (Color.RED);
        g.fillOval(75, 75, 10, 10);
        x+=25;
        if (x == 335)
        {
                x= 10;
                y+= 10;
        }
        System.out.print ("CHECK-REPAINTED");
        Image img = loadImage (rank + setSuit (suit) + ".gif");
        g.drawImage (img, 10*x, 2*y, null);
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem: repaint() won't call paintComponent

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem: repaint() won't call paintComponent

Posted 30 May 2011 - 08:42 AM

It would be better if you posted something compilable so we could recreate the error.

Cards is a JPanel. So you have have an arraylist of JPanels, 52 Cards objects. That really doesn't make any sense, especially since each Cards object has an array of 52 booleans. Why does each Cards need to know about the other 51 cards?

You have an arraylist of JPanels in a JPanel in a JFrame.


Before we go any farther, I would revisit your design.
Was This Post Helpful? 1
  • +
  • -

#3 Omulous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-May 11

Re: Problem: repaint() won't call paintComponent

Posted 30 May 2011 - 09:37 AM

Oh that array isn't used, I should take it out.
As for the Cards class, I guess I should take out the extends JPanel.

Here, I'll post the code of the whole thing.

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.ArrayList;
import javax.imageio.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JCheckBox;

public class FA_Card extends JFrame
{
    Cards card;
    Deck dC = new Deck(52);
    
    JPanel content = new JPanel ();
    JPanel north = new JPanel ();
    JPanel flipNotShuffle = new JPanel ();
    JPanel single = new JPanel ();
    JTextField txt = new JTextField (5);
    int chk = 0;
    JButton shuffle = new JButton ("Shuffle");
    JButton deal = new JButton ("Deal");
    JCheckBox dealPOS = new JCheckBox ("Position?");
    JButton add = new JButton ("Add");
    JButton search = new JButton ("Search");
    JButton quicksort = new JButton ("Q-Sort");
    JButton combsort = new JButton ("C-Sort");
    JButton submit = new JButton ("Go");
    JTextField pos = new JTextField (1);
    JPanel north2 = new JPanel();
    JPanel frame = new JPanel();
    JPanel draw = new JPanel();
    JComboBox crd;
    JComboBox suit;
    JComboBox sCrd;
    JComboBox sSuit;
    

    public FA_Card ()
    {

        // Create a content pane
        //card = new Card (250, 500);
        content.setLayout (new BorderLayout ()); // Use FlowLayout for panel
        content.add(north,"West");
        north.setLayout (new FlowLayout ()); // Use FlowLayout for input area
        north2.setLayout (new FlowLayout());
        
        JPanel south = new JPanel ();
        south.setLayout (new FlowLayout ());
        
       
        String[] numbs = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] sts = {"Spades", "Hearts", "Diamons", "Clubs"};

        crd = new JComboBox (numbs);
        suit = new JComboBox (sts);
        sCrd = new JComboBox (numbs);
        sSuit = new JComboBox (sts);
        crd.setEnabled (false);
        suit.setEnabled (false);

        dealPOS.addActionListener (new dP ());
        dealPOS.setEnabled(false);

        JLabel posLabel = new JLabel ("Position:");
       north.add(shuffle);
       north.add(deal);
       north.add (add);
       north.add (crd);
       north.add (suit);
       north.add (posLabel);
       north2.add(search);
       north2.add (quicksort);
       north2.add(sCrd);
       north2.add(sSuit);
       north2.add(submit);
       submit.setEnabled (false);
       sCrd.setEnabled(false);
       sSuit.setEnabled (false);
       
       draw.add(dC, BorderLayout.CENTER);
       dC.show();
       draw.add (dC, BorderLayout.CENTER);
       add.addActionListener(new addAction());
       search.addActionListener (new searchAction());

       dealPOS.setEnabled(false);
       pos.setEditable (false);
        
       submit.addActionListener (new CardAction ());

        
        //north.add (cd, BorderLayout.NORTH);
        north.add (txt, "WEST");
        txt.setEditable(false);
        content.add(draw,BorderLayout.CENTER);
        
        content.add (north, "West"); // Input area        
        content.add (north2, "South");

        setContentPane (content);
        pack ();
        setTitle ("Cards"); //sets title of the program
        setSize (550, 600);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //sets the default close operation to when you press close (X)
        setLocationRelativeTo (null);           // Center window.
        //getContentPane ().add (card, BorderLayout.CENTER);

    } // FasihAwan_ArrayList class


    class addAction implements ActionListener
    {
        public void actionPerformed (ActionEvent s)  //show, shuffle, deal, add, search, quicksort, combsort
        {
           suit.setEnabled(true);
           crd.setEnabled(true);
           sCrd.setEnabled (false);
           sSuit.setEnabled(false);
           submit.setEnabled(true);
           
        }
    }
   /***/ class submitSearch implements ActionListener
    {
    	public void actionPerformed (ActionEvent sS)
    	{//for search Card temp = new Card and then search for that?
    		String str = submit.getText();
    		try
    		{
    			int n = Integer.parseInt(str);
    		}
    		catch (NumberFormatException x)
    		{
    		}    		
    		
    		
    	}
    }
    class searchAction implements ActionListener
    {
    	public void actionPerformed (ActionEvent s)
    	{
    		sCrd.setEnabled(true);
    		sSuit.setEnabled (true);
            suit.setEnabled(false);
            crd.setEnabled(false);
            submit.setEnabled(true);
            
    	}
    }
    class dP implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            boolean dealP = dealPOS.isSelected ();
            if (dealP)
                pos.setEditable (true);
            else if (!dealP)
                pos.setEditable (false);

        }
    }

    class CardAction implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            Deck dC = new Deck (52);
            
            String num =(String) crd.getSelectedItem ();
            String st = (String)suit.getSelectedItem ();
            String str = pos.getText ();
            String fS = st.substring (0, 1);
            fS = fS.toLowerCase ();
            int value = Integer.parseInt (num);
            int deal_POS;
            int addNum = Integer.parseInt (num);
            int suitNum = (st.equals ("Spades") ? 1:st.equals ("Hearts") ? 2:st.equals ("Diamonds") ? 3:4);
            

        }

    }



public static void main (String[] args)
{
    FA_Card win = new FA_Card ();
    win.setVisible (true);
    win.setLocationRelativeTo (null);
}
}
class Deck extends JPanel
{
    String suit[] = {"c", "d", "h", "s"};
    Cards card;
    public ArrayList <Cards> deck = new ArrayList <Cards> ();

    public Deck ()
    {
        deck.size ();
    }


    public Deck (int size)
    {
        for (int y = 1 ; y <= 52 ; y++)
        {
            deck.add (card = new Cards (y));
        }
    }

    public void show()
    {
    	card.show();
    }
    public void shuffle ()
    {
        //deck.shuffle ();
    }


    public void deal (int choice)
    {
        card = deck.get (choice); // gets the xth card from the deck
        //display the card at the bottom of the program (stackable)
        deck.remove (choice);
    }


    public void add (int val, int st)
    {
    	int x = Cards.setSuit(suit[st]);
    	card = new Cards(val,x);
        deck.add (card);
    }


    public int search (Cards card)
    {
        if (!deck.contains (card))
            return -1;
        else
            return deck.indexOf (card);
    }
}


class Cards
{
    protected int rank, suit; //suite = c(1),d(2),h(3),s(4)
    protected Image card = null;
    public int x=10,counter = 0,y = 10;


    public Cards (int r, int s)  //given rank and suite
    {
        rank = r;
        suit = s;
        isUp [r * s] = true;
        repaint();
    }


    public Cards (int c)  //given a number
    {
        if (c > 13 && c < 27)
        {
            rank = c - 13;
            suit = 2;
            //isUp [rank * suit] = true;
        }
        else if (c > 26 && c < 40)
        {
            rank = c - 26;
            suit = 3;
            //isUp [rank * suit] = true;
        }
        else if (c > 39)
        {
            rank = c - 39;
            suit = 4;
            //isUp [rank * suit] = true;
        }
        else if (c < 14)
        {
            rank = c;
            suit = 1;
            //isUp [c] = true;
        }
                
    }
    public void show()
    {
    	this.repaint();
    }

    public void paintComponent (Graphics g)
    {
    	super.paintComponent(g);
    	g.setColor (Color.RED);
    	g.fillOval(75, 75, 10, 10);
    	x+=25;
    	if (x == 335)
    	{
    		x= 10;
    		y+= 10;
    	}
    	System.out.print ("CHECK-REPAINTED");
        Image img = loadImage (rank + setSuit (suit) + ".gif");
        g.drawImage (img, 10*x, 2*y, null);
    }


    public int getRank ()
    {
        return rank;
    }


    public int getSuite ()
    {
        return suit;
    }


    public void showCard ()
    {

    }


    public void turnOver ()
    {
        isUp [rank * suit] = true;
    }



    public String setSuit (int num)
    {
        String suit = "";
        if (num == 1)
            suit = "c";
        else if (num == 2)
            suit = "d";
        else if (num == 3)
            suit = "h";
        else if (num == 4)
            suit = "s";
        return suit;
    }
    public static int setSuit (String x)
    {
    	if (x.equals("c"))
    		return 1;
    	else if (x.equals("d"))
    		return 2;
    	else if (x.equals("h"))
    		return 3;
    	else
    		return 4;
    }


    public static Image loadImage (String name)  //Loads Images
    {

        Image img = null;
        try
        {
            img = ImageIO.read (new File (name));
        }
        catch (IOException e)
        {
        }
        return img;
    }
}




This post has been edited by Omulous: 30 May 2011 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem: repaint() won't call paintComponent

Posted 30 May 2011 - 09:42 AM

Why is the paintComponent() in your Cards class when it isn't a JPanel? Cards has no graphics context.

It doesn't even compile as is.
Was This Post Helpful? 1
  • +
  • -

#5 Omulous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-May 11

Re: Problem: repaint() won't call paintComponent

Posted 30 May 2011 - 09:44 AM

Oh right the card object isnt really used.

// The "FasihAwan_ArrayList" class.
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.ArrayList;
import javax.imageio.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JCheckBox;

public class FA_Card extends JFrame
{
    Cards card;
    Deck dC = new Deck(52);
    
    JPanel content = new JPanel ();
    JPanel north = new JPanel ();
    JPanel flipNotShuffle = new JPanel ();
    JPanel single = new JPanel ();
    JTextField txt = new JTextField (5);
    int chk = 0;
    JButton shuffle = new JButton ("Shuffle");
    JButton deal = new JButton ("Deal");
    JCheckBox dealPOS = new JCheckBox ("Position?");
    JButton add = new JButton ("Add");
    JButton search = new JButton ("Search");
    JButton quicksort = new JButton ("Q-Sort");
    JButton combsort = new JButton ("C-Sort");
    JButton submit = new JButton ("Go");
    JTextField pos = new JTextField (1);
    JPanel north2 = new JPanel();
    JPanel frame = new JPanel();
    JPanel draw = new JPanel();
    JComboBox crd;
    JComboBox suit;
    JComboBox sCrd;
    JComboBox sSuit;
    

    public FA_Card ()
    {

        // Create a content pane
        //card = new Card (250, 500);
        content.setLayout (new BorderLayout ()); // Use FlowLayout for panel
        content.add(north,"West");
        north.setLayout (new FlowLayout ()); // Use FlowLayout for input area
        north2.setLayout (new FlowLayout());
        
        JPanel south = new JPanel ();
        south.setLayout (new FlowLayout ());
        
       
        String[] numbs = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] sts = {"Spades", "Hearts", "Diamons", "Clubs"};

        crd = new JComboBox (numbs);
        suit = new JComboBox (sts);
        sCrd = new JComboBox (numbs);
        sSuit = new JComboBox (sts);
        crd.setEnabled (false);
        suit.setEnabled (false);

        dealPOS.addActionListener (new dP ());
        dealPOS.setEnabled(false);

        JLabel posLabel = new JLabel ("Position:");
       north.add(shuffle);
       north.add(deal);
       north.add (add);
       north.add (crd);
       north.add (suit);
       north.add (posLabel);
       north2.add(search);
       north2.add (quicksort);
       north2.add(sCrd);
       north2.add(sSuit);
       north2.add(submit);
       submit.setEnabled (false);
       sCrd.setEnabled(false);
       sSuit.setEnabled (false);
       
       draw.add(dC, BorderLayout.CENTER);
       dC.show();
       draw.add (dC, BorderLayout.CENTER);
       add.addActionListener(new addAction());
       search.addActionListener (new searchAction());

       dealPOS.setEnabled(false);
       pos.setEditable (false);
        
       submit.addActionListener (new CardAction ());

        
        //north.add (cd, BorderLayout.NORTH);
        north.add (txt, "WEST");
        txt.setEditable(false);
        content.add(draw,BorderLayout.CENTER);
        
        content.add (north, "West"); // Input area        
        content.add (north2, "South");

        setContentPane (content);
        pack ();
        setTitle ("Cards"); //sets title of the program
        setSize (550, 600);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //sets the default close operation to when you press close (X)
        setLocationRelativeTo (null);           // Center window.
        //getContentPane ().add (card, BorderLayout.CENTER);

    } // FasihAwan_ArrayList class


    class addAction implements ActionListener
    {
        public void actionPerformed (ActionEvent s)  //show, shuffle, deal, add, search, quicksort, combsort
        {
           suit.setEnabled(true);
           crd.setEnabled(true);
           sCrd.setEnabled (false);
           sSuit.setEnabled(false);
           submit.setEnabled(true);
           
        }
    }
   /***/ class submitSearch implements ActionListener
    {
    	public void actionPerformed (ActionEvent sS)
    	{//for search Card temp = new Card and then search for that?
    		String str = submit.getText();
    		try
    		{
    			int n = Integer.parseInt(str);
    		}
    		catch (NumberFormatException x)
    		{
    		}    		
    		
    		
    	}
    }
    class searchAction implements ActionListener
    {
    	public void actionPerformed (ActionEvent s)
    	{
    		sCrd.setEnabled(true);
    		sSuit.setEnabled (true);
            suit.setEnabled(false);
            crd.setEnabled(false);
            submit.setEnabled(true);
            
    	}
    }
    class dP implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            boolean dealP = dealPOS.isSelected ();
            if (dealP)
                pos.setEditable (true);
            else if (!dealP)
                pos.setEditable (false);

        }
    }

    class CardAction implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            Deck dC = new Deck (52);
            
            String num =(String) crd.getSelectedItem ();
            String st = (String)suit.getSelectedItem ();
            String str = pos.getText ();
            String fS = st.substring (0, 1);
            fS = fS.toLowerCase ();
            int value = Integer.parseInt (num);
            int deal_POS;
            int addNum = Integer.parseInt (num);
            int suitNum = (st.equals ("Spades") ? 1:st.equals ("Hearts") ? 2:st.equals ("Diamonds") ? 3:4);
            

        }

    }



public static void main (String[] args)
{
    FA_Card win = new FA_Card ();
    win.setVisible (true);
    win.setLocationRelativeTo (null);
}
}
class Deck
{
    String suit[] = {"c", "d", "h", "s"};
    Cards card;
    public ArrayList <Cards> deck = new ArrayList <Cards> ();

    public Deck ()
    {
        deck.size ();
    }


    public Deck (int size)
    {
        for (int y = 1 ; y <= 52 ; y++)
        {
            deck.add (card = new Cards (y));
        }
    }

    public void shuffle ()
    {
        //deck.shuffle ();
    }


    public void deal (int choice)
    {
        card = deck.get (choice); // gets the xth card from the deck
        //display the card at the bottom of the program (stackable)
        deck.remove (choice);
    }


    public void add (int val, int st)
    {
    	int x = Cards.setSuit(suit[st]);
    	card = new Cards(val,x);
        deck.add (card);
    }


    public int search (Cards card)
    {
        if (!deck.contains (card))
            return -1;
        else
            return deck.indexOf (card);
    }
}


class Cards extends JPanel
{
    protected int rank, suit; //suite = c(1),d(2),h(3),s(4)
    protected Image card = null;
    public int x=10,counter = 0,y = 10;


    public Cards (int r, int s)  //given rank and suite
    {
        rank = r;
        suit = s;
        isUp [r * s] = true;
        repaint();
    }


    public Cards (int c)  //given a number
    {
        if (c > 13 && c < 27)
        {
            rank = c - 13;
            suit = 2;
            //isUp [rank * suit] = true;
        }
        else if (c > 26 && c < 40)
        {
            rank = c - 26;
            suit = 3;
            //isUp [rank * suit] = true;
        }
        else if (c > 39)
        {
            rank = c - 39;
            suit = 4;
            //isUp [rank * suit] = true;
        }
        else if (c < 14)
        {
            rank = c;
            suit = 1;
            //isUp [c] = true;
        }
                
    }
    public void show()
    {
    	this.repaint();
    }

    public void paintComponent (Graphics g)
    {
    	super.paintComponent(g);
    	g.setColor (Color.RED);
    	g.fillOval(75, 75, 10, 10);
    	x+=25;
    	if (x == 335)
    	{
    		x= 10;
    		y+= 10;
    	}
    	System.out.print ("CHECK-REPAINTED");
        Image img = loadImage (rank + setSuit (suit) + ".gif");
        g.drawImage (img, 10*x, 2*y, null);
    }


    public int getRank ()
    {
        return rank;
    }


    public int getSuite ()
    {
        return suit;
    }


    public void showCard ()
    {

    }


    public void turnOver ()
    {
        isUp [rank * suit] = true;
    }



    public String setSuit (int num)
    {
        String suit = "";
        if (num == 1)
            suit = "c";
        else if (num == 2)
            suit = "d";
        else if (num == 3)
            suit = "h";
        else if (num == 4)
            suit = "s";
        return suit;
    }
    public static int setSuit (String x)
    {
    	if (x.equals("c"))
    		return 1;
    	else if (x.equals("d"))
    		return 2;
    	else if (x.equals("h"))
    		return 3;
    	else
    		return 4;
    }


    public static Image loadImage (String name)  //Loads Images
    {

        Image img = null;
        try
        {
            img = ImageIO.read (new File (name));
        }
        catch (IOException e)
        {
        }
        return img;
    }
}




Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Problem: repaint() won't call paintComponent

Posted 30 May 2011 - 02:10 PM

So what's wrong with this piece of code? Does your project even compile? As KYA said, you should revisit your design. Why is your Cards class a JPanel? It is good practice to separate your program state/data from your user interface. Have a Card class to model a Card, a Deck class to model a Deck and encapsulate a List<Card>. Then have a Game class to deal with your game play. Your GUI should interact with your Game class to get the data it should populate and to enact changes. Your GUI should NOT manage the program state and data. Let the Game class do this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1