2013-09-25 55 views
-1

這是我的第一個Java GUI程序,真的只是我的第二個java程序,所以你要對我容易:)我的程序是很多谷歌上搜索和閱讀Java文檔的結果。我的問題是,我有一張52張精靈片,並試圖使用subImage將這些卡分別保存到緩衝圖像陣列中,僅用於測試目的,在窗口中顯示所有52個圖像。該文件是在我確信的那個正確的目錄中。我相信我的問題在於我使用Jlabels,或者僅僅是一個愚蠢的錯誤。不管怎麼說,這是我的類,它的精靈表拆分的JLabel將不顯示圖像 - NullPointerException異常

package gui; 

import java.awt.GridLayout; 
import java.awt.image.BufferedImage; 
import java.io.File; 


import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class crdimgs extends JPanel {/** 
* 
*/ 
    static final long serialVersionUID = 1L; 
    public final int width = 10; 
    public final int height = 20; 
    public int rows = 13; 
    public int cols = 5; 

    public BufferedImage image; 
    File cardimg = new File("Cards.jpg"); 
    BufferedImage cards[]; 

    public void loadsplit(File loadimage){ 

     try{ 
      image = ImageIO.read(loadimage); 

      } catch(Exception error){ 
       System.out.print("error"); 
       } 


     cards = new BufferedImage[cols*rows]; 

    } 

    public crdimgs() { 
     loadsplit(cardimg); 
     setLayout(new GridLayout(rows, cols, 1, 1)); 

     int x = 0; 
     int y = 0; 
     int subimg = 0; 

     for(int i = 0; i < rows; i++) 
     { 
      JPanel panel = new JPanel(); 
      cards[subimg] = new BufferedImage(width, height, 5); 
      cards[subimg] = image.getSubimage(x, y, width, height); 
      panel.add(new JLabel(new ImageIcon(cards[subimg]))); 
      add(panel); 
      x+=width; 
      subimg++; 
     } 
     y+=height; 
     x=0; 
     } 
    } 
} 

而我的主類

package gui; 
import javax.swing.JFrame; 
import java.awt.Color; 


public class cards extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public cards(){ 

     setTitle("Poker"); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(1000, 700); 
     setLocationRelativeTo(null); 
     this.getContentPane().setBackground(Color.GREEN); 
     setVisible(true); 
     setResizable(false); 
     add(new crdimgs()); 

    } 

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

錯誤我收到的那一刻是:

errorException in thread "main" java.lang.NullPointerException 
at gui.crdimgs.<init>(crdimgs.java:53) 
at gui.cards.<init>(cards.java:22) 
at gui.cards.main(cards.java:28) 
+0

注意在'Exception'第一線...'errorException線程 「main」 java.lang.NullPointerException'它以'error'開頭,它來自'ImageIO.read'語句。嘗試讀取文件時發生錯誤。繼氣墊船的建議 – MadProgrammer

回答

4

可能你的圖像爲null ,可能是因爲你在錯誤的地方看它,但要找出,檢查出哪一行是53.

意見建議:

  • 開始然後建立。在您首次成功讀取並顯示單個圖像之前,不要嘗試執行復雜的圖像處理。
  • 檢查Java正在查找圖像的位置,因爲它可能不是您認爲它的位置。把System.out.println(System.getProperty("user.dir"));放在你的代碼中找出來。
  • 呼叫setVisible(true)加入到JFrame所有組件。
  • 改善打印棧跟蹤你的異常顯示的代碼:(!感謝MAD)error.printStackTrace()

例如:

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

public class PlayingCardTest { 


    public static void main(String[] args) { 
     String pathToDeck = "http://www.jfitz.com/cards/classic-playing-cards.png"; 
     try { 
     final List<ImageIcon> cardImgList = CreateCards.createCardIconList(pathToDeck); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new JFrame("Moving Cards"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new CardGameTable(cardImgList, frame)); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } 
    } 
} 

@SuppressWarnings("serial") 
class CardGameTable extends JLayeredPane { 

    private static final int PREF_W = 600; 
    private static final int PREF_H = 400; 
    private static final Color BASE_COLOR = new Color(0, 80, 0); 
    private static final int CARD_COUNT = 20; 
    private static final int WIDTH_SHOWING = 20; 

