简体   繁体   English

如何将图像添加到图像阵列?

[英]How do I add images to an image array?

I am making an applet that displays a random collection of 10 cards based upon 10 random integers. 我正在制作一个显示基于10个随机整数的10张卡片的随机集合的applet。

My idea was to make an array of the 52 displayable cards (not including jokers) and display each cards from the array based upon the random integer like this (sorry i don't know how to use code blocks): 我的想法是制作一个由52个可显示卡组成的数组(不包括小丑),并基于像这样的随机整数显示数组中的每个卡(对不起,我不知道如何使用代码块):

for (int i = 0; i<cards.length; i++) { //cards being my image array
     //code that displays each image
}

But I ran into trouble trying to add images into the array and I don't know how to display the images from the array either. 但是我在尝试将图像添加到阵列时遇到麻烦,我也不知道如何显示阵列中的图像。

Should I be adding them like this: 我应该这样添加它们吗:

Image[] cards = new Image[52];
cards[0] = c1;   //name of the Ace of Clubs, I had used getImage() to already get it

The preceding statements throw up errors saying it's an illegal start. 前面的语句抛出错误,指出这是一个非法的开始。

I also need help with displaying the images once I incorporate the images since I don't think: 合并图像后,我也需要显示图像的帮助,因为我不认为:

System.out.println(cards[x]);

Will work with images. 将与图像一起使用。

Thanks in advance and sorry for seeming so complicated, I've tried to water it down as much as possible! 在此先感谢您,感谢您看起来如此复杂,我已经尽力减少了!

So, here's my silly take on it... 所以,这是我的愚蠢做法...

在此处输入图片说明

public class RandomCards {
    public static void main(String[] args) {
        new RandomCards();
    }

    public RandomCards() {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                try {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new RandomCardsPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

            }

        });

    }

    public class RandomCardsPane extends JPanel {

        // A list is a collection of Image objects...
        private List<Image> cardList;
        private Image card = null;

        public RandomCardsPane() throws IOException {

            // My cards are stored in the default execution location of the program
            // and are named "Card_1.png" through "Card_51.png"...
            // You image loading process will be different, replace it here..

            // ArrayList is a dynamic list (meaning it can grow and shrink
            // over the life time of the list) and is backed by an array
            // which shouldn't concern you, the only thing you really need to
            // know is that it has excellent random access...
            cardList = new ArrayList<Image>(51);
            for (int index = 0; index < 51; index++) {
                cardList.add(ImageIO.read(new File("Card_" + index + ".png")));
            }

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    card = cardList.get(Math.min((int)Math.round(Math.random() * cardList.size()), 51));
                    repaint();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (card != null) {
                int x = (getWidth() - card.getWidth(this)) / 2;
                int y = (getHeight() - card.getHeight(this)) / 2;
                g.drawImage(card, x, y, this);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(225, 315);
        }
    }
}

I would also prefer ImageIO over Toolkit.getImage or even ImageIcon , apart from the fact that it guarantees the image data is loaded BEFORE the method returns, it also supports a greater number of image formats and is extendable via plugins... 我还宁愿使用ImageIO不是Toolkit.getImage甚至ImageIcon ,除了它保证在方法返回之前加载图像数据外,它还支持更多图像格式,并且可以通过插件扩展...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM