package;import;import;import;import;import;import;import;import;import;publicclassTestextendsJFrame{/**serial*/privatestaticfinallongserialVersionUID=1L;booleanmousetag=false;booleancrosstag=false;intepointx=0;intepointy=0;inttype=0;intxdir=10;intxdir1=21;intydir1=113;intxdir2=10;intydir2=110;JPanelpanel=null;JPanelcarPanel=null;ThreaddrawThread=null;ThreadcarThread=null;publicTest(){panel=newJPanel(){/**serial*/privatestaticfinallongserialVersionUID=1L;@Overridepublicvoidpaint(Graphicsg){super.paint(g);Graphics2Dg2=(Graphics2D)g;g2.drawString(你是我的小龟!,200,20);//十字路口g2.drawLine(138,0,138,90);g2.drawLine(0,90,138,90);g2.drawLine(0,150,138,150);g2.drawLine(138,150,138,270);g2.drawLine(330,0,330,90);g2.drawLine(330,90,490,90);g2.drawLine(330,150,330,270);g2.drawLine(330,150,490,150);switch(type){case0:g2.setColor(Color.RED);g2.fillRoundRect(10,10,22,22,90,90);g2.setColor(Color.GREEN);g2.fillRoundRect(10,35,22,22,90,90);g2.setColor(Color.YELLOW);g2.fillRoundRect(10,60,22,22,90,90);break;case1:g2.setColor(Color.GREEN);g2.fillRoundRect(10,10,22,22,90,90);g2.setColor(Color.RED);g2.fillRoundRect(10,35,22,22,90,90);g2.setColor(Color.YELLOW);g2.fillRoundRect(10,60,22,22,90,90);break;case2:g2.setColor(Color.GREEN);g2.fillRoundRect(10,10,22,22,90,90);g2.setColor(Color.YELLOW);g2.fillRoundRect(10,35,22,22,90,90);g2.setColor(Color.RED);g2.fillRoundRect(10,60,22,22,90,90);break;default:break;}//carg2.setColor(Color.GREEN);g2.fillOval(xdir1,ydir1,8,8);g2.setColor(Color.BLACK);g2.fillOval(xdir2,ydir2,12,12);//mouseroundif(mousetag){g2.setColor(Color.RED);g2.drawOval(epointx-12,epointy-35,20,20);g2.drawLine((xdir1+xdir2)epointx-6,epointy/-2+6,(ydir2+6),25);xdir1=(xdir1+xdir2)/2+6;ydir1=(ydir1+6);}if(crosstag){g2.setColor(Color.RED);g2.drawString(哎哟!,xdir1,ydir1);}}};drawThread=newThread(){@Overridepublicvoidrun(){while(true){try{Thread.sleep(1000);if(type==0){type=1;}elseif(type==1){type=2;}elseif(type==2){type=0;}panel.repaint(0,0,50,100);}catch(InterruptedExceptione){e.printStackTrace();}}}};add(panel,BorderLayout.CENTER);if(drawThread.getState()=={return;}else{drawThread.start();}/////////////////////carThread=newThread(){@Overridepublicvoidrun(){while(true){try{}//自由开,遇到障碍物拐弯freeRunCar();Thread.sleep(50);repaint();catch(InterruptedExceptione){}}}privatevoidfreeRunCar(){//前进方向if(epointxxdir2){xdir1=xdir2+8;if(xdir2480)xdir2=0;xdir2+=1;}elseif(epointxxdir2){xdir1=xdir2-4;if(xdir2480)xdir2=0;xdir2-=1;}elseif(epointx==xdir2){xdir1=xdir2;if(xdir2480)xdir2=0;xdir2-=1;}if(epointy-25ydir2){ydir1=ydir2+8;if(ydir2330)ydir2=0;ydir2+=1;}elseif(epointy-25ydir2){ydir1=ydir2-5;if(ydir2330)ydir2=0;ydir2-=1;}elseif(epointy-25==ydir2){ydir1=ydir2;if(ydir2330)ydir2=0;ydir2-=1;}//碰撞检测138,90/138,150/330,90/330,150if(xdir1=138&&ydir1=90){crosstag=true;}elseif(xdir1=138&&ydir1=150){crosstag=true;}elseif(xdir1=330&&ydir1=90){crosstag=true;}elseif(xdir1=330&&ydir1=150){crosstag=true;}else{crosstag=false;}if(crosstag){epointx=250;}}};if(carThread.getState()=={return;}else{carThread.start();}//鼠标threadfinalThreadmouseThread=newThread(){@Overridepublicvoidrun(){while(true){if(mousetag){}}}@Overridepublicvoidinterrupt(){mousetag=false;}};if(mouseThread.getState()=={return;}else{mouseThread.start();}this.addMouseListener(newMouseListener(){@OverridepublicvoidmouseClicked(MouseEvente){//+,+e.getY());}@OverridepublicvoidmouseReleased(MouseEvente){epointx=e.getPoint().x;epointy=e.getPoint().y;mousetag=false;}@OverridepublicvoidmousePressed(MouseEvente){epointx=e.getPoint().x;epointy=e.getPoint().y;mousetag=true;}});}@OverridepublicvoidmouseExited(MouseEvente){}@OverridepublicvoidmouseEntered(MouseEvente){}publicstaticvoidmain(String[]args){Testtest=newTest();test.setSize(newDimension(500,300));test.setLocationRelativeTo(null);test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);test.setResizable(false);test.setVisible(true);}}