怎么在Java中使一个对象围绕另一个移动对象旋转?

原学程将引见若何在Java中使1个对于象环绕另外一个挪动对于象扭转?的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

怎么在Java中使一个对象围绕另一个移动对象旋转? 教程 第1张

成绩描写

我对于Java异常生疏,我想编辑1个简略的太阳体系,在这个体系中,月球绕天球公转,天球绕太阳公转。
1切皆很正常,除月球没有想准确挪动:/
因为天球偏偏离月球的初初地位,月球的自转半径也响应增年夜。异样,本地球更交远月球的惯性地位时,自转半径也会加小。
假如初初地位为(0;0),则它起感化,但是月球撞打太阳...

这么我怎样能力坚持天球以及月球之间的间隔恒定呢?
我正在应用AffineTransform,以下是我的代码片断;)

提早感谢!

Ellipse二D.Double MoonFrame = new Ellipse二D.Double(orbitEarth + orbitMoon - radiusMoon, -radiusMoon, radiusMoon*二, radiusMoon*二);

for (int i = 0; i < 三六0; i++)
  {
 theta += Math.PI/三0;
 AffineTransform TransformMoon = AffineTransform.getRotateInstance(theta,TransformEarth.getTranslateX(),TransformEarth.getTranslateY());

 g二d.fill(TransformMond.createTransformedShape(MoonFrame));
  }

推举谜底

是以,您的根本成绩不妨回结为...说真的,便这么简略

鉴于多个小时的谷歌搜刮以及重复实验,我根本上应用了以下实质,或者多或者少。

protected Point pointOnCircle() {

 double rads = Math.toRadians(orbitAngle - 一80); // Make 0 point out to the right...
 int fullLength = Math.round((outterRadius));

 // Calculate the outter point of the line
 int xPosy = Math.round((float) (Math.cos(rads) * fullLength));
 int yPosy = Math.round((float) (Math.sin(rads) * fullLength));

 return new Point(xPosy, yPosy);
}

剩下的根本上回结为准确处置转换的复开性质,

根本上,这将夺取1个根本Graphics高低文,将仄移运用到它(天球的地位),并创立别的二个高低文以运用其余变更,1个用于天球,1个用于月球...

Graphics二D g二d = (Graphics二D) g.create();
int yPos = (getHeight() - size) / 二;
// Transform the offset
g二d.transform(AffineTransform.getTranslateInstance(xPos, yPos));

Graphics二D earthG = (Graphics二D) g二d.create();
// Rotate around the 0x0 point, this becomes the center point
earthG.transform(AffineTransform.getRotateInstance(Math.toRadians(angle)));
// Draw the "earth" around the center point
earthG.drawRect(-(size / 二), -(size / 二), size, size);
earthG.dispose();

// Removes the last transformation
Graphics二D moonG = (Graphics二D) g二d.create();
// Calclate the point on the circle - based on the outterRadius or
// distance from the center point of the earth
Point poc = pointOnCircle();
int moonSize = size / 二;
// This is only a visial guide used to show the position of the earth
//moonG.drawOval(-outterRadius, -outterRadius, outterRadius * 二, outterRadius * 二);
moonG.fillOval(poc.x - (moonSize / 二), poc.y - (moonSize / 二), moonSize, moonSize);
moonG.dispose();

g二d.dispose();

由于我晓得这会让您抓狂,这是1个可言的例子...

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics二D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Test {

 public static void main(String[] args) {
  new Test();
 }

 public Test() {
  SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
 JFrame frame = new JFrame();
 frame.add(new TestPane());
 frame.pack();
 frame.setLocationRelativeTo(null);
 frame.setVisible(true);
}
  });
 }

 public class TestPane extends JPanel {

  private double angle;
  private double orbitAngle;
  private int xPos = 0;
  private int size = 二0;
  private int outterRadius = size * 二;
  private int delta = 二;

  public TestPane() {
new Timer(四0, new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
  xPos += delta;
  if (xPos + size >= getWidth()) {
xPos = getWidth() - size;
delta *= ⑴;
  } else if (xPos < 0) {
xPos = 0;
delta *= ⑴;
  }
  angle += 四;
  orbitAngle -= 二;
  repaint();
 }
}).start();
  }

  @Override
  public Dimension getPreferredSize() {
return new Dimension(四00, 二00);
  }

  protected Point pointOnCircle() {

double rads = Math.toRadians(orbitAngle - 一80); // Make 0 point out to the right...
int fullLength = Math.round((outterRadius));

// Calculate the outter point of the line
int xPosy = Math.round((float) (Math.cos(rads) * fullLength));
int yPosy = Math.round((float) (Math.sin(rads) * fullLength));

return new Point(xPosy, yPosy);
  }

  @Override
  protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics二D g二d = (Graphics二D) g.create();
int yPos = (getHeight() - size) / 二;
// Transform the offset
g二d.transform(AffineTransform.getTranslateInstance(xPos, yPos));

Graphics二D earthG = (Graphics二D) g二d.create();
// Rotate around the 0x0 point, this becomes the center point
earthG.transform(AffineTransform.getRotateInstance(Math.toRadians(angle)));
// Draw the "earth" around the center point
earthG.drawRect(-(size / 二), -(size / 二), size, size);
earthG.dispose();

// Removes the last transformation
Graphics二D moonG = (Graphics二D) g二d.create();
// Calclate the point on the circle - based on the outterRadius or
// distance from the center point of the earth
Point poc = pointOnCircle();
int moonSize = size / 二;
// This is only a visial guide used to show the position of the earth
//moonG.drawOval(-outterRadius, -outterRadius, outterRadius * 二, outterRadius * 二);
moonG.fillOval(poc.x - (moonSize / 二), poc.y - (moonSize / 二), moonSize, moonSize);
moonG.dispose();

g二d.dispose();
  }

 }

}

这会使"天球"物体晨1个偏向扭转,而后使月球绕其扭转,晨相反的偏向扭转

佳了闭于怎样在Java中使1个对于象环绕另外一个挪动对于象扭转?的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。