准备做个扑克牌游戏,想先把所有的图片都加载进来,
我想在指定位置上画出图片,看了下API,ImageIcon里有个方法,paintIcon(Component, g, x, y);
自己想试一下,结果没有图片
突然发觉自己很多东西不会,
代码如下,欢迎拍砖package image;import javax.swing.*;
import java.awt.*;public class Image {
private static ImageIcon CardImage[] = new ImageIcon[58];  // Include four back of card image

/** Get the card image */
public static void loadImage(){
for(int i = 0; i < CardImage.length - 4; i++){
CardImage[i]  = new ImageIcon("card/" + i + ".png");
}

/** Load the four back of card */
CardImage[54] = new ImageIcon("card/b1fh.png");
CardImage[55] = new ImageIcon("card/b1fv.png");
CardImage[56] = new ImageIcon("card/b2fh.png");
CardImage[57] = new ImageIcon("card/b2fv.png");
}

/** Get the array of image label */
public static ImageIcon[] getImage(){
return CardImage;
}

public static void main(String args[]){
JFrame frame = new JFrame();
frame.setSize(500, 400);
frame.setLayout(new GridLayout(1, 1));
loadImage();
ImageIcon jlbImage[] = getImage();
JPanel panel = new Test(jlbImage);
frame.add(panel);

System.out.println(panel.getWidth());
System.out.println(panel.getHeight());


frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class Test extends JPanel{
public Test(ImageIcon[] imageIcon){
repaint();
}

protected void paintComponent(Graphics g, ImageIcon[] imageIcon){
super.paintComponent(g);
drawImage(imageIcon, g);
}

private void drawImage(ImageIcon[] imageIcon, Graphics g){
int xCoordinate = 0;
for(int i = 0; i < imageIcon.length; i++){
JLabel label = new JLabel();
label.setIcon(imageIcon[i]);
imageIcon[i].paintIcon(label, g, xCoordinate, 0);
xCoordinate += 10;
}
}
}