Java-从arrray打印图像对象

时间:2017-11-08 02:35:34

标签: java user-interface bufferedimage

我创建了一个用52个对象填充数组的方法。每个对象都有一个String套装,String face,int值和一个缓冲图像。每个对象从较大的图像和getSubImage方法获取其图像。我正在尝试制作一个显示卡片卡的GUI,但我不知道如何引用数组中的对象。标准数组[num]不适用于______语句

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

我想我需要按名称调用缓冲的图像,但我不确定要使用什么。这是我的完整代码(Card_Class是我构建卡片对象的类):

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);

    }

}

0 个答案:

没有答案