Results 1 to 4 of 4
  1. #1
    Catkill is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default Repaint() not working

    I have a program which needs to change the Image of 7 seperate JLabels (all placed on a single Panel) shown here:

    Java Code:
    JPanel the_Cards = new JPanel();
    		the_Cards.setLayout(new BoxLayout(the_Cards,BoxLayout.LINE_AXIS));
    		
    		the_Cards.add(Box.createRigidArea(new Dimension(20,0)));
    		the_Cards.add(Cards_picked[0]);
    		the_Cards.add(Cards_picked[1]);
    		the_Cards.add(Box.createRigidArea(new Dimension(30,0)));
    		the_Cards.add(Cards_picked[2]);
    		the_Cards.add(Cards_picked[3]);
    		the_Cards.add(Cards_picked[4]);
    		the_Cards.add(Box.createRigidArea(new Dimension(30,0)));
    		the_Cards.add(Cards_picked[5]);
    		the_Cards.add(Box.createRigidArea(new Dimension(25,0)));
    		the_Cards.add(Cards_picked[6]);
    		the_Cards.add(Box.createRigidArea(new Dimension(20,0)));
    		
    		
    		totalGUI.add(the_Cards);


    The JLables are stored in an array(Card_picked) and the icon is changed via:

    Java Code:
     public void UpdateCards(String number,String suit){
    		 
    		 int position = Integer.parseInt(number);
    		 
    		 position = position - 2;
    		
    		 if(suit.equals("s")){
    			 ImageIcon picture = new ImageIcon(Spades_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 else if(suit.equals("h")){
    			 ImageIcon picture = new ImageIcon(Hearts_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 else if(suit.equals("c")){
    			 ImageIcon picture = new ImageIcon(Clubs_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 else if(suit.equals("d")){
    			 ImageIcon picture = new ImageIcon(Diamonds_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 
    		 counter ++;
    this works for the first Label but once that funtion is called the rest disapear and clickng another button doesnt show the labels calling repaint does not help this problem either.

    Any ideas?

    thanks
    Catkill

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Catkill is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Here the full code :

    Java Code:
    package version1;
    
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Odds_View {
    
    	private String[] table_stakes = {"20-200","200-2000","2000-20000","20000-200000"};
    	private JComboBox table_chooser;
    	
    	public JLabel[] Cards_picked = new JLabel[7];
    	
    	ImageIcon holder = new ImageIcon("CardHolder.jpg");
    	
    	private Image[] Spades_Image = new Image[13];
    	private Image[] Hearts_Image = new Image[13];
    	private Image[] Clubs_Image = new Image[13];
    	private Image[] Diamonds_Image = new Image[13];
    	
    	private CardButton[] Spades_Barray = new CardButton[13];
    	private CardButton[] Hearts_Barray = new CardButton[13];
    	private CardButton[] Clubs_Barray = new CardButton[13];
    	private CardButton[] Diamonds_Barray = new CardButton[13];
    	
    	private String[] Card_Names_Spades = {"2s","3s","4s","5s","6s","7s","8s","9s","10s","11s","12s","13s","14s"};
    	private String[] Card_Names_Hearts = {"2h","3h","4h","5h","6h","7h","8h","9h","10h","11h","12h","13h","14h"};
    	private String[] Card_Names_Clubs = {"2c","3c","4c","5c","6c","7c","8c","9c","10c","11c","12c","13c","14c"};
    	private String[] Card_Names_Diamonds = {"2d","3d","4d","5d","6d","7d","8d","9d","10d","11d","12d","13d","14d"};
    	
    	public int counter = 0;
    	
    	public Odds_View(){
    		createAndShowGUI();
    	}
    	
    	private JPanel createContentPane(){
    		JPanel totalGUI = new JPanel();
    		
    		totalGUI.setLayout((new BoxLayout(totalGUI,BoxLayout.PAGE_AXIS)));
    		
    		//Table Selector
    		
    		JPanel table_selector = new JPanel();
    		table_selector.setLayout(new BoxLayout(table_selector,BoxLayout.LINE_AXIS));
    		
    		
    		table_selector.add(new JLabel("Please select your table stakes: "));
    		table_chooser = new JComboBox(table_stakes);
    		table_chooser.setSelectedIndex(0);
    		table_selector.add(table_chooser);
    		
    		totalGUI.add(table_selector);
    		totalGUI.add(Box.createRigidArea(new Dimension(0,20)));
    		
    		///////////////////
    		
    		//Place to hold the Cards chosen
    		JPanel Cards_Selected = new JPanel();
    		Cards_Selected.setLayout(new BoxLayout(Cards_Selected,BoxLayout.LINE_AXIS));
    		Cards_Selected.add(new JLabel("Pocket Cards: "));
    		Cards_Selected.add(Box.createRigidArea(new Dimension(60,0)));
    		Cards_Selected.add(new JLabel("Flop: "));
    		Cards_Selected.add(Box.createRigidArea(new Dimension(70,0)));
    		Cards_Selected.add(new JLabel("Turn: "));
    		Cards_Selected.add(Box.createRigidArea(new Dimension(30,0)));
    		Cards_Selected.add(new JLabel("River: "));
    		///////////////////////
    		
    		totalGUI.add(Cards_Selected);
    		
    		totalGUI.add(Box.createRigidArea(new Dimension(0,10)));
    		
    	    //PlaceHolders for the Cards
    		//ImageIcon holder = new ImageIcon("CardHolder.jpg");
    		
    		for (int i = 0; i < 7; i ++){
    			Cards_picked[i] = new JLabel();
    			Cards_picked[i].setIcon(holder);
    		}
    		JPanel the_Cards = new JPanel();
    		the_Cards.setLayout(new BoxLayout(the_Cards,BoxLayout.LINE_AXIS));
    		
    		the_Cards.add(Box.createRigidArea(new Dimension(20,0)));
    		the_Cards.add(Cards_picked[0]);
    		the_Cards.add(Cards_picked[1]);
    		the_Cards.add(Box.createRigidArea(new Dimension(30,0)));
    		the_Cards.add(Cards_picked[2]);
    		the_Cards.add(Cards_picked[3]);
    		the_Cards.add(Cards_picked[4]);
    		the_Cards.add(Box.createRigidArea(new Dimension(30,0)));
    		the_Cards.add(Cards_picked[5]);
    		the_Cards.add(Box.createRigidArea(new Dimension(25,0)));
    		the_Cards.add(Cards_picked[6]);
    		the_Cards.add(Box.createRigidArea(new Dimension(20,0)));
    		
    		
    		totalGUI.add(the_Cards);
    		///////////////////////////////
    		
    		//Card selector---------------------
    		JPanel label = new JPanel();
    		label.add(new JLabel("Card Selector:"));
    		totalGUI.add(label);
    		//---------------------------------
    		JPanel Card_Selector = new JPanel(new GridLayout(4,13));
    		
            int x1 = 0,x2 = 30, y1 = 0, y2 = 40;
    		
    		for (int i = 0; i < 13; i ++){
    			Spades_Image[i] = (ImageLoader.get().getCard("Spades.jpg", 30, 40, x1, x2, y1, y2, i));
    			Spades_Barray[i] = new CardButton(Spades_Image[i]);
    			
    			Card_Selector.add(Spades_Barray[i]);
    			
    			System.out.println("Image " + i + " Loaded");
    			
    			x1 = x1 + 30;
    			x2 = x2 + 30;
    		}
    		
    	    x1 = 0;x2 = 30; y1 = 0; y2 = 40;
    	    
    	    for (int i = 0; i < 13; i ++){
    			Hearts_Image[i] = (ImageLoader.get().getCard("Hearts.jpg", 30, 40, x1, x2, y1, y2, i));
    			Hearts_Barray[i] = new CardButton(Hearts_Image[i]);
    			
    			Card_Selector.add(Hearts_Barray[i]);
    			
    			System.out.println("Image " + i + " Loaded");
    			
    			x1 = x1 + 30;
    			x2 = x2 + 30;
    		}
    		
    	    x1 = 0;x2 = 30; y1 = 0; y2 = 40;
    	    
    	    for (int i = 0; i < 13; i ++){
    	    	Clubs_Image[i] = (ImageLoader.get().getCard("Clubs.jpg", 30, 40, x1, x2, y1, y2, i));
    	    	Clubs_Barray[i] = new CardButton(Clubs_Image[i]);
    			
    			Card_Selector.add(Clubs_Barray[i]);
    			
    			System.out.println("Image " + i + " Loaded");
    			
    			x1 = x1 + 30;
    			x2 = x2 + 30;
    		}
    		
    	    x1 = 0;x2 = 30; y1 = 0; y2 = 40;
    	    
    	    for (int i = 0; i < 13; i ++){
    	    	Diamonds_Image[i] = (ImageLoader.get().getCard("Diamonds.jpg", 30, 40, x1, x2, y1, y2, i));
    	    	Diamonds_Barray[i] = new CardButton(Diamonds_Image[i]);
    			
    			Card_Selector.add(Diamonds_Barray[i]);
    			
    			System.out.println("Image " + i + " Loaded");
    			
    			x1 = x1 + 30;
    			x2 = x2 + 30;
    		}
    	    
    	    
    	    totalGUI.add(Card_Selector);
    		/////////////////////////
    	    
    	  //Probability-------------------------------
    		JPanel Prob = new JPanel();
    		
    		Prob.add(new JLabel("Chances of Winning: "));
    		JLabel Probability = new JLabel("NULL");
    		Prob.add(Probability);
    		
    		
    		totalGUI.add(Prob);
    		//------------------------------------------
    		
    		//Sugestion--------------------------------
    		JPanel Suggest = new JPanel();
    		Suggest.add(new JLabel("Suggestion: "));
    		JLabel Suggestion = new JLabel("NULL");
    		Suggest.add(Suggestion);
    		
    		totalGUI.add(Suggest);
    		//------------------------------------------
    	    
    	    
    	    
    		totalGUI.setOpaque(true);
    		return totalGUI;
    	}
    	
    	public void createAndShowGUI(){
    		JFrame frame = new JFrame ("Calculator");
    		frame.setContentPane(createContentPane());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	 public void buttonActionListeners(ActionListener al)
    	    {
    	        /*for(int i = 0; i < button_name.length; i++)
    	        {
    	            button_array[i].setActionCommand(button_name[i]);
    	            button_array[i].addActionListener(al);
    	        }
    	        */
    		 	for (int i = 0; i < Card_Names_Spades.length; i ++){
    		 		   Spades_Barray[i].setActionCommand(Card_Names_Spades[i]);
    				   Spades_Barray[i].addActionListener(al);
    				   Hearts_Barray[i].setActionCommand(Card_Names_Hearts[i]);
    				   Hearts_Barray[i].addActionListener(al);
    				   Clubs_Barray[i].setActionCommand(Card_Names_Clubs[i]);
    				   Clubs_Barray[i].addActionListener(al);
    				   Diamonds_Barray[i].setActionCommand(Card_Names_Diamonds[i]);
    				   Diamonds_Barray[i].addActionListener(al);
    		 	}
    		 
    	    }
    
    	 public void UpdateCards(String number,String suit){
    		 
    		 int position = Integer.parseInt(number);
    		 
    		 position = position - 2;
    		
    		 if(suit.equals("s")){
    			 ImageIcon picture = new ImageIcon(Spades_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 else if(suit.equals("h")){
    			 ImageIcon picture = new ImageIcon(Hearts_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 else if(suit.equals("c")){
    			 ImageIcon picture = new ImageIcon(Clubs_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 else if(suit.equals("d")){
    			 ImageIcon picture = new ImageIcon(Diamonds_Image[position]);
    			 Cards_picked[counter].setIcon(picture);
    		 }
    		 
    		 counter ++;
    		 
    		 
    		 
    		 
    		 
    	 }
    	
    }
    Last edited by Catkill; 09-07-2009 at 02:07 AM.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    As posted, the program does not call updateCards(), so that method cannot fail ;-) The icons will certainly not change.

    Note that updateCards only updates a single card each time it is called.

    It is a very strange and error-prone style to have counter as a global variable that is modified inside updateCards. Almost always it is a good rule to initialize and increment a counter in the same method, preferably with a for statement.
    Last edited by zweibieren; 09-09-2009 at 10:53 PM. Reason: add note about updateCards

Similar Threads

  1. repaint() problems
    By Emily1100125 in forum AWT / Swing
    Replies: 5
    Last Post: 02-03-2009, 04:11 PM
  2. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 12:10 AM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •