2014-04-21 27 views
0

我想基本上翻拍小行星在Java中,但我會用一個禿鷹作爲擊落蘇聯國旗的船舶。現在,我的禿鷹形象是一個老鷹周圍有白色輪廓的正方形。我想刪除這個,有沒有辦法將這個以一對一的方式映射到各種多邊形?如何將自定義圖像映射到遊戲中的多邊形?

這裏是我的代碼,雖然我不知道究竟如何,這將有助於東西:

public class Main { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException { 
    GameTest t = new GameTest(); 
} 

public static class GameTest extends JFrame { 

    private static final int WINDOW_WIDTH = 800; 
    private static final int WINDOW_HEIGHT = 500; 
    private GamePanel gamePanel; 

    public GameTest() throws IOException { 
     super("Deep Fried Freedom"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
     setLayout(new BorderLayout()); 
     gamePanel = new GamePanel(); 
     add(gamePanel); 
     center(this); 
     setVisible(true); 
    } 

    public static void center(JFrame frame) { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     Point center = ge.getCenterPoint(); 

     int w = frame.getWidth(); 
     int h = frame.getHeight(); 

     int x = center.x - w/2, y = center.y - h/2; 
     frame.setBounds(x, y, w, h); 
     frame.validate(); 
    }//end of center method 
} 
} 


public class GamePanel extends JPanel { 
public static BufferedImage baldEagleImage; 

public GamePanel() throws IOException { 
    super(); 
    baldEagleImage = ImageIO.read(new File("baldeagleimage.jpg")); 
} 

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setColor(Color.black);// set color black 
    g.fillRect(0, 0, getWidth(), getHeight()); // paint background 
    g.drawImage(baldEagleImage, 350, 175, null);//paint the launcher 
}//end paintComponent method 
}//end GamePanel class 

回答

0

您有幾種方法可以達到這樣的效果。你最好的選擇是使用圖像的alpha通道。只需在圖像編輯工具(如Gimp)中打開圖像即可。在此設置您的圖像的背景透明的圖像。

另一個選項(這不是最好的)但滿足您的要求是在Java2D中使用繪畫描邊。看看使用java2d剪輯功能。你可以得到一個教程這個here

0

通常你將有一個代表船Java對象,而且它有x和y座標稱爲類似的centerX和centerY。這使得屏幕上的船舶中心位於可視區域的範圍內。當你想讓船舶上下移動時,你可以修改這些值,並且你也可以在這些座標上g.drawImage你想要使用的圖像(根據需要加上任何偏移量以使圖像看起來集中於你的喜好)。

一個常用的方法是在初始化時啓動一個線程,並且在該線程中是一個while(true)塊,它對需要更新的所有對象執行update()方法,然後執行Thread.sleep( 17)模仿每秒約60幀的幀速率。在這種方法中,你的船有它的X和Y座標更新,然後每隔17毫秒在那個位置畫出圖像,這就是你如何得到一艘船(以及任何其他物體),就像他們在屏幕。

相關問題