Results 1 to 3 of 3
  1. #1
    jsfreem is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Imageicon and getImage() not working with JLabels. Need help debugging

    I have a friends code using the same images with the same paths working. We simply do not understand what is wrong. Thanks for any help.

    Main
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.Color;
    import java.io.File;
    import javax.swing.JLabel;
    
    public class CardFight extends JFrame{
    	public static CardDeck mDeck;
    	public static CardDeck pDeck;
    	public static CardDeck cpuDeck;
    	public static int counter = 1;
    	//Swing
    	public static JFrame mainFrame;
    	public static JPanel panel;
    	public static JLabel pWar1;
    	public static JLabel pWar2;
    	public static JLabel cpuWar1;
    	public static JLabel cpuWar2;
    	public static JLabel PlayerDeck;
    	public static JLabel ComDeck;
    	private static JLabel playerRemaining;
    	private static JLabel cpuRemaining;
    	private static JTextArea text;
    	public static ImageIcon background = new ImageIcon("back.gif");
    	
    	public static void main(String[] args) { //1 open
    		mDeck = new CardDeck();
    		pDeck = new CardDeck();
    		cpuDeck = new CardDeck();
    		mDeck.shuffle();
    	
    		
    		//Swing
    		//mainFrame
    		mainFrame = new JFrame("Card Fight - Jake Freedman, Grant Banko, Chandler Evans");
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH); 
    		mainFrame.pack();
    		mainFrame.setSize(900, 750);
    		mainFrame.setVisible(true);
    		
    		//Creates main panel
    		panel = new JPanel();
    		panel.setLayout(null);
    		mainFrame.add(panel);
    		panel.setBackground(Color.gray);
    		
    		Font font = new Font("Felix Titling", Font.BOLD, 35);
    		JLabel titleLabel = new JLabel("A Classic Game of War");
    		titleLabel.setFont(font);
    		titleLabel.setForeground(Color.white);
    		titleLabel.setBounds(new Rectangle(200, 0, 400, 50));
    		panel.add(titleLabel, null);
    		
    		Font purple = new Font("Felix Tilting", Font.BOLD, 14);
    		Font small = new Font("Felix Tilting", Font.BOLD, 12);
    		
    		// Title for player's deck
    		JLabel playerLabel = new JLabel("Player");
    		playerLabel.setBounds(new Rectangle(132, 75, 100, 30));
    		playerLabel.setFont(purple);
    		playerLabel.setForeground(new Color(148,0,211));
    		panel.add(playerLabel, null);
    
    		// Card slot for player's deck
    		PlayerDeck = new JLabel("Player");
    		PlayerDeck.setBounds(new Rectangle(115, 100, 73, 97));
    		PlayerDeck.setIcon(background);
    		panel.add(PlayerDeck, null);
    
    		// Counter for player's remaining cards
    		playerRemaining = new JLabel("26");
    		playerRemaining.setBounds(new Rectangle(127, 200, 50, 20));
    		playerRemaining.setHorizontalAlignment(SwingConstants.CENTER);
    		playerRemaining.setBorder(BorderFactory.createLineBorder(Color.black));
    		playerRemaining.setFont(purple);
    		playerRemaining.setForeground(new Color(255,20,147));
    		panel.add(playerRemaining, null);
    
    		// Label for player's remaining cards
    		JLabel pRemainingLbl = new JLabel("Cards Remaining");
    		pRemainingLbl.setBounds(new Rectangle(102, 220, 100, 25));
    		pRemainingLbl.setFont(small);
    		pRemainingLbl.setForeground(new Color(148,0,211));
    		panel.add(pRemainingLbl, null);
    
    		// Player's card slot for war
    		pWar1 = new JLabel("");
    		pWar1.setBounds(new Rectangle(265, 50, 73, 97));
    		pWar1.setBackground(Color.black);
    		panel.add(pWar1, null);
    
    		// Another player card slot for war
    		pWar2 = new JLabel("");
    		pWar2.setBounds(new Rectangle(350, 50, 75, 97));
    		pWar2.setBackground(Color.black);
    		panel.add(pWar2, null);
    
    		// Computer deck title
    		JLabel cpuLabel = new JLabel("Computer");
    		cpuLabel.setBounds(new Rectangle(501, 75, 100, 30));
    		cpuLabel.setFont(purple);
    		cpuLabel.setForeground(new Color(148,0,211));
    		panel.add(cpuLabel, null);
    
    		// Card slot for Computer's deck
    		ComDeck = new JLabel("Computer");
    		ComDeck.setBounds(new Rectangle(500, 99, 73, 97));
    		ComDeck.setIcon(background);
    		panel.add(ComDeck, null);
    
    		// Counter for computer's remaining cards
    		cpuRemaining = new JLabel("26");
    		cpuRemaining.setBounds(new Rectangle(513, 200, 50, 20));
    		cpuRemaining.setHorizontalAlignment(SwingConstants.CENTER);
    		cpuRemaining.setBorder(BorderFactory.createLineBorder(Color.black));
    		cpuRemaining.setFont(purple);
    		cpuRemaining.setForeground(new Color(255,20,147));
    		panel.add(cpuRemaining, null);
    
    		// Label for player's remaining cards
    		JLabel cRemainingLbl = new JLabel("Cards Remaining");
    		cRemainingLbl.setBounds(new Rectangle(490, 220, 100, 25));
    		cRemainingLbl.setFont(small);
    		cRemainingLbl.setForeground(new Color(148,0,211));
    		panel.add(cRemainingLbl, null);
    
    		// Computer's card slot for war
    		cpuWar1 = new JLabel("");
    		cpuWar1.setBounds(new Rectangle(265, 150, 73, 97));
    		cpuWar1.setBackground(Color.black);
    		panel.add(cpuWar1, null);
    
    		// Another computer card slot for war
    		cpuWar2 = new JLabel("");
    		cpuWar2.setBounds(new Rectangle(350, 150, 73, 97));
    		cpuWar2.setBackground(Color.black);
    		panel.add(cpuWar2, null);
    		
    		text = new JTextArea();
    		text.setBounds(new Rectangle(50,300,375,100));
    		text.setText("");
    		text.setEditable(false);
    		text.setOpaque(false);
    		panel.add(text, null);
    				
    		//Flip button
    		JButton flip = new JButton("Flip");
    		flip.setBounds(new Rectangle(450, 350, 70, 25));
    		
    		flip.addActionListener(new ActionListener() {  // 1 open
    			public void actionPerformed(ActionEvent e) { // 1 open
    				counter = 2;
    				Card Player = pDeck.peek();
    				Card cpu = cpuDeck.peek();
    				
    			if (counter == 2) {								// 1 open
    				if (pDeck.empty() != true && cpuDeck.empty() != true) { // 1 open
    					Player = pDeck.peek();
    					cpu = cpuDeck.peek();
    					
    					PlayerDeck.setIcon(pDeck.getImage(Player));
    					pWar1.setIcon(null);
    					pWar2.setIcon(null);
    					ComDeck.setIcon(cpuDeck.getImage(cpu));
    					cpuWar1.setIcon(null);
    					cpuWar2.setIcon(null);
    					
    			// If player 1 is less than player 2
    			if (pDeck.peek().getCardNum() < cpuDeck.peek().getCardNum()){
    				// Show player's cards
    				System.out.println("Player 1: "+pDeck.peek());
    				System.out.println("Player 2: "+cpuDeck.peek());
    				System.out.println("Player 2 wins!");
    				
    				Player = pDeck.peek();
    				cpu = cpuDeck.peek();
    				
    				PlayerDeck.setIcon(pDeck.getImage(Player));
    				ComDeck.setIcon(cpuDeck.getImage(cpu));
    				
    				//Insert cards into winners deck
    				cpuDeck.insert(pDeck.peek());
    				cpuDeck.insert(cpuDeck.peek());
    				// Remove cards
    				pDeck.remove();
    				cpuDeck.remove();
    				System.out.println("");
    				System.out.println("Player 1 has "+pDeck.size()+" cards left");
    				System.out.println("Player 2 has "+cpuDeck.size()+" cards left");
    				
    				while (mDeck.size() != 0) {
    					counter = 3;
    				}
    			}
    			else if (pDeck.peek().getCardNum() > cpuDeck.peek().getCardNum()){
    				System.out.println("Player 1: "+pDeck.peek());
    				System.out.println("Player 2: "+cpuDeck.peek());
    				System.out.println("Player 1 wins!");
    				
    				Player = pDeck.peek();
    				cpu = cpuDeck.peek();
    				
    				PlayerDeck.setIcon(pDeck.getImage(Player));
    				ComDeck.setIcon(cpuDeck.getImage(cpu));
    				
    				pDeck.insert(pDeck.peek());
    				pDeck.insert(cpuDeck.peek());
    				pDeck.remove();
    				cpuDeck.remove();
    				System.out.println("");
    				System.out.println("Player 1 has "+pDeck.size()+" cards left");
    				System.out.println("Player 2 has "+cpuDeck.size()+" cards left");
    				
    				while (mDeck.size() != 0) {
    					counter = 3;
    				}
    			}
    			//War
    			else{ // 1 open
    				while (cpuDeck.peek().getCardNum() == pDeck.peek().getCardNum()){ // 1 open
    				System.out.println("Player 1: "+pDeck.peek());
    				System.out.println("Player 2: "+cpuDeck.peek());
    				
    				System.out.println("War!");
    				// Original war cards and 3 face down cards added to mDeck
    				for (int i = 0; i < 4; i++){
    					mDeck.insert(pDeck.peek());
    					mDeck.insert(cpuDeck.peek());
    					pDeck.remove();
    					cpuDeck.remove();
    				}
    				
    				pWar1.setIcon(background);
    				pWar2.setIcon(pDeck.getImage(pDeck.peek()));
    				
    				cpuWar1.setIcon(background);
    				cpuWar2.setIcon(cpuDeck.getImage(cpuDeck.peek()));
    				
    					if (pDeck.peek().getCardNum() > cpuDeck.peek().getCardNum()){
    						System.out.println("Player 1 wins the war!");
    						mDeck.insert(pDeck.peek());
    						mDeck.insert(cpuDeck.peek());
    						pDeck.remove();
    						cpuDeck.remove();
    						for (int i = 0; i < 6; i++){
    							pDeck.insert(mDeck.peek());
    						}
    						
    						// Display decks
    						System.out.println("");
    						System.out.println("Player 1 has "+pDeck.size()+" cards left");
    						System.out.println("Player 2 has "+cpuDeck.size()+" cards left");
    					}
    					else if ((pDeck.peek().getCardNum() < cpuDeck.peek().getCardNum())){
    						System.out.println("Player 2 wins the war!");
    						
    						Player = pDeck.peek();
    						cpu = cpuDeck.peek();
    						
    						mDeck.insert(pDeck.peek());
    						mDeck.insert(cpuDeck.peek());
    						pDeck.remove();
    						cpuDeck.remove();
    						for (int i = 0; i < 6; i++){
    							pDeck.insert(mDeck.peek());
    						}
    						
    						// Display decks
    						System.out.println("");
    						System.out.println("Player 1 has "+pDeck.size()+" cards left");
    						System.out.println("Player 2 has "+cpuDeck.size()+" cards left");
    						counter = 2;
    					}
    				}
    				if (pDeck.empty() == true) {
    					String gameover = "Game Over!";
    					text.setText(gameover);
    					text.setForeground(new Color(148,0,200));
    				}
    				else if (cpuDeck.empty() == true) {
    					String youwin = "You Win!";
    					text.setText(youwin);
    					text.setForeground(new Color(148,0,200));
    				}
    			}
    			
    				}
    				
    			}
    			
    		}
    	});
    		panel.add(flip, null);
    		
    		JButton exit = new JButton("Exit");
    		exit.setBounds(new Rectangle(550, 350, 70, 25));
    		exit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				System.exit(0);
    			}
    		});
    		panel.add(exit, null);
    		
    
    		JButton newgame = new JButton("New Game");
    		newgame.setBounds(new Rectangle(450, 300, 170, 25));
    		newgame.grabFocus();
    		
    		newgame.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				PlayerDeck.setIcon(background);
    				ComDeck.setIcon(background);
    				counter = 1;
    				if (counter == 1) {
    					// Creates 52 card deck of null cards
    					for (int i = 0; i < 52; i++){
    					pDeck.deck[i] = null;
    					cpuDeck.deck[i] = null;
    					}
    					
    					// Distrubutes main deck to player deck and cpu deck
    					for (int i = 0; i <= 25; i++){
    						pDeck.insert(new Card(mDeck.deck[i].getCardNum(), mDeck.deck[i].getCardSuit()));
    						mDeck.deck[i] = null;
    					}
    					
    					for (int i = 26; i < 52; i++){
    						cpuDeck.insert(new Card(mDeck.deck[i].getCardNum(), mDeck.deck[i].getCardSuit()));
    						mDeck.deck[i] = null;
    					}
    					counter = 3;
    					PlayerDeck.setIcon(background);
    					pWar1.setIcon(null);
    					pWar2.setIcon(null);
    					ComDeck.setIcon(background);
    					cpuWar1.setIcon(null);
    					cpuWar2.setIcon(null);
    				}
    			}
    		});
    		panel.add(newgame, null);
    
    		
    		
    }
    
    }
    classes
    Java Code:
    import javax.swing.ImageIcon;
    
    public class CardDeck {
    	Card [] deck;
    	private int front, back;
    	private int count;
    	
    	//Makes array of class Card size 52, main deck
    	public CardDeck() {
    		deck=new Card[52];
    		front=0;
    		back=-1;
    		count=0;
    		
    		//puts all cards into the array
    		int cardCount=0;
    		for (int suit=0; suit<4; suit++) {
    			for (int num=0; num<13; num++) {
    				deck[cardCount]=new Card(num, suit);
    				cardCount++;
    			}
    		}
    	}
    	
    	public ImageIcon getImage(Card crd) {
    		String cardstring = crd.toString();
    		ImageIcon crdIcon = new ImageIcon("/image" + cardstring + ".png");
    		return crdIcon;
    	}
    	
    	//changes the value of the array spots everytime
    	public void shuffle() {
    		for (int i = 0; i < 51; i++) {
                int rand = (int)(Math.random()*(i-1));
                Card tmp = deck[i];
                deck[i] = deck[rand];
                deck[rand] = tmp;
    		}
    	}
    	
    	//displays the card 
    	public void display() {
    		int count=0;
    		while (count<deck.length) {
    			System.out.println(deck[count].toString()+" ");
    			count++;
    		}
    		System.out.println();
    	}
    	
    	//when win you put both cards at back of queue
    	public void insert(Card s) {
    		back++;
    		if (back==deck.length)
    			back=0;
    		deck[back] = s;
    		count++;
    	}
    	
    	//when peek top card it removes it from the queue
    	public void remove() {
    		front++;
    		if (front==deck.length)
    			front=0;
    		count--;
    	}
    	
    	//shows the top card
    	public Card peek() {
    		return deck[front];
    	}
    	
    	//checks if the players deck of cards is empty
    	public boolean empty() {
    		if (count==0) 
    			return true;
    		else 
    			return false;
    		}
    	
    	//keeps track of how many cards are in the players deck
    	public int size() {
    		return count;
    	}
    	}
    Java Code:
    public class Card {
    	private int num;
    	private int suit;
    	
    	//defines the card
    	public Card(int x, int y) {
    		this.num = x;
    		this.suit = y;
    	}
    	
    	//print out cards
    	public void display(Card card1, Card card2) {
    		System.out.println(card1);
    		System.out.println(card2);
    	}
    	
    	//gets card value and sets it to numerical value
    	public int getCardNum() {
    		if (getNum()=="2") return 0;
    		else if (getNum()=="3") return 1;
    		else if (getNum()=="4") return 2;
    		else if (getNum()=="5") return 3;
    		else if (getNum()=="6") return 4;
    		else if (getNum()=="7") return 5;
    		else if (getNum()=="8") return 6;
    		else if (getNum()=="9") return 7;
    		else if (getNum()=="10") return 8;
    		else if (getNum()=="J") return 9;
    		else if (getNum()=="Q") return 10;
    		else if (getNum()=="K") return 11;
    		else 
    			return 12;
    		}
    	
    	//gets numerical value and sets it to card value
    	public String getNum() {
    		if (num==0) return "2";
    		else if (num==1) return "3";
    		else if (num==2) return "4";
    		else if (num==3) return "5";
    		else if (num==4) return "6";
    		else if (num==5) return "7";
    		else if (num==6) return "8";
    		else if (num==7) return "9";
    		else if (num==8) return "10";
    		else if (num==9) return "J";
    		else if (num==10) return "Q";
    		else if (num==11) return "K";
    		else 
    			return "A";
    	}
    	
    	//gets numerical value and sets it to suit of card
    	public String getSuit() {
    		if (suit==0) return "Heart";
    		else if(suit==1) return "Spade";
    		else if (suit==2) return "Diamond";
    		else if (suit==3) return "Club";
    		else 
    			return "Not Valid";
    	}
    	
    	//returns suit value
    	public int getCardSuit() {
    		return suit;
    	}
    	
    	//returns the whole card
    	public String toString() {
    		return getNum()+" "+getSuit();
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: Imageicon and getImage() not working with JLabels. Need help debugging

    And your error is?
    If it's an exception then please provide the full exception including stack trace, highlighting the line(s) in your code it occurs on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jsfreem is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Imageicon and getImage() not working with JLabels. Need help debugging

    There was no error. Simply the JLabels were not displaying the ImageIcon. I found the problem, which shouldn't have been a problem, in using shortened img paths instead of full. I figured it would look in the project file where I placed the image, but I guess not.

Similar Threads

  1. ImageIcon not working. HELP!
    By Prime624 in forum NetBeans
    Replies: 4
    Last Post: 02-09-2013, 09:05 AM
  2. SWTResourceManager.getImage: Error getting image
    By shay2 in forum SWT / JFace
    Replies: 0
    Last Post: 06-26-2012, 12:38 PM
  3. Japplet.getImage(): What if image file does not exist?
    By CoderMan in forum Java Applets
    Replies: 0
    Last Post: 02-12-2010, 12:31 AM
  4. Replies: 0
    Last Post: 11-13-2009, 04:18 PM
  5. getImage - not fail when image doesn't exist
    By leonid.ge in forum Java Applets
    Replies: 0
    Last Post: 07-24-2009, 04:17 PM

Posting Permissions

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