2

When I run my code it doesn't show up. Basically I have a custom Jcomponent which I add to my JFrame or View and then create a View that makes the frame in my main method. I already added to JFrame here is my code for the JComponent:

public class CardDisplay extends JComponent {
private Card card;
private Image cardImage;

public CardDisplay()
{
    cardImage = Toolkit.getDefaultToolkit().createImage(("Phase10//res//Blue2.png"));
}

@Override
public void paint(Graphics g)
{
    g.drawImage(cardImage, 125 ,200, this);
}
public class View {
public View(){

}

public void makeFrame()
{
   JFrame frame = new JFrame("Phase 10");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new BorderLayout());
   JPanel handPanel = new JPanel();
   CardDisplay cd = new CardDisplay();
   handPanel.setLayout(new FlowLayout());
   frame.add(handPanel, BorderLayout.SOUTH);
   handPanel.add(cd);
   frame.pack();
   frame.setSize(600,500);
   frame.setResizable(false);
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
}

public static void main(String[] args){
    View view = new View();
    Game game = new Game();
    view.makeFrame();
    //game.run();

}
Rendition
  • 77
  • 9
  • see my post http://stackoverflow.com/a/8422427/1007845 – Adrian Mar 06 '12 at 16:54
  • This is probably the problem in eventQueue when you starting your app. Add the lines of code which start application and creates CardDisplay. – Serhiy Mar 06 '12 at 16:54
  • @Adrian - Tried that but it still does not work – Rendition Mar 06 '12 at 17:17
  • @Rendition try my other post on the same thread; I put it the entire code. It's probably paintComponent(..) and perhaps the way you add the component to your panels. – Adrian Mar 06 '12 at 18:07

2 Answers2

3

Here is the working version. The problem was mainly related to the preferred size of the component. Please note the implementation of method getPreferredSize().

If you would like to see what are the component boundaries I'd recommend using MigLayout layout manager in debug mode (the site has all necessary documentation).

public class CardDisplay extends JComponent {
    private BufferedImage cardImage;

    public CardDisplay() {
        try {
            cardImage = ImageIO.read(new File("Phase10//res//Blue2.png"));
        } catch (final IOException e) {
            e.printStackTrace();
        }    
    }

    @Override
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.drawImage(cardImage, 0, 0, null);
    }

    @Override
    public Dimension getPreferredSize() {
        if (cardImage == null) {
            return new Dimension(100, 100);
        } else {
            return new Dimension(cardImage.getWidth(null), cardImage.getHeight(null));
        }
    }

    public static class View {
        public View() {}

        public void makeFrame() {
            final JFrame frame = new JFrame("Phase 10");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            final JPanel handPanel = new JPanel();
            final CardDisplay cd = new CardDisplay();
            handPanel.setLayout(new FlowLayout());
            frame.add(handPanel, BorderLayout.SOUTH);
            handPanel.add(cd);
            frame.pack();
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }

    public static void main(final String[] args) {
        final View view = new View();
        view.makeFrame();
    }
}
01es
  • 5,362
  • 1
  • 31
  • 40
  • I implemented your code and it sends an Error stating it can't read the input file? I tried using ImageIO.read before i posted this and got the same thing. I've tried moving the file into the same folder as the Class to make sure the file extension was not a problem. – Rendition Mar 06 '12 at 18:58
  • In a local project that I've have created for this code, it was simply "image.jpg", where the actual file was located at the base of the project. – 01es Mar 06 '12 at 19:04
  • what is final BufferedImage img = null; for? – Rendition Mar 06 '12 at 19:10
  • @Rendition not needed -- just leftovers from when I was prototyping the example. – 01es Mar 06 '12 at 19:19
  • Okay well i've tried replacing the Image and it still cannot read the file. I've copied the exact code this time two... If it changes anything I'm working on a mac and my IDE is NetBeans – Rendition Mar 06 '12 at 19:25
  • hm... try cardImage = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("logo.jpg")); where logo.jpg is on the project's classpath. – 01es Mar 06 '12 at 19:26
  • Now I'm geting IllegalArgumentException : Input = null! – Rendition Mar 06 '12 at 19:32
  • This means that the image file is not on classpath and, thus it is not loaded. – 01es Mar 06 '12 at 19:38
  • :( but its in the same folder as the classes therefore the path should just be like "Blue2.jpg"? Edit: I got it to finally work but I have to put the Full path from /Users/user/Desktop/..../Blue2.jpg... really annoying TY so much for the help! – Rendition Mar 06 '12 at 19:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8596/discussion-between-01es-and-rendition) – 01es Mar 06 '12 at 19:48
  • 1
    `g.drawImage(cardImage, 0, 0, null);` should be `g.drawImage(cardImage, 0, 0, this);`. – Andrew Thompson Mar 07 '12 at 05:15
2

For JComponents, you use paintComponent instead of paint. Paint is actually used to draw components while paintComponent is used to draw images.

Shawn Shroyer
  • 901
  • 1
  • 8
  • 18