java画乌龟源代码-郭遥航.doc
/*JAVA基本功小练习用java语言描述小王八用鼠标可以拖动小乌龟进行移动选中乌龟时可以显示小乌龟的腹面*/importjava.awt.*;importjavax.swing.*;importjava.awt.event.MouseMotionListener;importjava.awt.event.MouseListener;importjava.awt.event.MouseEvent;publicclassMyTest{publicstaticvoidmain(Stringargs[]){JFramew=newJFrame();w.setSize(1280,800);w.setBackground(Color.CYAN);MyPanelmp=newMyPanel();mp.setBackground(Color.CYAN);w.add(mp);w.addMouseMotionListener(mp);mp.addMouseMotionListener(mp);w.addMouseListener(mp);mp.addMouseListener(mp);w.setVisible(true);}}classMyPanelextendsJPanelimplementsMouseMotionListener,MouseListener{intx=500;inty=300;booleanisRight=true;publicvoidpaint(Graphicsg){super.paint(g);//画乌龟正面g.setColor(newColor(50,200,0));//画四肢g.fillOval(x,y,35,50);g.fillOval(x+105,y,35,50);g.fillOval(x,y+110,35,50);g.fillOval(x+105,y+110,35,50);if(isRight==false){g.setColor(Color.RED);//画背面脚掌g.fillOval(x+5,y+15,15,15);g.fillOval(x+5,y+8,5,5);//周围的小圆g.fillOval(x+14,y+5,5,5);//周围的小圆g.fillOval(x+20,y+13,5,5);//周围的小圆g.fillOval(x+120,y+15,15,15);g.fillOval(x+5+110,y+10,5,5);//周围的小圆g.fillOval(x+14+110,y+5,5,5);//周围的小圆g.fillOval(x+20+112,y+10,5,5);//周围的小圆g.fillOval(x+3,y+128,15,15);g.fillOval(x+5,y+8+135,5,5);//周围的小圆g.fillOval(x+12,y+5+142,5,5);//周围的小圆g.fillOval(x+20,y+13+130,5,5);//周围的小圆g.fillOval(x+120,y+125,15,15);g.fillOval(x+5+110,y+8+135,5,5);//周围的小圆g.fillOval(x+12+110,y+5+142,5,5);//周围的小圆g.fillOval(x+20+110,y+13+130,5,5);//周围的小圆}g.setColor(newColor(50,200,0));g.fillOval(x+50,y-40,40,60);//画龟头if(isRight){//画眼白g.setColor(Color.WHITE);g.fillOval(x+55,y-30,10,10);g.fillOval(x+79,y-30,10,10);//画眼黑g.setColor(Color.BLACK);g.fillOval(x+55,y-30,6,6);g.fillOval(x+79,y-30,6,6);}else{g.setColor(Color.RED);//画红嘴g.fillOval(x+59,y-26,23,25);g.setColor(newColor(50,200,0));//遮盖露出月牙红嘴g.fillOval(x+58,y-35,25,25);g.setColor(Color.BLACK);g.drawLine(x+65,y-35,x+62,y-30);g.drawLine(x+73,y-35,x+77,y-30);}if(isRight){g.setColor(newColor(50,200,0));//画尾巴g.fillOval(x+65,y+130,35,90);g.setColor(Color.CYAN);g.fillOval(x+75,y+130,30,100);}else{g.setColor(newColor(50,200,0));//画尾巴g.fillOval(x+45,y+130,35,90);g.setColor(Color.CYAN);g.fillOval(x+39,y+130,30,100);}g.setColor(newColor(50,160,50));//画龟身g.fillOval(x,y,140,160);//画鬼身大圈if(isRight){g.setColor(newColor(50,200,0));//画龟身小圈g.fillOval(x+10,y+10,120,140);g.setColor(Color.BLACK);g.drawLine(x+50,y+60,x+90,y+60);g.drawLine(x+50,y+100,x+90,y+100);g.drawLine(x+50,y+60,x+30,y+80);g.drawLine(x+90,y+60,x+110,y+80);g.drawLine(x+50,y+100,x+30,y+80);g.drawLine(x+90,y+100,x+110,y+80);g.drawLine(x+50,y+60,x+30,y+28);g.drawLine(x+90,y+60,x+110,y+28);g.drawLine(x+30,y+80,x+11,y+75);g.drawLine(x+110,y+80,x+129,y+75);g.drawLine(x+50,y+100,x+38,y+140);g.drawLine(x+90,y+100,x+102,y+140);}else{g.setColor(newColor(255,230,230));//画龟身小圈g.fillOval(x+10,y+10,120,140);g.setColor(Color.BLACK);g.drawLine(x+50,y+60,x+90,y+60);//六边形上线g.drawLine(x+50,y+100,x+90,y+100);//六边形下线//g.drawLine(x+50,y+60,x+30,y+80);//六边形上左//g.drawLine(x+90,y+60,x+110,y+80);//六边形上右//g.drawLine(x+50,y+100,x+30