1. <small id='pyI5q'></small><noframes id='pyI5q'>

    2. <legend id='pyI5q'><style id='pyI5q'><dir id='pyI5q'><q id='pyI5q'></q></dir></style></legend>

    3. <i id='pyI5q'><tr id='pyI5q'><dt id='pyI5q'><q id='pyI5q'><span id='pyI5q'><b id='pyI5q'><form id='pyI5q'><ins id='pyI5q'></ins><ul id='pyI5q'></ul><sub id='pyI5q'></sub></form><legend id='pyI5q'></legend><bdo id='pyI5q'><pre id='pyI5q'><center id='pyI5q'></center></pre></bdo></b><th id='pyI5q'></th></span></q></dt></tr></i><div id='pyI5q'><tfoot id='pyI5q'></tfoot><dl id='pyI5q'><fieldset id='pyI5q'></fieldset></dl></div>
      <tfoot id='pyI5q'></tfoot>
          <bdo id='pyI5q'></bdo><ul id='pyI5q'></ul>

        到达指定位置后如何旋转矩形?

        How to rotate a rectangle after reaching specified position?(到达指定位置后如何旋转矩形?)

          <bdo id='Cou6S'></bdo><ul id='Cou6S'></ul>
          • <small id='Cou6S'></small><noframes id='Cou6S'>

          • <i id='Cou6S'><tr id='Cou6S'><dt id='Cou6S'><q id='Cou6S'><span id='Cou6S'><b id='Cou6S'><form id='Cou6S'><ins id='Cou6S'></ins><ul id='Cou6S'></ul><sub id='Cou6S'></sub></form><legend id='Cou6S'></legend><bdo id='Cou6S'><pre id='Cou6S'><center id='Cou6S'></center></pre></bdo></b><th id='Cou6S'></th></span></q></dt></tr></i><div id='Cou6S'><tfoot id='Cou6S'></tfoot><dl id='Cou6S'><fieldset id='Cou6S'></fieldset></dl></div>
              <tbody id='Cou6S'></tbody>

                <tfoot id='Cou6S'></tfoot><legend id='Cou6S'><style id='Cou6S'><dir id='Cou6S'><q id='Cou6S'></q></dir></style></legend>

                  本文介绍了到达指定位置后如何旋转矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想旋转一个矩形,例如y 位置达到指定位置.我想在路口表现出一个矩形作为汽车 - 只需转动例如对.我更喜欢旋转并继续.

                  I would like to rotate a rectangle when e.g. y position achieve specified position. I would like to behave a rectangle as a car on a junction - just turn e.g. right. I prefer just rotate and continue.

                  草稿代码如下所示:

                  Graphics2D g2d = (Graphics2D) g.create();
                  g2d.setPaint(new Color(150, 150, 0));
                  
                  //1. when rectangle achieve 500 on y dimension just rotate left/right
                  if(y==500) {
                      _rotate = true;
                      g2d.rotate(Math.toRadians(90.));
                  }
                  if(_rotate) { //if rotate, continue way on x dimension
                      ++x ;
                      g2d.fillRect(x, y, 20, 40);
                  } else { //else go to the north
                      --y;
                      g2d.fillRect(x, y, 20, 40);
                  }
                  

                  推荐答案

                  您的问题缺少很多信息.

                  There is a lot of information which is missing from your question.

                  为了能够旋转一个形状,你需要知道一些事情,你需要知道它的当前位置和它的下一个目标位置,然后你可以简单地计算这两个点之间的角度.

                  In order to be able to rotate a shape, you need to know a few things, you need to know it's current position and it's next target position, then you can simply calculate the angle between these two points.

                  那么问题就变成了,你如何计算这些位置.有很多方法可以实现这一点,以下是一个简单的路径跟踪过程.

                  The question then becomes, how do you calculate these positions. There are plenty of ways you might achieve this, the following is a simple path following process.

                  首先,我们生成一条我们需要遵循的路径,我们使用 Shape API 来计算沿路径的点.我们使用一个简单的基于时间的动画(而不是通过点循环,我们通过计算动画播放的时间除以我们希望它花费的时间来计算沿路径的进度)并选择最佳的点符合我们目前的进度.

                  First, we generate a path which we need to follow, we use the Shape API to calculate the points along the path. We use a simple time based animation (rather the looping through the points, we calculate the progress along the path by calculating amount of time the animation has been playing divided by the amount of time we want it to take) and picking the point which best matches our current progress.

                  我们使用 AffineTransform 来旋转玩家形状并将生成的 Shape 转换到所需的位置.轻松

                  We use a AffineTransform to rotate the player shape and the translate the resulting Shape to the required position. Ease

                  import java.awt.Dimension;
                  import java.awt.EventQueue;
                  import java.awt.Graphics;
                  import java.awt.Graphics2D;
                  import java.awt.Rectangle;
                  import java.awt.Shape;
                  import java.awt.event.ActionEvent;
                  import java.awt.event.ActionListener;
                  import java.awt.event.KeyEvent;
                  import java.awt.geom.AffineTransform;
                  import java.awt.geom.Path2D;
                  import java.awt.geom.PathIterator;
                  import java.awt.geom.Point2D;
                  import java.util.ArrayList;
                  import java.util.List;
                  import javax.swing.AbstractAction;
                  import javax.swing.ActionMap;
                  import javax.swing.InputMap;
                  import javax.swing.JFrame;
                  import javax.swing.JPanel;
                  import javax.swing.KeyStroke;
                  import javax.swing.Timer;
                  import javax.swing.UIManager;
                  import javax.swing.UnsupportedLookAndFeelException;
                  
                  public class PathFollow {
                  
                      public static void main(String[] args) {
                          new PathFollow();
                      }
                  
                      public PathFollow() {
                          EventQueue.invokeLater(new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                  } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                                      ex.printStackTrace();
                                  }
                  
                                  JFrame frame = new JFrame("Testing");
                                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                  frame.add(new TestPane());
                                  frame.pack();
                                  frame.setLocationRelativeTo(null);
                                  frame.setVisible(true);
                              }
                          });
                      }
                  
                      public class TestPane extends JPanel {
                  
                          private Shape pathShape;
                          private List<Point2D> points;
                          private Shape car;
                  
                          private double angle;
                          private Point2D pos;
                          private int index;
                  
                          protected static final double PLAY_TIME = 5000; // 5 seconds...
                  
                          private Long startTime;
                  
                          public TestPane() {
                  
                              Path2D path = new Path2D.Double();
                              path.moveTo(0, 200);
                              path.curveTo(100, 200, 0, 100, 100, 100);
                              path.curveTo(200, 100, 0, 0, 200, 0);
                  
                              pathShape = path;
                  
                              car = new Rectangle(0, 0, 10, 10);
                  
                              points = new ArrayList<>(25);
                              PathIterator pi = pathShape.getPathIterator(null, 0.01);
                              while (!pi.isDone()) {
                                  double[] coords = new double[6];
                                  switch (pi.currentSegment(coords)) {
                                      case PathIterator.SEG_MOVETO:
                                      case PathIterator.SEG_LINETO:
                                          points.add(new Point2D.Double(coords[0], coords[1]));
                                          break;
                                  }
                                  pi.next();
                              }
                  
                  //          System.out.println(points.size());
                  //          pos = points.get(0);
                  //          index = 1;
                              Timer timer = new Timer(40, new ActionListener() {
                                  @Override
                                  public void actionPerformed(ActionEvent e) {
                  
                                      if (startTime == null) {
                                          startTime = System.currentTimeMillis();
                                      }
                                      long playTime = System.currentTimeMillis() - startTime;
                                      double progress = playTime / PLAY_TIME;
                                      if (progress >= 1.0) {
                                          progress = 1d;
                                          ((Timer) e.getSource()).stop();
                                      }
                  
                                      int index = Math.min(Math.max(0, (int) (points.size() * progress)), points.size() - 1);
                  
                                      pos = points.get(index);
                                      if (index < points.size() - 1) {
                                          angle = angleTo(pos, points.get(index + 1));
                                      }
                                      repaint();
                                  }
                              });
                              timer.start();
                          }
                  
                          @Override
                          public Dimension getPreferredSize() {
                              return new Dimension(200, 200);
                          }
                  
                          @Override
                          protected void paintComponent(Graphics g) {
                              super.paintComponent(g);
                              Graphics2D g2d = (Graphics2D) g.create();
                              g2d.draw(pathShape);
                  
                              AffineTransform at = new AffineTransform();
                  
                              if (pos != null) {
                  
                                  Rectangle bounds = car.getBounds();
                                  at.rotate(angle, (bounds.width / 2), (bounds.width / 2));
                  
                                  Path2D player = new Path2D.Double(car, at);
                  
                                  g2d.translate(pos.getX() - (bounds.width / 2), pos.getY() - (bounds.height / 2));
                                  g2d.draw(player);
                  
                              }
                  
                              g2d.dispose();
                          }
                  
                          // In radians...
                          protected double angleTo(Point2D from, Point2D to) {
                              double angle = Math.atan2(to.getY() - from.getY(), to.getX() - from.getX());
                              return angle;
                          }
                  
                      }
                  
                  }
                  

                  这篇关于到达指定位置后如何旋转矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                  <tfoot id='PVvSP'></tfoot>

                  1. <i id='PVvSP'><tr id='PVvSP'><dt id='PVvSP'><q id='PVvSP'><span id='PVvSP'><b id='PVvSP'><form id='PVvSP'><ins id='PVvSP'></ins><ul id='PVvSP'></ul><sub id='PVvSP'></sub></form><legend id='PVvSP'></legend><bdo id='PVvSP'><pre id='PVvSP'><center id='PVvSP'></center></pre></bdo></b><th id='PVvSP'></th></span></q></dt></tr></i><div id='PVvSP'><tfoot id='PVvSP'></tfoot><dl id='PVvSP'><fieldset id='PVvSP'></fieldset></dl></div>
                    <legend id='PVvSP'><style id='PVvSP'><dir id='PVvSP'><q id='PVvSP'></q></dir></style></legend>

                    <small id='PVvSP'></small><noframes id='PVvSP'>

                        <tbody id='PVvSP'></tbody>
                          • <bdo id='PVvSP'></bdo><ul id='PVvSP'></ul>