Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使图像循环到屏幕的另一侧_Java - Fatal编程技术网

Java 如何使图像循环到屏幕的另一侧

Java 如何使图像循环到屏幕的另一侧,java,Java,我为我正在做的一个项目做了一个测试程序。它绘制一辆汽车,并将其从左到右穿过JFrame。我想做的是,一旦汽车物体完全通过框架的另一侧,它就会在另一侧回环。我想我必须在主类animationTester中为计时器使用一个外部循环,但我不完全确定我想在循环中放入什么。还有人告诉我,我无法将窗口的大小硬编码到程序中。不管怎样,这是我的课 沙培康班 import java.awt.*; import java.util.*; import javax.swing.*; /** An icon t

我为我正在做的一个项目做了一个测试程序。它绘制一辆汽车,并将其从左到右穿过JFrame。我想做的是,一旦汽车物体完全通过框架的另一侧,它就会在另一侧回环。我想我必须在主类animationTester中为计时器使用一个外部循环,但我不完全确定我想在循环中放入什么。还有人告诉我,我无法将窗口的大小硬编码到程序中。不管怎样,这是我的课

沙培康班

import java.awt.*;
import java.util.*;
import javax.swing.*;

/**
   An icon that contains a moveable shape.
*/
public class ShapeIcon implements Icon
{
   private int width;
   private int height;
   private MoveableShape shape;

   public ShapeIcon(MoveableShape shape,
      int width, int height)
   {
      this.shape = shape;
      this.width = width;
      this.height = height;
   }

   public int getIconWidth()
   {
      return width;
   }

   public int getIconHeight()
   {
      return height;
   }

   public void paintIcon(Component c, Graphics g, int x, int y)
   {
      Graphics2D g2 = (Graphics2D) g;
      shape.draw(g2); // has draw method since it implements MoveableShape
   }


}
车形类

import java.awt.*;
import java.awt.geom.*;
import java.util.*;

/**
   A car that can be moved around.
*/
public class CarShape implements MoveableShape
{
   private int x;
   private int y;
   private int width;
   /**
      Constructs a car item.
      @param x the left of the bounding rectangle
      @param y the top of the bounding rectangle
      @param width the width of the bounding rectangle
   */
   public CarShape(int x, int y, int width)
   {
      this.x = x;
      this.y = y;
      this.width = width;
   }

   public void translate(int dx, int dy)
   {
      x += dx;
      y += dy;
   }

   public void draw(Graphics2D g2)
   {
      Rectangle2D.Double body
            = new Rectangle2D.Double(x, y + width / 6, 
                  width - 1, width / 6);
      Ellipse2D.Double frontTire
            = new Ellipse2D.Double(x + width / 6, y + width / 3, 
                  width / 6, width / 6);
      Ellipse2D.Double rearTire
            = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
                  width / 6, width / 6);

      // The bottom of the front windshield
      Point2D.Double r1
            = new Point2D.Double(x + width / 6, y + width / 6);
      // The front of the roof
      Point2D.Double r2
            = new Point2D.Double(x + width / 3, y);
      // The rear of the roof
      Point2D.Double r3
            = new Point2D.Double(x + width * 2 / 3, y);
      // The bottom of the rear windshield
      Point2D.Double r4
            = new Point2D.Double(x + width * 5 / 6, y + width / 6);
      Line2D.Double frontWindshield
            = new Line2D.Double(r1, r2);
      Line2D.Double roofTop
            = new Line2D.Double(r2, r3);
      Line2D.Double rearWindshield
            = new Line2D.Double(r3, r4);

      g2.draw(body);
      g2.draw(frontTire);
      g2.draw(rearTire);
      g2.draw(frontWindshield);
      g2.draw(roofTop);
      g2.draw(rearWindshield);
   }


}
可移动形状类

import java.awt.*;

/**
   A shape that can be moved around.
*/
public interface MoveableShape
{
   /**
      Draws the shape.
      @param g2 the graphics context
   */
   void draw(Graphics2D g2);
   /**
      Moves the shape by a given amount.
      @param dx the amount to translate in x-direction
      @param dy the amount to translate in y-direction
   */
   void translate(int dx, int dy);
}
主类

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
   This program implements an animation that moves
   a car shape.
*/
public class AnimationTester
{
   private static final int ICON_WIDTH = 400;
   private static final int ICON_HEIGHT = 100;
   private static final int CAR_WIDTH = 100;
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      final MoveableShape shape
            = new CarShape(0, 0, CAR_WIDTH);

      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 = 100;
      // Milliseconds between timer ticks
      Timer t = new Timer(DELAY, new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               shape.translate(1, 0);
               label.repaint();

            }
         });
      t.start();
   }


}
您可以修改translate方法以允许一个end参数,这样车辆在到达帧的末尾时会再次启动:

public void translate(int dx, int dy, int end) {
    x += dx;
    y += dy;
    if (end == x) {
        x = -100;
    }
}
然后将frame.getWidth()传递给AnimationTester.class上的translate调用:

Timer t = new Timer(DELAY, new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                car.translate(1, 0, frame.getWidth());
                label.repaint();

            }
        });

我希望它有帮助:)

当设置
x
位置时,将其设置为
(x+dx+frameWidth)%frameWidth
。好的,这在逻辑上对我来说很有意义,但是如果我不能硬编码窗口的大小,我如何找到frameWidth的值?
frame.getWidth()
因此,在我的translate方法中,我是否需要为帧本身添加第三个参数,以便在其中写入int frameWidth=frame.getWidth()之类的内容;因为现在我在调用汽车对象上的translate,而不是框架,所以我不能在translate中使用frame.getWidth()引用框架本身;