资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java小乌龟尾巴代码,java画乌龟代码

java编个小程序

解题思路,用输出语句来表示乌龟和兔子,因为兔子跑得快,乌龟跑得慢,这一点可以通过设置优先级来体现。程序如下

创新互联建站是专业的应县网站建设公司,应县接单;提供成都网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行应县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

class Rabbit implements Runnable{//兔子线程

int counts;//用来表示路程,因为赛跑不能无限跑。

@Override

public void run() {

while (true) {

try {

Thread.sleep(1);// 兔子休息,也就是休眠。

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("我是兔子");

counts++;

if(counts==100)//表示到达终点,跳出循环。

break;

}

}

}

class Tortoise implements Runnable{//乌龟线程

int counts;

@Override

public void run(){

while (true) {

System.out.println("我是乌龟");

counts++;

if(counts==100)//表示到达终点,跳出循环。

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);//创建兔子线程。

Thread to=new Thread(tortoise);//创建乌龟线程。

//因为兔子的要跑得比乌龟快,这个可以通过设置优先级来体现。

ra.setPriority(Thread.MAX_PRIORITY);//兔子有最高优先级。

to.setPriority(1);//乌龟有较低的优先级。

ra.start();//启动线程

to.start();//启动线程

}

}

运行程序,因为乌龟孜孜不倦,所以还是赛跑赢了兔子嘿嘿。

程序大神速来 java画图问题

直接将代码复制就能运行。画的是一只乌龟

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 ) ; //扩散出来的花纹

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("鼠标拖动");

//   System.out.println("X坐标:"+arg0.getX()+" Y坐标:"+arg0.getY());

x=arg0.getX();//获得矩形右下角的X坐标

y=arg0.getY();//获得矩形右下角的Y坐标

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--;

}

}

}

点击乌龟,可以翻身。

求龟兔赛跑JAVA代码

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);

}

}

因为是练习,所以没有讲究效率等,只考虑了你说的功能,大体意思也就是调用applet的paint功能不断的去重绘

java基础代码,求问那句maria.doSth(jose)的影响,两个乌龟分别怎么动(蓝色是jo

jose 不动 ,maria forward(40) turn(-90)

这是java 中的方法传参问题 ,在java中参数类型是引用类型,传的是这个引用参数的引用的副本,在dosth()中,这个引用turtle指向了maria的地址,改变的都是maria值

java画乌龟

首先,手动画一个小乌龟,如下:

然后,按照Java绘图基本步骤一步步来。

swing 编程步骤:

1. 继承JFrame

2. 定义组件

3.创建组件(构造函数)

4.添加组件

5.对窗体设置

6.显示窗体

最终效果如下:

代码如下:

/** 

* 功能:画一个乌龟 

*/  

package com.test1;  

import java.awt.*;  

import javax.swing.*;  

public class MyTortoise  extends JFrame{  

MyPanel2 mp = null;  

//构造函数  

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;  

//构造函数  

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.调用父类函数完成初始化任务  

//这句话不能少  

super.paint(g);  

//2.画乌龟,调用方法即可  

this.drawTortoise(50, 50, g);  

}  

}  

//定义一个乌龟类  

class Tortoise {  

//表示乌龟的横坐标  

int x = 0;  

//表示乌龟的纵坐标  

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;  

}  

}

求java龟兔赛跑程序代码!

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://cdkjz.cn/article/dsgsepc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220