*Edit -- Essentially, instead of displaying an Image as an Icon in a label, I think we are supposed to just implement the display() method from the Displayable interface in the card class (as done), then invoke the setItem() and paint() methods of CardGamePanel to draw the card within a panel instance - this is where I'm struggling.
Sorry if that was unclear.* Though getting the label to display in my panel would be a breakthrough also.
Hi, I'm having problems displaying a card image (which I have added to a Label as an Icon) in my application. First of all I will post the relevant classes/parts of that may be relevant to the problems. May look longer than it is but it's quite simple.
Displayable Interface
package displayableCard;
import java.awt.*;
import javax.swing.*;
public interface Displayable {
void display (Graphics g, int x, int y);
}
CardGamePanel
package displayableCard;
import javax.swing.*;
import java.awt.*;
public class CardGamePanel extends JPanel{
private Displayable theItem;
private int x, y;
public void paint(Graphics g) {
if (theItem != null)
theItem.display(g, x, y);
}
public void setItem(Displayable item, int x, int y) {
theItem = item;
this.x = x;
this.y = x;
}
}
DisplayableCard
package displayableCard;
import java.awt.*;
import javax.swing.*;
public class DisplayableCard extends EnumCard implements Displayable{
private Rank rank;
private Suit suit;
private Image image;
public DisplayableCard(Rank theRank)
{
this(theRank, Suit.HEARTS);
image = new ImageIcon("....\\classic-cards\\" + rank + suit + ".png").getImage(); //shortened for brevity
}
public DisplayableCard(Rank theRank, Suit theSuit)
{
//.......
}
public Image getImage()
{
return image;
}
public void display(Graphics g, int x, int y)
{
g.drawImage(image, 0, 0, null); //implements interface
}
}
Main
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
Graphics g;
JLabel cardLabel;
CardGamePanel cardPanel;
public Main() {
DisplayableCard card1 = new DisplayableCard(Rank.ACE);
cardPanel = new CardGamePanel();
cardLabel = new JLabel(new ImageIcon(card1.getImage()));
//cardPanel.setItem(card1, 0, 0); //when used breaks with nullpointer
cardPanel.add(cardLabel);
cardPanel.paint(g); //unsure if this is even doing anything
getContentPane().add(cardPanel, BorderLayout.CENTER); //this version displays a blank panel
//getContentPane().add(cardLabel, BorderLayout.CENTER); //this version displays a label with the image
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
I've sorted out a couple of problems now, changing getContentPane().add(cardPanel, FlowLayout.CENTER) to BorderLayout. Whatever issue that was causing the frame now displays, but without graphics.
I then tested using getContentPane().add(cardLabel, BorderLayout.CENTER), and in this case it actually displays the correct Image, so thanks for the help on displaying images on a label in the other thread.
My only {color:#ff9900}*
problem now is getting the label to display when I add it to cardPanel*{color}. I checked using setVisible(true) just to make sure, and still get a blank panel(grey). This is the last problem I have I think and perhaps the biggest. When attempting to invoke the setItem(Displayable item, int x, int y) method from CardGamePanel I once again get a null pointer exception, but can not see anywhere in my program that I have missed any necessary instantiations of objects.
Any idea what this final problem might be?
I think the problem is when invoking setItem() from CardGamePanel on my instance cardPanel. This is where I pass the object that needs to be drawn I think. But every time I invoke it I get null pointer and so this has a knock on effect of the paint() method not working as I can't pass an item in for the paint() method to display.