0
我想做一個子彈類。當它被調用時,它會得到一個方向和一個初始位置。問題是方向不起作用,無論我設定爲什麼方向,它都只會上升。旋轉圖形
請幫忙。
在此先感謝
public class Bullet extends JComponent implements ActionListener{
private int bx,by;
private double theta;
private double BvelX, BvelY;
private Timer timer = new Timer(8,this);
public Bullet(int bx, int by, double theta)
{
this.bx = bx;
this.by = by;
this.theta = theta;
BvelX = 0;
BvelY = -1;
timer.start();
revalidate();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D graphicsS = (Graphics2D) g;
graphicsS.rotate(theta, bx, by);
graphicsS.setColor(Color.RED);
graphicsS.fillOval(bx, by, 8, 8);
}
public void actionPerformed(ActionEvent e)
{
bx += BvelX;
by += BvelY;
/*by += 5*(Math.sin(theta));
bx += 5*(Math.cos(theta));*/
revalidate();
repaint();
}
}
我也試過,但方向是關閉的 – user2378547 2013-05-13 21:27:54
而且我也有一個與graphicsS.rotate(theta,bx,by)的旋轉;怎麼沒有做什麼? – user2378547 2013-05-13 21:28:59
,因爲你永遠不會更新'theta',所以它保持不變。您需要按照「bx」和「by」的方式進行操作。 – 2013-05-13 21:44:52