我试图直线移动一个圆圈。但我的守则没有给出预期的结果。我使用mouseMotionListener来不断获取目标点,x_pos和y_pos是为我的圈子协调的。我正在使用三角函数sin cos和atan,以直线移动对象 - 我从堆栈溢出上发布的问题看到here的逻辑 - 但它没有给我预期的结果,我做错了什么plz帮助:
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
public class Snake extends JPanel {
static Snake snake = new Snake();
static Co_Ordinates co = new Co_Ordinates();
static int x_pos = 10, y_pos = 500;
static int slope;
int delta_x,delta_y;
double direction;
double x_inc,y_inc;
public Snake() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.green);
g2d.fillArc(x_pos, y_pos, 20, 20, 0, 360);
try {
Thread.sleep(10);
} catch (Exception e) {
}
/*if(x_pos==co.x_Cur){
if(co.y_Cur>=y_pos)
y_pos++;
else if(co.y_Cur<=y_pos)
y_pos--;
}
else if(y_pos==co.y_Cur){
if(co.x_Cur>=x_pos)
x_pos++;
else if(co.x_Cur<=x_pos)
x_pos--;
}
*/
//slope=((co.y_Cur - y_pos) / (co.x_Cur - x_pos));
//y_pos = slope*x_pos+y_pos;
//x_pos++;
//System.out.println("...");
if(x_pos!=co.x_Cur&&y_pos!=co.y_Cur){
int delta_x = co.x_Cur - x_pos;
int delta_y = co.y_Cur - y_pos;
direction = Math.atan(delta_y / delta_x); // Math.atan2(deltaY, deltaX) does the same thing but checks for deltaX being zero to prevent divide-by-zero exceptions
double speed = 5.0;
x_inc = (speed * Math.cos(direction));
y_inc = (speed * Math.sin(direction));
x_pos+=x_inc;
y_pos+=y_inc;
}
//x_pos = co.x_Cur;
repaint(10);// parameters
}
public void move() {
}
public static void main(String args[]) {
JFrame jf = new JFrame();
jf.add(co);
jf.addMouseMotionListener(co);
jf.addMouseListener(co);
jf.add(snake);
jf.setSize(600, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class Co_Ordinates extends JPanel implements MouseMotionListener, MouseListener {
static int slope;
static Snake sn = new Snake();
static int x_Cur=sn.x_pos-20;
static int y_Cur=sn.y_pos-40;
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
x_Cur = e.getX() - 20;
y_Cur = e.getY() - 40;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}// Class Listener ends here
答案 0 :(得分:0)
在您使用Math.atan
的代码中,应更改为Math.atan2
。
atan
仅提供介于-90deg和90deg之间的角度值。
atan2
给出的角度值介于-180deg和180deg之间。
我修改了以下代码部分。
atan
更改为atan2
请试试这个。
// if(x_pos!=co.x_Cur&&y_pos!=co.y_Cur){
delta_x = co.x_Cur - x_pos;
delta_y = co.y_Cur - y_pos;
direction = Math.atan2(delta_y, delta_x);
double speed = Math.sqrt(delta_x*delta_x + delta_y*delta_y);
speed = Math.min(speed, 5.0);
x_inc = (speed * Math.cos(direction));
y_inc = (speed * Math.sin(direction));
x_pos+=x_inc;
y_pos+=y_inc;
//}