解題思路,用輸出語句來表示烏龜和兔子,因?yàn)橥米优艿每欤瑸觚斉艿寐@一點(diǎn)可以通過設(shè)置優(yōu)先級來體現(xiàn)。程序如下

創(chuàng)新互聯(lián)建站是專業(yè)的應(yīng)縣網(wǎng)站建設(shè)公司,應(yīng)縣接單;提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行應(yīng)縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
class Rabbit implements Runnable{//兔子線程
int counts;//用來表示路程,因?yàn)橘惻懿荒軣o限跑。
@Override
public void run() {
while (true) {
try {
Thread.sleep(1);// 兔子休息,也就是休眠。
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是兔子");
counts++;
if(counts==100)//表示到達(dá)終點(diǎn),跳出循環(huán)。
break;
}
}
}
class Tortoise implements Runnable{//烏龜線程
int counts;
@Override
public void run(){
while (true) {
System.out.println("我是烏龜");
counts++;
if(counts==100)//表示到達(dá)終點(diǎn),跳出循環(huán)。
break;
}
}
}
public class ThreadTest {
/**
* @param args
*/
public static void main(String[] args) {
Runnable rabbit=new Rabbit();
Runnable tortoise=new Tortoise();
Thread ra=new Thread(rabbit);//創(chuàng)建兔子線程。
Thread to=new Thread(tortoise);//創(chuàng)建烏龜線程。
//因?yàn)橥米拥囊艿帽葹觚斂欤@個可以通過設(shè)置優(yōu)先級來體現(xiàn)。
ra.setPriority(Thread.MAX_PRIORITY);//兔子有最高優(yōu)先級。
to.setPriority(1);//烏龜有較低的優(yōu)先級。
ra.start();//啟動線程
to.start();//啟動線程
}
}
運(yùn)行程序,因?yàn)闉觚斪巫尾痪耄赃€是賽跑贏了兔子嘿嘿。
直接將代碼復(fù)制就能運(yùn)行。畫的是一只烏龜
package Draw;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Tortoise1 {
public static void main(String args[]){
Frame w=new Frame();
w.setSize(1280,800);
TortoisePanel1 tp=new TortoisePanel1();
w.add(tp);
w.addMouseListener(tp);
tp.addMouseListener(tp);
w.addMouseMotionListener(tp);
tp.addMouseMotionListener(tp);
Thread? t=new Thread(tp);
t.start();
w.show();
}
}
class TortoisePanel1 extends Panel implements MouseListener,MouseMotionListener,Runnable{
int x = 30;
int y = 30 ;
int mx = 0;
int my = 0;
int flag=-1;
boolean b = true ;
public void paint(Graphics g){
super.paint(g) ;
this.setBackground(Color.LIGHT_GRAY) ;
if (b){
g.setColor(new Color( 80 , 180 , 80 )) ; //尾巴
g.fillOval( 140+x , 285+y , 150 , 150 ) ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillOval( 155+x , 300+y , 140 , 140) ;
g.fillOval( 195+x , 260+y , 200 ,240 ) ;
g.setColor(new Color( 80 , 180 , 80 )) ;
g.fillOval( 145+x , 355+y , 40 , 40) ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillOval( 157+x , 367+y , 15 , 15) ;
}else{
g.setColor(new Color( 80 , 180 , 80)) ; //尾巴
g.fillOval(13+x , 260+y , 150 , 150) ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillOval(3+x , 265+y , 143 , 143) ;
g.setColor(new Color( 80 , 180 , 80)) ;
g.fillOval(100+x,353+y,40,40) ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillOval(113+x , 365+y , 15 , 15) ;
}
if (b){
g.setColor(new Color(80,180,80)) ; //頭
g.fillOval( 120+x , 80+y , 60 , 100 ) ;
g.setColor(Color.WHITE) ;
g.fillOval( 130+x ,90+y , 10 ,10 ) ; //左眼
g.fillOval( 160+x ,90+y , 10 ,10 ) ; //右眼
g.setColor(Color.BLACK) ;
g.fillOval( 130+x ,90+y , 8 ,8 ) ; //左眼珠
g.fillOval( 160+x ,90+y , 8 ,8 ) ; //右眼珠
}else{
g.setColor(new Color(80,180,80)) ; //頭
g.fillOval( 120+x , 80+y , 60 , 100 ) ;
g.setColor(new Color(200,0,0)) ; //嘴
g.fillOval( 130+x , 100+y , 40 ,50 ) ;
g.setColor(new Color(80,180,80)) ;
g.fillOval( 130+x , 80+y , 40 ,50 ) ;
g.setColor(Color.BLACK) ;
g.drawLine( 143+x ,83+y , 140+x , 90+y ) ; //鼻子
g.drawLine( 158+x ,83+y , 160+x , 90+y ) ;
}
if (b){
g.setColor(new Color(80,180,80)) ;
g.fillOval( 50+x ,140+y ,50 , 70 ) ; //左爪
g.fillOval( 200+x ,140+y ,50 , 70 ) ; //右爪
g.fillOval( 50+x ,280+y ,50 , 70 ) ; //左后爪
g.fillOval( 200+x ,280+y ,50 , 70 ) ; //右后爪
}else{
g.setColor(new Color(80,180,80)) ; //左爪
g.fillOval( 50+x ,140+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ; //左指頭
g.fillOval( 55+x ,155+y ,5 , 7 ) ;
g.fillOval( 65+x ,148+y ,5 , 7 ) ;
g.fillOval( 75+x ,145+y ,5 , 7 ) ;
g.fillOval( 85+x ,150+y ,5 , 7 ) ;
g.fillOval( 65+x ,160+y ,20 , 20 ) ;
g.setColor(new Color(80,180,80)) ; //右爪
g.fillOval( 200+x ,140+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ; //右指頭
g.fillOval( 210+x ,150+y ,5 , 7 ) ;
g.fillOval( 220+x ,145+y ,5 , 7 ) ;
g.fillOval( 230+x ,148+y ,5 , 7 ) ;
g.fillOval( 240+x ,155+y ,5 , 7 ) ;
g.fillOval( 215+x ,160+y ,20 , 20 ) ;
g.setColor(new Color(80,180,80)) ; //左后爪
g.fillOval( 50+x ,280+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ; //左后指頭
g.fillOval( 50+x ,315+y ,5 , 7 ) ;
g.fillOval( 55+x ,328+y,5 , 7 ) ;
g.fillOval( 65+x ,340+y ,5 , 7 ) ;
g.fillOval( 80+x ,340+y ,5 , 7 ) ;
g.fillOval( 63+x ,310+y ,20 , 20 ) ;
g.setColor(new Color(80,180,80)) ; //右后爪
g.fillOval( 200+x ,280+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ; //右后指頭
g.fillOval( 245+x ,320+y ,5 , 7 ) ;
g.fillOval( 240+x ,330+y ,5 , 7 ) ;
g.fillOval( 230+x ,340+y ,5 , 7 ) ;
g.fillOval( 215+x ,338+y ,5 , 7 ) ;
g.fillOval( 215+x ,310+y ,20 , 20 ) ;
}
if (b){
g.setColor(new Color(50 ,160 , 50)) ; //大圈
g.fillOval( 60+x , 150+y , 180 ,200 ) ;
g.setColor(new Color(50,200,50)) ;
g.fillOval( 70+x , 160+y , 160 ,180 ) ; //小圈
g.setColor(new Color(0,0,0)) ;
g.drawLine( 130+x , 210+y , 170+x , 210+y ) ; //中間的花紋
g.drawLine( 170+x , 210+y , 200+x , 240+y ) ;
g.drawLine( 200+x , 240+y , 170+x , 270+y ) ;
g.drawLine( 170+x , 270+y , 130+x , 270+y ) ;
g.drawLine( 130+x , 270+y , 100+x , 240+y ) ;
g.drawLine( 100+x , 240+y , 130+x , 210+y ) ;??
g.drawLine( 100+x , 180+y , 130+x , 210+y ) ; //擴(kuò)散出來的花紋
g.drawLine( 170+x , 210+y , 200+x ,180+y ) ;
g.drawLine( 200+x , 240+y , 230+x ,250+y ) ;
g.drawLine( 170+x , 270+y , 200+x ,320+y ) ;
g.drawLine( 130+x , 270+y , 100+x ,320+y ) ;
g.drawLine( 100+x , 240+y , 70+x ,250+y ) ;
} else {
g.setColor(new Color(80,180,80)) ;
g.fillOval( 60+x , 150+y , 180 ,200 ) ; //大圈
g.setColor(new Color(255,230,240)) ;
g.fillOval( 70+x , 160+y , 160 ,180 ) ; //小圈
g.setColor(new Color(0,0,0)) ; //肚肚上的花紋
g.drawLine( 100+x ,180+y , 115+x , 195+y ) ;
g.drawLine( 200+x ,180+y , 185+x , 195+y ) ;
g.drawLine( 115+x ,195+y , 185+x , 195+y ) ;
g.drawLine( 100+x ,240+y , 75+x , 220+y ) ;
g.drawLine( 200+x ,240+y , 225+x , 220+y ) ;
g.drawLine( 100+x ,240+y , 200+x , 240+y ) ;
g.drawLine( 120+x ,290+y , 90+x , 310+y ) ;
g.drawLine( 180+x ,290+y , 210+x , 310+y ) ;
g.drawLine( 120+x ,290+y , 180+x , 290+y ) ;
g.drawLine( 150+x ,160+y , 150+x , 340+y ) ;
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
int x1,y1;
x1=e.getX();
y1=e.getY();
if(x1x){flag=1;}
if(x1x){flag=3;}
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
b=false;
repaint();
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
b=true;
repaint();
}
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
b=true;
//???System.out.println("鼠標(biāo)拖動");
//???System.out.println("X坐標(biāo):"+arg0.getX()+" Y坐標(biāo):"+arg0.getY());
x=arg0.getX();//獲得矩形右下角的X坐標(biāo)
y=arg0.getY();//獲得矩形右下角的Y坐標(biāo)
repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void run() {
// TODO Auto-generated method stub
if(flag==1){
x++;
}
if(flag==2){
y++;
}
if(flag==3){
x--;
}
if(flag==4){
y--;
}
}
}
點(diǎn)擊烏龜,可以翻身。
package test;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
class RunningAnimal extends Thread {
int distance;
int speed;
int sleeptime;
int length;
String name;
TestThread testthread;
boolean first = true;
public void run() {
while (distance length) {
distance += speed;
if(distance length)
distance = length;
if(name.equals("Turtle")){
testthread.turtleint = distance;
}else{
testthread.rabitint = distance;
System.out.println("rabit: " + testthread.rabitint + "\ntutle: " + testthread.turtleint);
}
testthread.repaint();
try {
sleep(sleeptime);
} catch (InterruptedException e) {
}
}
if(first){
testthread.repaint();
}
}
RunningAnimal(TestThread test, String aname, int alldistance, int aspeed, int asleeptime) {
name = aname;
length = alldistance;
speed = aspeed;
sleeptime = asleeptime;
distance = 0;
testthread = test;
}
}
public class TestThread extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
RunningAnimal turtle;
RunningAnimal rabit;
JButton start = new JButton("開始");
String turtlestr = "烏龜";
String rabitstr = "兔子";
int turtleint;
int rabitint;
public void init() {
setLayout(new BorderLayout());
turtle = new RunningAnimal(this, "Turtle", 500, 20, 100);
turtle.setPriority(7);
rabit = new RunningAnimal(this, "Rabit", 500, 300, 5000);
rabit.setPriority(3);
this.setLayout(new BorderLayout());
this.add(start, BorderLayout.SOUTH);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
turtle.start();
rabit.start();
start.setEnabled(false);
}
});
}
public void paint(Graphics g){
if(turtleint 0)
g.drawLine(20, 20, 20 + turtleint, 20);
g.drawOval(20 + turtleint, 20, 3, 3);
g.drawString(turtlestr, 20, 40);
if(rabitint 0)
g.drawLine(20, 60, 20 + rabitint, 60);
g.drawOval(20 + rabitint, 60, 3, 3);
g.drawString(rabitstr, 20, 80);
}
}
因?yàn)槭蔷毩?xí),所以沒有講究效率等,只考慮了你說的功能,大體意思也就是調(diào)用applet的paint功能不斷的去重繪
jose 不動 ,maria forward(40) turn(-90)
這是java 中的方法傳參問題 ,在java中參數(shù)類型是引用類型,傳的是這個引用參數(shù)的引用的副本,在dosth()中,這個引用turtle指向了maria的地址,改變的都是maria值
首先,手動畫一個小烏龜,如下:
然后,按照J(rèn)ava繪圖基本步驟一步步來。
swing 編程步驟:
1. 繼承JFrame
2. 定義組件
3.創(chuàng)建組件(構(gòu)造函數(shù))
4.添加組件
5.對窗體設(shè)置
6.顯示窗體
最終效果如下:
代碼如下:
/**?
*?功能:畫一個烏龜?
*/??
package?com.test1;??
import?java.awt.*;??
import?javax.swing.*;??
public?class?MyTortoise??extends?JFrame{??
MyPanel2?mp?=?null;??
//構(gòu)造函數(shù)??
public?MyTortoise(){??
mp?=?new?MyPanel2();??
this.add(mp);??
this.setTitle("小烏龜,丑丑噠");??
this.setSize(400,300);??
this.setVisible(true);??
this.setLocation(300,200);??
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
}??
public?static?void?main(String[]?args)?{??
MyTortoise?mtg?=?new?MyTortoise();??
}?????
}??
//我的面板。只有JPanel有畫圖方法,JFrame沒有,故必須在JFrame中添加JPanel??
class?MyPanel2?extends?JPanel{??
//定義一個烏龜??
Tortoise?t?=?null;??
//構(gòu)造函數(shù)??
public?MyPanel2(){????
t?=?new??Tortoise(100,100);??
}??
//畫烏龜??
public?void?drawTortoise(int?x,?int?y,?Graphics?g){??
//1.畫臉??
g.setColor(Color.green);??
g.fillOval(x+60,?y,?30,?15);??
//2.畫左眼??
g.setColor(Color.black);??
g.fillOval(x+65,?y+3,?5,?5);??
//3.畫右眼??
g.fillOval(x+78,?y+3,?5,?5);??
//4.畫脖子??
g.setColor(Color.green);??
g.fillOval(x+70,?y,?10,?42);??
//5.畫烏龜殼??
g.setColor(Color.red);??
g.fillOval(x+40,?y+40,?70,?100);??
//6.畫左上腳??
g.setColor(Color.green);??
g.fillOval(x+15,?y+60,?30,?10);??
//7.畫右上腳??
g.fillOval(x+105,?y+60,?30,?10);??
//8.畫左下腳??
g.fillOval(x+15,?y+110,?30,?10);??
//9.畫右下腳??
g.fillOval(x+105,?y+110,?30,?10);??
//10.畫尾巴??
g.setColor(Color.black);??
g.drawLine(x+70,y+140,x+130,y+210);??
g.drawOval(x+95,?y+150,?30,?30);??
}??
//覆蓋JPanel的paint方法??
//Graphics?是繪圖的重要類。你可以把他理解成一只畫筆??
public?void?paint(Graphics?g){??
//1.調(diào)用父類函數(shù)完成初始化任務(wù)??
//這句話不能少??
super.paint(g);??
//2.畫烏龜,調(diào)用方法即可??
this.drawTortoise(50,?50,?g);??
}??
}??
//定義一個烏龜類??
class?Tortoise?{??
//表示烏龜?shù)臋M坐標(biāo)??
int?x?=?0;??
//表示烏龜?shù)目v坐標(biāo)??
int?y?=?0;??
public?int?getX()?{??
return?x;??
}??
public?void?setX(int?x)?{??
this.x?=?x;??
}??
public?int?getY()?{??
return?y;??
}??
public?void?setY(int?y)?{??
this.y?=?y;??
}??
public?Tortoise(int?x,?int?y){??
this.x?=?x;??
this.y?=?y;??
}??
}
import java.util.*;
public class Test{
public static void main(String[] args)throws Exception {
float t,g,m=0,num=0;
float tt,gg;
int q=0;
Scanner s;
System.out.println("輸入兔子跑一圈時間/秒:");
s=new Scanner(System.in);
t=s.nextFloat();
System.out.println("輸入烏龜跑一圈時間/秒:");
s=new Scanner(System.in);
g=s.nextFloat();
tt=(float)1/(t*1000);
gg=(float)1/(g*1000);
System.out.println("賽跑開始……");
while(true){
try{
Thread.sleep(10);
}catch(Exception e){}
m+=10;
if((int)((tt-gg)*m)q){
q=(int)((tt-gg)*m);
System.out.println("在第"+m/1000+"秒");
System.out.println("兔子超過烏龜"+q+"圈");
}
}
}}
文章題目:java小烏龜尾巴代碼,java畫烏龜代碼
鏈接地址:http://chinadenli.net/article32/dsgsepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、、網(wǎng)站排名、搜索引擎優(yōu)化、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)