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

时间:2019-11-15 08:07:43

标签: java

我为正在进行的项目制作了这个测试程序。它绘制汽车,并使其从左到右跨JFrame移动。我正在尝试做的是,这样一旦汽车物体完全通过框架的另一侧,它将在另一侧循环。我认为我必须在名为animationTester的主类中为计时器使用外部循环,但是我不确定我要放入循环中的内容。还被告知我不能将窗口的大小硬编码到程序中。无论如何,这是我的课程。

ShapeIcon类

 <?php
    require_once("includes/config.php");
    //

    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
        class mypdf extends FPDF{

        function viewTable(){
            $this->setfont('Arial','B',12);
            $search=$_POST['search1'];
            $option=$_POST['option1'];
            $period=$_POST['period1'];
            $datefrom=$_POST['datefrom1'];
            $dateto=$_POST['dateto1'];
            $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));


                if($period ==null){
                $sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date 
                FROM tblfacility 
                JOIN tbluser on tbluser.id=tblfacility.user 
                where ".$search." ='".$option."' ";}
                else{
                $sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
                }
                $query = $dbh->prepare($sql);
                $query->execute();
                $results=$query->fetchAll(PDO::FETCH_OBJ);
                //echo "<prep>";
                //print_r($sql);

                while($results=$query->fetchAll(PDO::FETCH_OBJ)){
                    $this->cell(50,10,$results->user ,1,0,'L');
                    $this->cell(50,10,$results->affiliation,1,0,'L');
                    $this->cell(40,10,$results->type,1,0,'L');
                    $this->cell(30,10,$results->sampleid,1,0,'L');
                    $this->cell(40,10,$results->time,1,0,'L');
                    $this->Ln();
                }

        }

        }
    $pdf = new mypdf();
    $pdf->AliasNbPages();
    $pdf->AddPage('L','A4',0);
    $pdf->viewTable();
    $pdf->output();


    ?>

CarShape类

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
   }


}

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

1 个答案:

答案 0 :(得分:0)

您可以修改 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();

            }
        });

我希望这会有所帮助:)