日期:2014-05-20 浏览次数:20659 次
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; } } }