Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Likely your image is null, possibly because you're looking in the wrong place for it, but to find out, check out which line is 53. </p> <p>Suggestions:</p> <ul> <li>Start <strong>small</strong> and then build on. Don't try to do complex image manipulation until you first successfully read and show a single image.</li> <li>Check where Java is looking for the image, because likely it isn't where you think it is. Put <code>System.out.println(System.getProperty("user.dir"));</code> in your code to find out.</li> <li>Call <code>setVisible(true)</code> <strong>after</strong> adding all components to the JFrame.</li> <li>Improve your exception display code by printing the stack trace: <code>error.printStackTrace()</code> (thanks Mad!).</li> </ul> <p>For example:</p> <pre><code>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&lt;ImageIcon&gt; 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&lt;ImageIcon&gt; 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 &lt; 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 &amp;&amp; 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&lt;ImageIcon&gt; createCardIconList(String pathToDeck) throws MalformedURLException, IOException { BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck)); int width = fullDeckImg.getWidth(); int height = fullDeckImg.getHeight(); List&lt;ImageIcon&gt; iconList = new ArrayList&lt;ImageIcon&gt;(); for (int suit = 0; suit &lt; SUIT_COUNT; suit++) { for (int rank = 0; rank &lt; 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; } } </code></pre> <p>Which shows:<br> <img src="https://i.stack.imgur.com/FKwLY.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload