2012-12-10 39 views
1

我有很多在窗口中移動的平面(線程),我希望根據平面的方向切換ImageIcon。 例如:如果一個平面向右移動,那麼該平面的imageIcon是正確的,然後平面向左移動,交換平面的imageIcon被留下。 如何在paintComponent方法中做到這一點? 對不起,我的英語不好。在java中切換imageIcon?

+1

見[翻轉圖像水平地(http://stackoverflow.com/questions/13742365/how-do-i-flip-an- image-horizo​​ntal-flip-with-glreadpixels-bufferedimage-and-o/13756357#13756357)爲它的一部分。但是在啓動時,而不是在'paintComponent(..)'中。你有什麼問題? –

回答

3

如果您正在討論交換由JLabel顯示的ImageIcon,那麼您不應該在paintComponent中切換ImageIcons,而應該在代碼的非paintComponent區域(可能在Swing Timer中)執行此操作。即使你不是在討論JLabel,也不應該使用paintComponent方法來改變對象的狀態。

然而,你的問題留下了太多的不足以讓我們能夠完全和好地回答它。考慮告訴和展示更多。

2

在設置方向你也應該設置圖像圖標,或者有一個getImageIcon(direction)

paintComponent沒有重邏輯應該發生;它應該儘可能快。 paintComponent被調用的時間和頻率沒有(全部)控制。

3

如果您正在尋找邏輯thingy,那麼一個小例子就在這裏,但您可能需要修改它以滿足您的需求。

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import java.util.Random; 
import javax.swing.*; 

public class FlyingAeroplane 
{ 
    private Animation animation; 
    private Timer timer; 
    private ActionListener timerAction = new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent ae) 
     { 
      animation.setValues(); 
     } 
    }; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Aeroplane Flying"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     animation = new Animation(); 
     frame.setContentPane(animation); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
     timer = new Timer(100, timerAction); 
     timer.start(); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new FlyingAeroplane().displayGUI(); 
      } 
     }); 
    } 
} 

class Animation extends JPanel 
{ 
    private final int HEIGHT = 150; 
    private final int WIDTH = 200; 
    private int x; 
    private int y; 
    private ImageIcon image; 
    private boolean flag; 
    private Random random; 

    public Animation() 
    { 
     x = 0; 
     y = 0; 
     image = new ImageIcon(getClass().getResource("/image/aeroplaneright.jpeg")); 
     flag = true; 
     random = new Random(); 
    } 

    public void setValues() 
    { 
     x = getXOfImage(); 
     y = random.nextInt(70); 
     repaint(); 
    } 

    private int getXOfImage() 
    { 
     if (flag) 
     {   
      if ((x + image.getIconWidth()) == WIDTH) 
      { 
       flag = false; 
       x--; 
       return x; 
      } 
      x++; 
      image = new ImageIcon(getClass().getResource("/image/aeroplaneright.jpeg")); 
     } 
     else if (!flag) 
     { 
      if (x == 0) 
      { 
       flag = true; 
       x++; 
       return x; 
      } 
      x--; 
      image = new ImageIcon(getClass().getResource("/image/aeroplaneleft.jpeg")); 
     } 
     return x; 
    } 

    @Override 
    public Dimension getPreferredSize() 
    { 
     return (new Dimension(WIDTH, HEIGHT)); 
    } 

    @Override 
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     g.drawImage(image.getImage(), x, y, this); 
    } 
} 

圖像中使用:

AEROPLANERIGHT AEROPLANELEFT

+0

請仔細閱讀如何[手動添加圖片到您的項目](http://stackoverflow.com/a/11372350/1057230)或此[線程](http://stackoverflow.com/a/9866659/1057230) –