【问题标题】:Switch imageIcon in java?在java中切换imageIcon?
【发布时间】:2012-11-27 10:51:21
【问题描述】:

我有许多在窗口中移动的平面(线程),我想根据平面的方向切换 ImageIcon。 例如:如果一个平面向右走,平面的imageIcon向右,然后平面向左,把imageIcon换成向左的平面。 如何在方法paintComponent 中做到这一点? 对不起我的英语不好。

【问题讨论】:

  • 请参阅flip an image horizontally 了解其中的一部分。但在启动时进行,而不是在paintComponent(..)。你在哪方面有问题?

标签: java swing paintcomponent imageicon


【解决方案1】:

如果您正在讨论交换 JLabel 显示的 ImageIcon,那么您不应该在 paintComponent 中切换 ImageIcons,而是应该在代码的 non-paintComponent 区域中执行此操作,也许在 Swing Timer 中。即使您不是在谈论 JLabel,paintComponent 方法也不应该用于更改对象的状态。

但是,您的问题留下了太多未说明的内容,无法让我们能够完整而准确地回答它。考虑更多地讲述和展示。

【讨论】:

    【解决方案2】:

    在设置方向时,您也应该设置图像图标,或者使用getImageIcon(direction)

    paintComponent 中不应发生繁重的逻辑;它应该尽可能快。您无法(完全)控制调用paintComponent 的时间和频率。

    【讨论】:

      【解决方案3】:

      如果您正在寻找逻辑的东西,那么这里有一个小例子,尽管您可能需要根据需要对其进行修改。

      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);
          }
      }
      

      使用的图片:

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多