0

I have created a method which fills an array with 52 objects. Each object has a String suit, String face, int value, and a buffered image. Each object gets its image from a larger image and a getSubImage method. I am trying to make a GUI which displays a card from the deck, but I don't know how to reference the objects in the array. The standard array[num] does not work for the ______in the statement

JLabel cardLabel = new JLabel(new ImageIcon(_______.cardImage))

I think I need to call the buffered image by name, but I'm just not sure what to use. Here is my full code (Card_Class is my class for constructing the card objects):

package com.company;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class Deck {

    private Card_Class[] deckOfCards;
    private int currentCard;

    public Deck() throws IOException {

        String[] faces = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
        String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
        deckOfCards = new Card_Class[52];
        currentCard = 0;

        final int width = 10;
        final int height = 17;
        final int rows =4;
        final int columns = 13;

        BufferedImage bigImage = ImageIO.read(new File("AllCards.png"));
        BufferedImage tempCardImage;

        for(int suit=0;suit <4; suit++){

            for(int face=0;face<13;face++){
                tempCardImage = bigImage.getSubimage(
                        face*width+(face*8),
                        suit*height+(suit*14),
                        width,
                        height);
                deckOfCards[(face+(suit*13))] = new Card_Class(suits[suit],faces[face],(13*suit+face),tempCardImage);
            }
        }

    }
    public void displayDeck(){
        for(Card_Class card: deckOfCards)

            System.out.println(card);
    }

    public static void main(String[] args) throws IOException {
        Deck theDeck = new Deck();
        JFrame window = new JFrame("Display for a card object");
        window.setSize(400, 600);
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.setVisible(true);

        JPanel contentPane = new JPanel(new BorderLayout());
        JLabel cardLabel = new JLabel(new ImageIcon(_______.cardImage));
        cardLabel.setSize(300, 400);

        contentPane.add(cardLabel);
        window.add(contentPane);

    }

}
  • `deckOfCards[x].cardImage` – MadProgrammer Nov 08 '17 at 02:40
  • This doesn't work. A non-static field can't be referenced from a static context. –  Nov 08 '17 at 02:54
  • Possible duplicate [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – MadProgrammer Nov 08 '17 at 02:55
  • The main issue is how to access each object and it's image individually without having to call the entire array. –  Nov 08 '17 at 03:00
  • You have the cards in the array already, how else would you access them and why would you bother? – MadProgrammer Nov 08 '17 at 03:06
  • I want to be able to shuffle and deal the deck and I don't know how I can shuffle an array of object. –  Nov 08 '17 at 04:07
  • It’s easy enough to shuffle a collection. [Google](https://www.google.com.au/search?client=safari&rls=en&q=java+shuffle+array&ie=UTF-8&oe=UTF-8&gfe_rd=cr&dcr=0&ei=n4MCWtyQOpDp8wfXuqDoDw) seems to have some ideas on the subject – MadProgrammer Nov 08 '17 at 04:08
  • Now I'm running into an issue where I want to move the card based on its value. However, I'm not able to call on specific cards because they are all in an array. I want to write something that says 'the card with int value 6 swaps positions in the array with the card with int value 8, but I don't know how to index these. –  Nov 11 '17 at 14:49

0 Answers0