有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java制作图标在jframe上运行

public class AnimationTester
{
   public static void main(String[] args)
  {
  JFrame frame = new JFrame();

  final ArrayList<CarShape> shape = new ArrayList<> ();
     shape.add(new CarShape(0, 0, CAR_WIDTH));
     shape.add(new CarShape(60, 80, CAR_WIDTH/2));


  final ShapeIcon icon = new ShapeIcon(shape,
     ICON_WIDTH, ICON_HEIGHT);

  final JLabel label = new JLabel(icon);
  frame.setLayout(new FlowLayout());
  frame.add(label);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);

  final int DELAY = 10;
  // milliseconds between timer ticks
  Timer t = new Timer(DELAY, new
     ActionListener()
     {
        public void actionPerformed(ActionEvent event)
        {   
            int x = 0;
            for(CarShape car: shape)
            {
                if (x > ImageObserver.WIDTH) 
                    x = 1;
                else x += 5;

                car.translate(x, 0);
                label.repaint();
            }
        }
     });
  t.start();
   }

   private static final int ICON_WIDTH = 400;
   private static final int ICON_HEIGHT = 100;
   private static final int CAR_WIDTH = 100;
}

嗨,我正在尝试制作一个汽车图标,它会在画面上移动,然后重新出现在左侧。该代码能够使汽车穿过车架,但当它到达车架末端时,图标无法再次出现在左侧。我在这里发布这段代码是为了在调试过程中获得一些帮助。我认为逻辑错误就在这里

for(CarShape car: shape)
            {
                if (x == ImageObserver.WIDTH) 
                    x = 0;
                else x += 5;

                car.translate(x, 0);
                label.repaint();
            }

以下是我当前的输出: enter image description here

以下是本课程的其他课程: https://pastee.org/k65ev https://pastee.org/668h9 https://pastee.org/hxevz


共 (0) 个答案