Animacja piłek

0

Witam,
Chcialem zrobic animację pilek na zasadzie ze podaje predkosc i kat pod jakim pilka wystartuje. Mam sama animacje i teraz chce dodac kąt pod jakim wystartuje pilka no i predkosc z jaka sie bedzie poruszala. No i wlasnie potrzebuje podpowiedzi co do sposobu tego wykonania. Moze ktos z was robil cos podobnego to proszę o wskazowki.

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class BounceThread {
  public static void main(String[] args) {
    JFrame frame = new BounceThreadFrame();
    frame.show();
  }
}

class BounceThreadFrame extends JFrame {
  public BounceThreadFrame() {
    setSize(300, 200);
    setTitle("Bounce");

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = getContentPane();
    canvas = new JPanel();
    contentPane.add(canvas, "Center");
    JPanel p = new JPanel();
    addButton(p, "Start", new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        Ball b = new Ball(canvas);
        b.start();
      }
    });

    addButton(p, "Close", new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        canvas.setVisible(false);
        System.exit(0);
      }
    });
    contentPane.add(p, "South");
  }

  public void addButton(Container c, String title, ActionListener a) {
    JButton b = new JButton(title);
    c.add(b);
    b.addActionListener(a);
  }

  private JPanel canvas;
}

class Ball extends Thread {
  public Ball(JPanel b) {
    box = b;
    
  }
  
public void move() {
    if (!box.isVisible())
      return;
    Graphics g = box.getGraphics();
    g.setXORMode(box.getBackground());
    g.fillOval(x, y, XSIZE, YSIZE);
    
    x += dx;
    y += dy;
    Dimension d = box.getSize();
    if (x < 0) {
      x = 0;
      dx = -dx;
    }
    if (x + XSIZE >= d.width) {
      x = d.width - XSIZE;
      dx = -dx;
    }
    if (y < 0) {
      y = 0;
      dy = -dy;
    }
    if (y + YSIZE >= d.height) {
      y = d.height - YSIZE;
      dy = -dy;
    }
    g.fillOval(x, y, XSIZE, YSIZE);
    g.dispose();
  }

  public void run() {
    try {
      //draw();
      for (int i = 1; i <= 11100; i++) {
        move();
        sleep(1);
      }
    } catch (InterruptedException e) {
    }
  }

  private JPanel box;

  private static final int XSIZE = 10;

  private static final int YSIZE = 10;

  private int x = 0;

  private int y = 0;

  private int dx = 2;

  private int dy = 2;
}
0

Pitagoras to mial leb nie od parady. Ponizej twoja klasa Ball ze zmienionym konstruktorem. V nie powinno byc zbyt male (<10) bo wtedy rzeczywisty kat moze byc nie zawsze taki jak bysmy chcieli albo dx i/lub dy wyjdzie zero i pileczka bedzie stala. Jesli chcialbys uzyskac wolny i płynny ruch pileczki x,y,dx i dy powinny rzeczywiste a nie calkowite.

class Ball extends Thread {
    /**
     * 
     * @param b panel
     * @param V predkosc w pikselach na jeden ruch
     * @param k - kat w stopniach. Np w prawo 0 lub 360stopni, na dol 270stopni 
     */
  public Ball(JPanel b, int V, double k) {
    box = b;
    k = ((k*Math.PI)/180);
    dy = (int) (Math.sin(k) * V);
    dx = (int) (Math.cos(k) * V);
    
  }
  
public void move() {
    if (!box.isVisible())
      return;
    Graphics g = box.getGraphics();
    g.setXORMode(box.getBackground());
    g.fillOval(x, y, XSIZE, YSIZE);
    
    x += dx;
    y += dy;
    Dimension d = box.getSize();
    if (x < 0) {
      x = 0;
      dx = -dx;
    }
    if (x + XSIZE >= d.width) {
      x = d.width - XSIZE;
      dx = -dx;
    }
    if (y < 0) {
      y = 0;
      dy = -dy;
    }
    if (y + YSIZE >= d.height) {
      y = d.height - YSIZE;
      dy = -dy;
    }
    g.fillOval(x, y, XSIZE, YSIZE);
    g.dispose();
  }

  public void run() {
    try {
      //draw();
      for (int i = 1; i <= 11100; i++) {
        move();
        sleep(50);
      }
    } catch (InterruptedException e) {
    }
  }

  private JPanel box;

  private static final int XSIZE = 10;

  private static final int YSIZE = 10;

  private int x = 0;

  private int y = 0;

  private int dx = 2;

  private int dy = 2;
}
0

Dzięki wielkie za pomoc. [browar]

1 użytkowników online, w tym zalogowanych: 0, gości: 1