2012-12-10 38 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) –