    private JPanel basePane = new JPanel(null); 

    public CardGameTable(List<ImageIcon> cardImgList, final JFrame frame) { 
     basePane.setSize(getPreferredSize()); 
     basePane.setBackground(BASE_COLOR); 
     add(basePane, JLayeredPane.DEFAULT_LAYER); 

     final MyMouseAdapter myMouseAdapter = new MyMouseAdapter(this, basePane); 
     addMouseListener(myMouseAdapter); 
     addMouseMotionListener(myMouseAdapter); 

     for (int i = 0; i < CARD_COUNT; i++) { 
     JLabel card = new JLabel(cardImgList.remove(0)); 
     card.setSize(card.getPreferredSize()); 
     int x = (PREF_W/2) + WIDTH_SHOWING * (CARD_COUNT - 2 * i)/2 - 
       card.getPreferredSize().width/2; 
     int y = PREF_H - card.getPreferredSize().height - WIDTH_SHOWING * 2; 
     card.setLocation(x, y); 
     basePane.add(card); 
     } 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

} 

class MyMouseAdapter extends MouseAdapter { 
    private JLabel selectedCard = null; 
    private JLayeredPane cardGameTable = null; 
    private JPanel basePane = null; 
    private int deltaX = 0; 
    private int deltaY = 0; 

    public MyMouseAdapter(JLayeredPane gameTable, JPanel basePane) { 
     this.cardGameTable = gameTable; 
     this.basePane = basePane; 
    } 

    @Override 
    public void mousePressed(MouseEvent mEvt) { 
     Component comp = basePane.getComponentAt(mEvt.getPoint()); 
     if (comp != null && comp instanceof JLabel) { 
     selectedCard = (JLabel) comp; 
     basePane.remove(selectedCard); 
     basePane.revalidate(); 
     basePane.repaint(); 

     cardGameTable.add(selectedCard, JLayeredPane.DRAG_LAYER); 
     cardGameTable.revalidate(); 
     cardGameTable.repaint(); 
     deltaX = mEvt.getX() - selectedCard.getX(); 
     deltaY = mEvt.getY() - selectedCard.getY(); 
     } 
    } 

    @Override 
    public void mouseReleased(MouseEvent mEvt) { 
     if (selectedCard != null) { 
     cardGameTable.remove(selectedCard); 
     cardGameTable.revalidate(); 
     cardGameTable.repaint(); 

     basePane.add(selectedCard, 0); 
     basePane.revalidate(); 
     basePane.repaint(); 
     selectedCard = null; 
     } 
    } 

    @Override 
    public void mouseDragged(MouseEvent mEvt) { 
     if (selectedCard != null) { 
     int x = mEvt.getX() - deltaX; 
     int y = mEvt.getY() - deltaY; 
     selectedCard.setLocation(x, y); 
     cardGameTable.revalidate(); 
     cardGameTable.repaint(); 
     } 
    } 
} 

class CreateCards { 
    private static final int SUIT_COUNT = 4; 
    private static final int RANK_COUNT = 13; 

    public static List<ImageIcon> createCardIconList(String pathToDeck) 
     throws MalformedURLException, IOException { 
     BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck)); 
     int width = fullDeckImg.getWidth(); 
     int height = fullDeckImg.getHeight(); 
     List<ImageIcon> iconList = new ArrayList<ImageIcon>(); 

     for (int suit = 0; suit < SUIT_COUNT; suit++) { 
     for (int rank = 0; rank < RANK_COUNT; rank++) { 
      int x = (rank * width)/RANK_COUNT; 
      int y = (suit * height)/SUIT_COUNT; 
      int w = width/RANK_COUNT; 
      int h = height/SUIT_COUNT; 
      BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h); 
      iconList.add(new ImageIcon(cardImg)); 
     } 
     } 
     Collections.shuffle(iconList); 
     return iconList; 
    } 
} 

其中顯示:
enter image description here

+0

此外,而不是打印是System.out.print'(「錯誤」);'發生異常時,使用'error.printStackTrace();'和/或重新拋出異常 – MadProgrammer

+0

@MadProgrammer:好點, 謝謝! –

+1

它是你的回答,讓我看到了「錯誤」的前綴輸出 – MadProgrammer