I have this class, Carta, that I use to draw a card. The paint method works well on itself:
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
RoundRectangle2D rect2 = new RoundRectangle2D.Double(posicio.x + CARD_WIDTH + 20,
(posicio.y * CARD_HEIGHT) + 10,
CARD_WIDTH,
CARD_HEIGHT,
CORNER_ANGLE,
CORNER_ANGLE);
g2d.setColor(Color.WHITE);
g2d.fill(rect2);
g2d.setColor(Color.black);
g2d.draw(rect2);
BufferedImage imatge = null;
if (caraAmunt) {
try {
imatge = ImageIO.read(new File(cami));
} catch (IOException ex) {
Logger.getLogger(Carta.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
try {
imatge = ImageIO.read(new File(camiDefecte));
} catch (IOException ex) {
Logger.getLogger(Carta.class.getName()).log(Level.SEVERE, null, ex);
}
}
g.drawImage(imatge.getScaledInstance(CARD_WIDTH, CARD_HEIGHT, Image.SCALE_SMOOTH),
posicio.x + CARD_WIDTH + 20,
(posicio.y * CARD_HEIGHT) + 10,
null);
}
The problem I have is when trying to get to paint it on a JPanel. This is part of a larger GUI that contains a JPanel with a deck of these Cards. When I add them to the JPanel I see nothing on screen. I am instantiating a JFrame in which I have the JPanel to which I add every Card. How can I paint them inside this JPanel, say JPanel foo?
Example code
Carta foo = new Carta(SPADES, ACE, new Point(0, 0), true);
this.add(foo);
I have already solved the problem. I was painting inside the class and that did not make sense. Thanks for the replies :)