有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将图像添加到图像数组?

我正在制作一个小程序,根据10个随机整数显示10张卡片的随机集合

我的想法是制作一个包含52张可显示卡片(不包括小丑)的数组,并根据这样的随机整数显示数组中的每张卡片(对不起,我不知道如何使用代码块):

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

但我在尝试将图像添加到阵列时遇到了麻烦,我也不知道如何显示阵列中的图像

我是否应该这样添加它们:

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

前面的语句抛出错误,说这是一个非法的开始

我还需要在合并图像后显示图像的帮助,因为我不认为:

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

将处理图像

提前谢谢,很抱歉看起来这么复杂,我已经尽可能地淡化了


共 (1) 个答案

  1. # 1 楼答案

    所以,这是我愚蠢的想法

    enter image description here

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

    我也更喜欢ImageIO而不是Toolkit.getImage甚至ImageIcon,除了它保证在方法返回之前加载图像数据之外,它还支持更多的图像格式,并且可以通过插件进行扩展