2013-08-25 162 views
1

我在做一個小遊戲。這不是一個行動,而是一個難題,所以表現並不那麼重要。 現在,我有主遊戲區,一個背景圖片。在某些情況下,我想在背景圖像的一部分上繪製其他圖像。 我的問題是,背景圖像以及疊加的(一個或多個)背景圖像都可以是GIF動畫,具有未知數量的幀(基本上,用戶選擇)。Java:在其他動畫gif上疊加/疊加動畫gif

現在,我想要做的事情基本上是:繪製背景圖像並將其設置爲動畫(如果它是gif),然後在其上繪製0-n個較小的動畫gif,相對於背景圖像的位置,像素座標。 此外,它應該可調整大小,以便圖像相應地縮放/移動。

如何最終結果可能類似於:http://i.stack.imgur.com/PxdDt.png(試想一下它的動畫)

我已經找到了它一個解決方案,它的工作原理是設置佈局管理器爲null,使用絕對定位的JLabel與圖標有他們的動畫,使用一個作爲背景,另一個添加到它的前景:

background.setSize(backgroundIcon.getIconWidth(), backgroundIcon.getIconHeight()); 
foreground.setSize(foregroundIcon.getIconWidth(), foregroundIcon.getIconHeight()); 
background.setLocation(0, 0); 
foreground.setLocation(30, 30); 
background.setLayout(null); 
background.add(foreground); 
frame.setLayout(null); 
frame.add(background); 

(以下全碼)

設置佈局管理器爲NULL,因爲我聽說,有問題(特別,因爲我想。想要 稍後在圖像中添加一些文字,儘管我會在背景中使用另一個標籤,而不是背景標籤本身或其他東西),調整大小似乎不可能,因爲我無法調整JLabels圖像圖標的大小(發現了一些解決方案,它將gif分割成每層一個bufferedImage,並在將它們重新放回到一起之前調整它們的大小,通過這些解決方案在幀之間失去特定的延遲,等等)。此外,由於位置應該像素完美,因此即使可能調整大小也可能由於舍入誤差而產生問題。

現在,有沒有更好的方法來做到這一點? 我可以以某種方式加載GIF動畫並手動合併它們(即使它們可能有不同數量的圖層),所以我只能繪製一個圖像?有沒有可以手動設置組件位置的佈局管理器,但如果調整窗口/面板的大小,它會自動縮放它?有沒有第三方圖形項目可以做我想要的東西?

理想情況下,我會做的事情,如果他們不是動畫,如建立一個合併的圖像,然後調整和顯示那個。

全碼:

import java.awt.Dimension; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public final class Tester extends JFrame { 
    public static void main(String[] args) throws MalformedURLException { 
     new Tester(); 
    } 

    private Tester() throws MalformedURLException { 
     setTitle("Tester"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Icon backgroundIcon = new ImageIcon(new URL("http://s5.favim.com/orig/51/animated-gif-gif-hands-sign-language-Favim.com-542945.gif")); 
     Icon foregroundIcon = new ImageIcon(new URL("http://i.imgur.com/89HANHg.gif")); 

     JLabel background = new JLabel(backgroundIcon); 
     JLabel foreground = new JLabel(foregroundIcon); 

     // ugly 
     background.setSize(backgroundIcon.getIconWidth(), backgroundIcon.getIconHeight()); 
     foreground.setSize(foregroundIcon.getIconWidth(), foregroundIcon.getIconHeight()); 
     background.setLocation(0, 0); 
     foreground.setLocation(30, 30); 
     background.setLayout(null); 
     background.add(foreground); 
     setLayout(null); 
     add(background); 

     // set size of frame to size of content 
     setResizable(false); 
     getContentPane().setPreferredSize(new Dimension(backgroundIcon.getIconWidth(), backgroundIcon.getIconHeight())); 
     pack(); 

     setLocationRelativeTo(null); 
     setVisible(true); 
    } 
} 
+0

在你的[sscce](http://sscce.org/)中,通過'URL'訪問發佈的圖片,如[這裏](http://stackoverflow.com/a/10862262/230513)或[below](http://stackoverflow.com/a/18434145/230513);如圖所示使用合成圖像(http://stackoverflow.com/a/15982915/230513);或使用'UIManager'圖標,如[此處]所示(http://stackoverflow.com/a/12228640/230513)。 – trashgod

+0

進行了相應編輯 – Klaue

回答

3

這對我的作品。較小的軌道圖像以較大的月相圖像爲中心。

import java.awt.*; 
import javax.swing.*; 
import java.net.*; 

public final class Tester extends JFrame { 
    public static void main(String[] args) throws MalformedURLException { 
     new Tester(); 
    } 

    private Tester() throws MalformedURLException { 
     setTitle("Tester"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Icon backgroundIcon = new ImageIcon(
       new URL("http://1point1c.org/gif/moon/moonphases.gif")); 
     Icon foregroundIcon = new ImageIcon(
       new URL("http://1point1c.org/gif/thum/plnttm.gif")); 

     JLabel background = new JLabel(backgroundIcon); 
     JLabel foreground = new JLabel(foregroundIcon); 

     background.setLayout(new GridBagLayout()); 
     background.add(foreground); 
     add(background); 

     pack(); 

     setLocationByPlatform(true); 
     setVisible(true); 
    } 
} 
+0

如果前景圖像應居中,但它必須位於特定位置,則此(或其他版面管理器)應該沒問題。 想象一下,例如,背景圖像顯示的牆上有一個空的畫框,在鼠標懸停的情況下,應該顯示動畫的GIF(覆蓋)。它必須完全在框架內,因此我能想到的所有佈局管理器都無法工作。 – Klaue

+0

什麼,所以'空畫框'(不管那是什麼)不居中?它移動了嗎,還是處於靜態位置? –

+0

*「它必須完全在框架內」,*確切地說**哪裏?** *「因此,所有我能想到的佈局管理器都不起作用。」*如果存在定位第二個圖像的邏輯,則該邏輯可以包含在佈局管理器中。 –