Witam!
Znalazłem w sieci takowy kod. Ma on za zadanie wyświetlić obrazek. Nie bardzo rozumiem dlaczego wyświetla on obrazek tylko i wyłącznie na JFrame, nie wyświetla go już na JPanel. Chciałbym go wykorzystać do wyświetlania animacji 16 katkowej. W jaki sposób należy zmodyfikować ten kod, aby obraz był wyświetlany na panelu? Dobrze by było, żeby respektował kanał alpha i widać było to co jest "pod spodem". Dzięki
public class Test {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test");
URL transPng = new URL("http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png");
Image image = ImageIO.read(transPng);
frame.add(new ImageComponent(image), BorderLayout.CENTER);
// JLabel label = new JLabel(new ImageIcon(image));
// label.setOpaque(false);
// frame.add(label, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
static class ImageComponent extends JComponent {
private Image image;
public ImageComponent(Image image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle r = g.getClipBounds();
// paint bg
int s = 10;
for (int y = r.y / s; y < r.y + r.height; y += s) {
int o = (y % (2*s) == 0 ? s : 0);
for (int x = r.x / s + o; x < r.x + r.width; x += 2*s)
g.fillRect(x, y, s, s);
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
}
}