Движение картинки хаотично со скоростью V

Помогите добавить еще один объект на frame (например картинку), и что бы он двигался хаотично со скоростью V . Хаотичность достигается случайной изменением направления движения раз в N секунд. Работаю с графикой и потоками впервые,буду очень благодарен за любую помощь.

Код:

public class LabSevenFirst extends JPanel implements ActionListener{

    public static double angle=0;
    private JFrame wnd;
    private float V, R;
    private int t0;
    private Timer timer;
    private JButton start, stop, apply;
    private JLabel vl, rl,stud1,stud2;
    private JTextField vtf, rtf;
    public double[] b = {0, 310, 210, 5, 2};
     public Image image;
     public JLayeredPane swap;


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

        System.out.println(Thread.currentThread().getName());
    }


    LabSevenFirst() {
    wind();
    }


     public void wind()
     {
         t0 = 30;
         timer = new Timer(t0, this);
         timer.setActionCommand("timer");
         wnd = new JFrame("Lab-7.1");
         wnd.setLayout(null);
         wnd.setSize(500, 500);
         wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setBounds(100, 50, 300, 300);
         start = new JButton("Start");
         stop = new JButton("Stop");
         apply = new JButton("Apply");
         vl = new JLabel("V");
         rl = new JLabel("R");
         vtf = new JTextField(V + "");
         rtf = new JTextField(R + "");
         vl.setBounds(5, 380, 20, 20);
         rl.setBounds(5, 400, 20, 20);
         vtf.setBounds(30, 380, 40, 20);
         rtf.setBounds(30, 400, 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(5, 430, 80, 20);
         wnd.add(this);
         wnd.add(start);
         wnd.add(stop);
         wnd.add(apply);
         wnd.add(vl);
         wnd.add(rl);
         wnd.add(vtf);
         wnd.add(rtf);
         wnd.setVisible(true);
     }

   @Override
    protected void paintComponent(Graphics g) {
        int width = getWidth();
        int height = getHeight();
        g.setColor(Color.lightGray);
        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;

        g2d.setColor(Color.red);
        x += R * Math.cos(V*angle);
        y += R * Math.sin(V*angle);
       double r =  Math.max(0.1 * R, 5);
       g2d.fill(circle(x, y, r));


    }


    private 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 a, b;
                try {
                    a = Float.parseFloat(vtf.getText());
                    b = Float.parseFloat(rtf.getText());
                    V = a;
                    R = b;
                    repaint();
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
                }
                break;
            }
            case "timer" : {
                angle += 0.05;
                repaint();
                break;
            }
        }
    }
}

В коде есть движение точки по окружности (это то что я сделал). Насколько я понимаю,нужно добавить другой поток в котором будет новый объект . И потом передать его в основной поток. В этом и моя другая проблема.


Ответы (0 шт):