[英]smoothly move a JButton component to mouse click position in java
我對Java的初學者水平要好一些,並且在學生作業方面遇到了麻煩。 我不得不將一個類(student.java)“變成一個按鈕”-這已經完成了(這就是為什么我的代碼不是創建新的JButton實例,而是創建一個新的“ student”實例)。 我需要讓按鈕將其位置重新定位到用戶單擊鼠標的位置。 我已成功完成此任務,因此已滿足我的作業要求。
但是,我希望按鈕可以平滑地移動到鼠標單擊的位置,而不是從以前的位置突然跳到新的位置。 下面是我的代碼。 我已經嘗試過mouseClicked()方法內部的數學運算,但是它對按鈕的運動沒有影響。
任何提示將非常感謝。 謝謝!
public myJPanel(){
super();
setLayout(null);
setBackground(Color.decode("#F5F2EB"));
setVisible(true);
setPreferredSize(new Dimension(640,480));
setMinimumSize(new Dimension(640,480));
setMaximumSize(new Dimension(640,480));
Font f = new Font("Copperplate Gothic Bold", Font.BOLD, 16);
student btn = new student("First","Last", num, "");
add(btn);
btn.setBounds(100, 150, 400, 90);
btn.setText(btn.getInfo());
btn.setBackground(Color.decode("#89A690"));
btn.setForeground(Color.decode("#F5F2EB"));
btn.setOpaque(true);
btn.setFont(f);
btn.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
// move btn object
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX(); //mouse click x position
int y = e.getY(); //mouse click y position
int px = btn.getX() - x; //previous btn x position(to get distance between old / new position)
int py = btn.getY() - y; //previous btn y position(to get distance between old / new position)
double speed = 5; //speed
double ang = (float)Math.atan2(py, px) * 180 / Math.PI; //angle
x += Math.cos(ang * Math.PI/180) * speed; //move to x
y += Math.sin(ang * Math.PI/180) * speed; //move to y
btn.setLocation(x,y); //relocate button to new location
}});
您的代碼中需要某種動畫概念,僅更新位置不會使其平滑移動。 您需要的變更
例。 在這里,我計算了總距離,然后根據時間和速度插值“到目前為止的距離”。
還要注意使用Math.toDegrees()和Math.toRadians(),盡管您根本不需要它們,除非您想將ang用作度數。
public class Foo {
private static class Animate extends JPanel {
private JButton btn;
private int startX;
private int startY;
private long startTime;
private double ang;
private double distance;
public Animate() {
super();
setLayout(null);
btn = new JButton("Dr Horse");
btn.setBounds(100, 150, 40, 10);
add(btn);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
startX = btn.getX();
startY = btn.getY();
startTime = System.nanoTime();
int px = btn.getX() - e.getX();
int py = btn.getY() - e.getY();
distance = Math.sqrt(px * px + py * py);
ang = Math.toDegrees(Math.atan2(py, px));
}
});
Timer timer = new Timer(1000 / 20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double duration = (System.nanoTime() - startTime) / 1e6;
int speed = 50;// pixels per second
double distanceSoFar = Math.min(speed * duration / 1000d, distance);
int x = startX - (int) (distanceSoFar * Math.cos(Math.toRadians(ang)));
int y = startY - (int) (distanceSoFar * Math.sin(Math.toRadians(ang)));
btn.setLocation(x, y);
}
});
timer.setRepeats(true);
timer.start();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Animate());
frame.setSize(500, 400);
frame.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.