アニメーション GIF を使用して Swing でアニメーション背景を表示する方法
アニメーション GIF は Swing アプリケーションで簡単に表示できますが、画像を背景として使用するには、別のアプローチが必要です。背景にアニメーション画像をロードするには、ImageIcon を使用して画像を取得するのが理想的です。
ImageIcon は、静止画像を配信する他のメソッドとは異なり、アニメーション画像を提供します。次のコードは、アニメーション GIF を使用して 50 個のボタンがあるパネルの背景をアニメーション化する方法を示しています。 javax.swing.* をインポートします。 インポート javax.swing.border.EmptyBorder; java.net.URLをインポートします。 class ImagePanel extends JPanel { プライベート画像画像; ImagePanel(イメージ画像) { this.image = 画像; } @オーバーライド public voidPaintComponent(グラフィックス g) { super.paintComponent(g); g.drawImage(image,0,0,getWidth(),getHeight(),this); } public static void main(String[] args) throws Exception { URL url = 新しい URL("https://i.sstatic.net/iQFxo.gif"); 最終画像 image = new ImageIcon(url).getImage(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame("画像"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationByPlatform(true); ImagePanel imagePanel = 新しい ImagePanel(image); imagePanel.setLayout(new GridLayout(5,10,10,10)); imagePanel.setBorder(new EmptyBorder(20,20,20,20)); for (int ii=1; ii
import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.net.URL; class ImagePanel extends JPanel { private Image image; ImagePanel(Image image) { this.image = image; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image,0,0,getWidth(),getHeight(),this); } public static void main(String[] args) throws Exception { URL url = new URL("https://i.sstatic.net/iQFxo.gif"); final Image image = new ImageIcon(url).getImage(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame("Image"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationByPlatform(true); ImagePanel imagePanel = new ImagePanel(image); imagePanel.setLayout(new GridLayout(5,10,10,10)); imagePanel.setBorder(new EmptyBorder(20,20,20,20)); for (int ii=1; ii
免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。
Copyright© 2022 湘ICP备2022001581号-3