物体的移动

这是任务: 蚂蚁以其速度V直线移动到其居住区域(例如[0; 0])的一个位置,然后以相同的速度回到其出生点。对象的移动。对象必须在特定点处停止并返回到起点。我应该如何修正我的代码?我写的一些代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

class vThread extends Thread{
public void run(){
    new LabSevenFirst();
    System.out.println(Thread.currentThread().getName());
}

}

public class LabSevenFirst extends JPanel implements ActionListener {
private JFrame fr;
double x = 10;
double y = 10;
double r = 10;
public static double T=0, V;
private float x1, y1, x2, y2, xc, yc;
private int t0;
private Timer timer;
private JButton start, stop, apply;
private JLabel forx1, fory1, forx2, fory2, forV;
private JTextField fx1, fy1, fx2, fy2, fV;


public static void main(String[] args) throws InterruptedException {

    vThread mt = new vThread();
    mt.setName("Ants-labours");
    mt.start();

   Thread.yield();//позволяет досрочно завершить квант времени текущей нити
    Thread.sleep(3000);
    System.out.println(Thread.currentThread().getName());
}

LabSevenFirst() {

    t0 = 1000/60;
    timer = new Timer(t0, this);
    timer.setActionCommand("timer");
    fr = new JFrame("Movement of ants-labours");
    fr.setLayout(null);
    fr.setSize(600, 600);
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 50, 300, 300);
    start = new JButton("Start");
    stop = new JButton("Stop");
    apply = new JButton("Apply");
    forx1 = new JLabel("x1");
    fory1 = new JLabel("y1");
    forx2 = new JLabel("x2");
    fory2 = new JLabel("y2");
    forV = new JLabel("V");
    fx1 = new JTextField(x1 + "");
    fy1 = new JTextField(y1 + "");
    fx2 = new JTextField(x2 + "");
    fy2 = new JTextField(y2 + "");
    fV = new JTextField(V + "");
    forx1.setBounds(5, 380, 20, 20);
    fory1.setBounds(5, 400, 20, 20);
    forx2.setBounds(5, 420, 20, 20);
    fory2.setBounds(5, 440, 20, 20);
    forV.setBounds(5, 460, 20, 20);
    fx1.setBounds(30, 380, 40, 20);
    fy1.setBounds(30, 400, 40, 20);
    fx2.setBounds(30, 420, 40, 20);
    fy2.setBounds(30, 440, 40, 20);
    fV.setBounds(30, 460, 40, 20);
    start.setActionCommand("start");
    stop.setActionCommand("stop");
    apply.setActionCommand("apply");
    start.addActionListener(this);
    stop.addActionListener(this);
    apply.addActionListener(this);
    start.setBounds(300, 430, 80, 20);
    stop.setBounds(390, 430, 80, 20);
    apply.setBounds(210, 430, 80, 20);
    fr.add(this);
    fr.add(start);
    fr.add(stop);
    fr.add(apply);
    fr.add(forx1);
    fr.add(fory1);
    fr.add(forx2);
    fr.add(fory2);
    fr.add(forV);
    fr.add(fx1);
    fr.add(fy1);
    fr.add(fx2);
    fr.add(fy2);
    fr.add(fV);
    fr.setVisible(true);
}

@Override
protected void paintComponent(Graphics g) {

    int width = getWidth();
    int height = getHeight();
    //System.out.println("width" + width);
    // System.out.println("height" + height);
    g.setColor(Color.yellow);
    g.fillRect(0, 0, width, height);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(3f));
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    //double x = 0.5 * width;
    //double y = 0.5 * height;
    double r = 0.75 * Math.min(x, y);



    double dx,dy;

    double t,l;
    l=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
   // System.out.println("!!l!!"+l);
    t= l/V;
    //System.out.println("!!t!!"+t);
    g2d.setColor(Color.black);
    if(T<t) {
        dx = ((x2 - x1) / (Math.sqrt(Math.pow(x2 - x1, 2)) + Math.pow(y2 - y1, 2)));
        //System.out.println("!!dx!!" + dx);
        dy = ((y2 - y1) / (Math.sqrt(Math.pow(x2 - x1, 2)) + Math.pow(y2 - y1, 2)));
        //System.out.println("!!dy!!" + dy);
        x += x1 + dx * V * T;//+ dx * (V * T);
        //System.out.println("!!x!!" + x);
        //System.out.println("!!x1!!" + x1);
        y += y1 + dy * V * T;// + dy * (V * T);
        r =  Math.max(0.1 * r, 5);
       // System.out.println("!!y!!" + y);
        //System.out.println("!!y1!!" + x1);

    }


    if (x==x2 && y == y2 && T>t) {

     dx = ((x2 - x1) / (Math.sqrt(Math.pow(x2 - x1, 2)) + Math.pow(y2 - y1, 2)));

        dy = ((y2 - y1) / (Math.sqrt(Math.pow(x2 - x1, 2)) + Math.pow(y2 - y1, 2)));

        x -= x1 + dx * V * T;//+ dx * (V * T);

        y -= y1 + dy * V * T;// + dy * (V * T);
        r = Math.max(0.1 * r, 5);


    }

    g2d.fill(circle(x,y,r));

    //if (x == x2 && y == y2)
    // x = x1 -
}

public Shape circle(double x, double y, double R){

    return new Ellipse2D.Double(x - r, y - r, 2 * r, 2 * r);
}

@Override
public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
        case "stop": {
            timer.stop();
            break;
        }
        case "start": {
            timer.start();
            break;
        }
        case "apply": {
            float ax1, ay1, bx2, by2, cv;
            try {
                ax1 = Float.parseFloat(fx1.getText());
                ay1 = Float.parseFloat(fy1.getText());
                bx2 = Float.parseFloat(fx2.getText());
                by2 = Float.parseFloat(fy2.getText());
                cv = Float.parseFloat(fV.getText());
                x1 = ax1;
                y1 = ay1;
                x2 = bx2;
                y2 = by2;
                V = cv;
                repaint();
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "Invalid input", "Error", 
JOptionPane.ERROR_MESSAGE);
            }
            break;
        }
        case "timer": {
            T += 0.6;
            System.out.println("!!T!!"+T);
            repaint();
            break;
   }
  }
 }
}
评论