import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import javax.swing.JFrame;public class Test extends JFrame{
int x=0;
   int y;
public void tt(){


this.setLocation(100,100);
     this.setSize(800,600); this.show();

 new Thread(new PaintThread()).start();
}
   
  
public void paint(Graphics g){

     super.paint(g);
     Color color=new Color(0,117,58);
     g.setColor(color);
     //g.drawOval(135+x, 170+y, 80+x, 100+y);
     g.fillOval(135+x, 170+y, 80, 100);
     color=new Color(238,238,238);
     g.setColor(color);
     //g.drawOval(140, 170, 80, 100);
     g.fillOval(140+x, 170+y, 80, 100);
    
     color=new Color(0,117,58);
     g.setColor(color);
     //g.drawOval(140, 220, 30,30);
     g.fillOval(140+x, 220+y, 30,30);
    
    
     color=new Color(238,238,238);
     g.setColor(color);
     //g.drawOval(147, 225, 18, 18);
     g.fillOval(147+x, 225+y, 18, 18);
    
    
    
    
      color=new Color(0,117,58);
     g.setColor(color);
     //g.drawOval(100, 100, 80, 100);
     g.fillOval(100+x, 100+y, 80, 100);
     //g.drawLine(100, 100, 80, 80);
     color=new Color(0,117,58);
     g.setColor(color);
     g.fillOval(105+x, 100+y, 13, 23);
     //g.drawOval(105, 100, 13, 23);//左前脚
     color=new Color(0,117,58);
     g.setColor(color);
     g.fillOval(130+x, 80+y, 23, 60);
     //g.drawOval(130, 80, 23, 60);//头
    
    
     color=new Color(238,238,238);
     g.setColor(color);
     g.fillOval(135+x, 84+y, 4,4);
     //g.drawOval(135, 84, 4,4);//左眼
    
     color=new Color(0,0,0);
     g.setColor(color);
     g.fillOval(136+x, 85+y, 2,2);
     //g.drawOval(136, 85, 2,2);//左眼球
    
    
    
     color=new Color(238,238,238);
     g.setColor(color);
     g.fillOval(144+x, 84+y, 4,4);
     //g.drawOval(144, 84, 4,4);//右眼
    
    
    
     color=new Color(0,0,0);
     g.setColor(color);
     g.fillOval(145+x, 85+y, 2,2);
     //g.drawOval(145, 85, 2,2);//右眼球
    
    
    
     color=new Color(0,117,58);
     g.setColor(color);
     g.fillOval(160+x, 100+y, 13, 23);
    
     //g.drawOval(160, 100, 13, 23);//右前脚
    
     color=new Color(0,117,58);
     g.setColor(color);
     g.fillOval(98+x, 170+y, 13, 23);     //g.drawOval(98, 170, 13, 23);//左后脚
    
     color=new Color(0,117,58);
     g.setColor(color);
     g.fillOval(171+x, 170+y, 13, 23);
     //g.drawOval(171, 170, 13, 23);//右后脚
     color=new Color(23,23,23);
     g.setColor(color);
     //g.fillOval(105, 100, 13, 23);
     g.drawLine(130+x, 130+y, 150+x, 130+y);//——
     g.drawLine(150+x, 130+y, 158+x, 150+y);// \
     g.drawLine(158+x, 150+y, 150+x, 170+y);// /
     g.drawLine(150+x, 170+y, 130+x, 170+y);// 下——
        g.drawLine(130+x, 170+y, 122+x, 150+y);// 下 \
        g.drawLine(122+x, 150+y, 130+x, 130+y);// 下 /
        g.drawLine(130+x, 130+y, 117+x, 110+y);
        g.drawLine(150+x, 130+y, 160+x, 110+y);
        g.drawLine(130+x, 170+y, 119+x, 190+y);
        g.drawLine(150+x, 170+y, 160+x, 190+y);
        g.drawLine(158+x, 150+y, 180+x, 150+y);
        g.drawLine(122+x, 150+y, 100+x, 150+y);
         y-=5;
//         if(171+x==0){
//        y-=5;
//         paint(g);
//         return;
//      }
}
public static void main(String[] args) {
Test test=new Test();
//test.setSize(300, 300);
test.tt();
 

}

private class PaintThread implements Runnable
{    public void run() {
   while(true)
    {
     repaint();//线程中使用重画
     try {
      Thread.sleep(50);//每隔50毫秒
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
   
  }
  
}
}
这是小弟的代码,乌龟已经画好了,怎么能让它自己绕着动起来呢,求助啊,很紧急,谢谢各位了!!!

解决方案 »

  1.   

    使用 GeneralPath 表示乌龟。用 AffineTransform 旋转/移动位置。javax.swing.Timer 定时更新位置。
      

  2.   

    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;import javax.swing.JFrame;public class BallCanvas extends Canvas implements Runnable { private static final long serialVersionUID = 1L;
        private int h = 0;// 高度
        private final float G = 9.8f;// 重力加速度
        private final int D = 20;// 小球直径
        private float t = 0;// 时间
        private int x = 0;// 水平位移
        private int v = 36;// 平抛速度    public BallCanvas() {
            new Thread(this).start();
        }    @Override
        public void run() {
            while (true) {
                t += 0.2;
                x = (int) (v * t);// 水平位移
                h = (int) (G * t * t / 2);// 下落高度
                repaint();
                if (x > this.getWidth() || h > this.getHeight()) {
                    t = 0;
                    x = 0;
                    h = 0;
                }            try {
                    Thread.sleep(85);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }    @Override
        public void paint(Graphics g) {
            // 重新绘制屏幕
            // g.setColor(Color.white);
            // g.fillRect(0, 0, this.getWidth(), this.getHeight());        g.setColor(Color.red);
            g.fillArc(x, h, D, D, 0, 360);        g.setColor(Color.blue);
            g.fillArc(0, h, D, D, 0, 360);    }    /**
         * @param args
         */
        public static void main(String[] args) {
            JFrame frame = new JFrame("我的小球");
            Container con = frame.getContentPane();
            con.add(new BallCanvas(), BorderLayout.CENTER);
            frame.setPreferredSize(new Dimension(400, 400));
            frame.setVisible(true);
            frame.validate();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
        }
    }这个是别人写,2个球运动的,你把这个看懂了,应该对你的有帮助吧!