本人一自学JAVA的新手,想做一物体绕中心点旋转。
但做出来的东西却不对。。希望哪位大侠教下我该怎么改。。
以下就是我写的:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Zuo19_2 extends Applet implements ActionListener,Runnable{
Ellipse2D[] p=new Ellipse2D.Double[2];
Line2D l;
Graphics2D ga,gb;
int s=0;
Image buff;
Thread ballThread;
Button b1=new Button("快速");
Button b2=new Button("慢速");
public void init(){
buff=createImage(getWidth(),getHeight());
gb=(Graphics2D)buff.getGraphics();
p[0]=new Ellipse2D.Double(200,50,40,40);
p[1]=new Ellipse2D.Double(206,150,15,15);
l=new Line2D.Double(185,150,180,90);
setLayout(new BorderLayout());
Panel p=new Panel();
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
add(p,"South");
b1.addActionListener(this);
b2.addActionListener(this);
}
public void paint(Graphics g){
if(ga==null)
ga=(Graphics2D)getGraphics();
drawScreen();
}
public void drawScreen(){
gb.clearRect(0,0,getWidth(),getHeight());
gb.draw(l);
gb.setPaint(Color.red);
for(int i=0;i<2;i++)
gb.fill(p[i]);
ga.drawImage(buff,0,0,this);
}
public void run(){
while(true){
gb.rotate(1);
drawScreen();
try{
Thread.sleep(s);
}catch(Exception e){}
}
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==b1)
s=100;
else if(ae.getSource()==b2)
s=1000;
ballThread=new Thread(this);
ballThread.start();
}
}