I'm designing a card game and I want to draw (paint) a card onto a panel. When I paint, though, only a tiny portion of the image is showing. You can sort of see it in this screenshot:
I wrote a wrapper class (CardImage) for a BufferedImage:
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class CardImage {
BufferedImage img = null;
public CardImage() {
}
public BufferedImage setImage(Card c) {
try {
img = ImageIO.read(new File("/media/billy/HOME/workspace/Shithead/src/cards/" + toName(c)));
} catch(Exception e) {
System.out.println(e);
}
/*
int scale_factor = 8;
System.out.println(img.getHeight());
Image dimg = img.getScaledInstance((int)img.getWidth()/scale_factor, (int)img.getHeight()/scale_factor, Image.SCALE_SMOOTH);
Graphics g = img.createGraphics();
g.drawImage(dimg, 0, 0, null);
g.dispose();
*/
return img;
}
public String toName(Card c) {
String tmp = c.toString().replaceAll(" ", "_");
tmp = tmp.toLowerCase();
tmp = tmp + ".png";
return tmp;
}
}
and I have a HandPanel that extends JPanel in which I want to draw the CardImage BufferedImage:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class HandPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
CardImage cardImage = new CardImage();
Card card = new Card(2,2);
BufferedImage img = cardImage.setImage(card);
System.out.println(img.getHeight(null) + " " + img.getWidth(null));
g.drawImage(img, 0, 0, null);
}
}
And I have a another class to contain the HandPanel:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShitGUI {
public static void main(String[] args) {
ShitGUI gui = new ShitGUI();
}
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
public ShitGUI() {
mainPanel.setPreferredSize(new Dimension(500,500));
HandPanel pan = new HandPanel();
mainPanel.add(pan);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
}
The images of the cards are fairly big (creating Card(2,2) is just a 2 of Hearts, and I know I acquire the path correctly).
Any help is appreciated. Thanks!
N.B. When I uncomment the section containing dimg, I obtain a scaled down red card (rather than black and white, not scaled image as I otherwise get). I understanding the scaling, but I don't understand why my approach is giving me a black and white image.