Java 以一定角度移动球
我是一个新手,尝试在斜坡上移动球。这是我给出坐标(100300600)的代码,用来在斜坡上移动球,但它正在远离斜坡线。以下是代码:-Java 以一定角度移动球,java,graphics,awt,Java,Graphics,Awt,我是一个新手,尝试在斜坡上移动球。这是我给出坐标(100300600)的代码,用来在斜坡上移动球,但它正在远离斜坡线。以下是代码:- import javax.swing.*; import java.awt.*; public class AgentMotion extends JPanel implements Runnable { Color color = Color.red; int dia = 0; long delay = 40; pri
import javax.swing.*;
import java.awt.*;
public class AgentMotion extends JPanel implements Runnable
{
Color color = Color.red;
int dia = 0;
long delay = 40;
private double x;
private double y;
private double x1;
private double y1;
private int dx = 1;
private int dy = 1;
private int dv = 1;
private double direction;
double a;
double b;
double a1;
double b1;
public void abc(double x, double y, double x2, double y2) {
this.x = x;
this.y = y;
this.x1 = x2;
this.y1 = y2;
this.direction=Math.toRadians(Math.atan2(x1-x,y1-y));
System.out.println("segfewg"+direction);
this.a = x;
this.b = y;
this.a1 = x1;
this.b1 = y1;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
//int x=100;
//int y=200;
int x3=(int)this.x;
int y3=(int)this.y;
g.fillOval(x3,y3,5,5); // adds color to circle
g.setColor(Color.black);
g2.drawOval(x3,y3,5,5); // draws circle
g2.drawLine(100, 300, 300, 600);
}
public void run() {
if(direction<0)
{
System.out.println("refregreg");
while(dy!=0) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move2();
repaint();
move();
repaint();
}
}
else
{
while(dx!=0) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move2();
repaint();
move();
repaint();
}
}
}
public void move() {
if(direction>0)
{
if(x + dv*Math.cos(direction) <a ||x + dia + dv * Math.cos(direction) >b) {
dx *= 0;
color = getColor();
}
x += dx;
}
else
{
System.out.println(x + dia + dv * Math.cos(direction));
if(x + dia + dv * Math.cos(direction) >b) {
dx *= 0;
color = getColor();
}
x -= dx;
}
}
public void move2() {
if(direction>0)
{
if(dv * Math.sin(direction) + y <a1 || dv * Math.sin(direction) + dia + y > b1) {
dy *= 0;
color = getColor();
}
y += dy;
}
else
{
System.out.println(dv * Math.sin(direction) + dia + y);
if(dv * Math.sin(direction) + y <a1 || dv * Math.sin(direction) + dia + y < b1) {
dy *= 0;
color = getColor();
}
y -= dy;
}
}
public Color getColor() {
int rval = (int)Math.floor(Math.random() * 256);
int gval = (int)Math.floor(Math.random() * 256);
int bval = (int)Math.floor(Math.random() * 256);
return new Color(rval, gval, bval);
}
public void start() {
while(dx==0) {
try {
System.out.println("jiuj");
Thread.sleep(25);
} catch(InterruptedException e) {
System.out.println("dwdwdwd");
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
import javax.swing.*;
导入java.awt.*;
公共类AgentMotion扩展了JPanel实现Runnable
{
颜色=颜色。红色;
int-dia=0;
长延时=40;
私人双x;
私人双y;
专用双x1;
私人双y1;
私有整数dx=1;
私有整数dy=1;
私有int dv=1;
私人双向;
双a;
双b;
双a1;
双b1;
公共无效abc(双x,双y,双x2,双y2){
这个.x=x;
这个。y=y;
这1.x1=x2;
这是1.y1=y2;
这个方向=数学toradian(数学atan2(x1-x,y1-y));
System.out.println(“segfewg”+方向);
a=x;
这个。b=y;
这1.a1=x1;
这1.b1=y1;
}
受保护组件(图形g){
超级组件(g);
图形2d g2=(图形2d)g;
g2.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
g、 设置颜色(颜色);
//int x=100;
//int y=200;
int x3=(int)this.x;
int y3=(int)this.y;
g、 Fillova(x3,y3,5,5);//为圆添加颜色
g、 设置颜色(颜色为黑色);
g2.绘制椭圆(x3,y3,5,5);//绘制圆
g2.拉线(100300300600);
}
公开募捐{
如果(方向0)
{
if(x+dv*Math.cos(方向)b){
dx*=0;
color=getColor();
}
x+=dx;
}
其他的
{
系统输出println(x+dia+dv*Math.cos(方向));
如果(x+dia+dv*Math.cos(方向)>b){
dx*=0;
color=getColor();
}
x-=dx;
}
}
公众谘询委员会2(){
如果(方向>0)
{
if(dv*数学sin(方向)+y b1){
dy*=0;
color=getColor();
}
y+=dy;
}
其他的
{
系统输出打印ln(dv*数学sin(方向)+直径+y);
if(dv*Math.sin(direction)+y几点建议
dx和dy应该是双精度的
正如@Spektre所说的,atan是一种矫枉过正的方法。你想要的是更简单地计算方向
this.direction=(y2-y)/(x2-x)
如果我们将(100300600)作为abc的参数(这可能是该类的构造函数),斜率为1.5。因此我们可以说:
dx=1.0
dy=1.5
这将完美地保持不变。对于一般情况,如果你想改变起点和终点,你必须计算比率。因此你可以将dx或dy设置为1.0,然后设置另一个,这样比率就可以保持不变。类似于下面的数学伪代码:
- dx=(x2-x)/最小值(x2-x,y2-y)
- dy=(y2-y)/最小值(x2-x,y2-y)
太多不相关和未注释的代码无法查看…我不是JAVA编码器,但atan2还没有弧度(检查该函数的引用)?还要检查所有分数变量是否都是浮点或双精度,而不是int…我想这项任务的变量太多了…为什么dx*=0?这简化为dx=0。