| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2462 人关注过本帖
标题:请教大神 如何让兔子和乌龟一起动
只看楼主 加入收藏
fan19980613
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-11-12
结帖率:0
收藏
 问题点数:0 回复次数:8 
请教大神 如何让兔子和乌龟一起动
现在只有乌龟自己跑  兔子不跑
代码运行没有错误


package 实验6;
import javax.swing.*;
import *;
import java.awt.*;
import *;
class Animals extends JPanel implements Runnable{
    Image backpic, rabbit, tortoise;  
    int x1=0,y1=20,x2=0,y2=150;
    int x=0;
    int rabbitlength = 0;
    int tortoiselength=0;
    int rabbittime=0;
    int tortoisetime = 0;
    public void paint(Graphics g) {   
        g.setColor(Color.WHITE);
        g.fillRect(0,0,500,300);
        rabbit = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/rabbit.jpg"));
        tortoise = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tortoise.jpg"));
        g.drawImage(rabbit, x1, y1, 30, 100, this);
        g.drawImage(tortoise,x2,y2 ,30,100, this);
        
      
   }
    public void run() {
        
        
        boolean flag = true;
        while(flag) {
            try {  
                Thread.sleep(100);  
            } catch (InterruptedException ex) {  
            ex.printStackTrace();
            }  
            String name = Thread.currentThread().getName();
            if(name.equals("兔子")) {
                rabbitlength= rabbitlength+10;
                rabbittime = rabbittime+5;
                x1+=10;
                if(rabbitlength%80==0) {
                    try {
                        Thread.sleep(2000);
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("兔子休息");
                    rabbittime = rabbittime+20;
                }
                System.out.println(name+"已经跑了"+rabbitlength+"米");
                if(rabbitlength==300) {
                    System.out.println("兔子到达了终点,用时为:"+rabbittime+"秒");
                    flag = false;
                }
                repaint();
            }
            if(name.equals("乌龟")) {
                tortoiselength=tortoiselength+5;
                tortoisetime = tortoisetime+3;
                x2+=5;
                System.out.println(name+"已经跑了"+tortoiselength+"米");
                if(tortoiselength==300) {
                    System.out.println("乌龟到达了终点,用时为:"+tortoisetime+"秒");
                    flag = false ;
                }
                repaint();
            
            }
            
        }
}
}
public class RabbitTortoiseRace {

    public static void main(String args[]) {
        Animals rabbit = new Animals();
        Animals tortoise= new Animals();
        new Thread(rabbit,"兔子").start();
        new Thread(tortoise,"乌龟").start();
        JFrame f = new JFrame("龟兔赛跑游戏");
        f.add(rabbit);
        f.add(tortoise);
        f.setSize(400, 300);
        f.setVisible(true);
    }

     

}




搜索更多相关主题的帖子: 兔子 int System out println 
2018-04-18 20:18
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
rabbit = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/rabbit.jpg"));
        tortoise = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tortoise.jpg"));

这两张图呢

假如人生没有梦想,和咸鱼有什么区别!
2018-04-18 20:44
fan19980613
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-11-12
收藏
得分:0 
回复 2楼 疯狂的小a
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册

sorry  忘记了
2018-04-18 20:50
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
class Animals extends JPanel implements Runnable{
    Image backpic, rabbit, tortoise;  
    static int x1=0,y1=20,x2=0,y2=150;
    int x=0;
    int rabbitlength = 0;
    int tortoiselength=0;
    int rabbittime=0;

这边是成员变量,没共享吧,改成类变量试试呢?
代码需要整理一下呢

剑栈风樯各苦辛,别时冰雪到时春
2018-04-18 22:53
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
回复 4楼 林月儿
目测可以

假如人生没有梦想,和咸鱼有什么区别!
2018-04-19 08:37
fan19980613
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-11-12
收藏
得分:0 
回复 4楼 林月儿
改成你这样的 兔子真的动了    可是怎么我在frame中加一个对象 也是两个都在跑
JFrame f = new JFrame("龟兔赛跑游戏");
        f.add(rabbit);
        //f.add(tortoise);  这句话注释了 或者上面那句话 注释  两个都跑  这是怎么回事
2018-04-20 11:02
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
两个都不加也会跑的吧,首先加载图片不是这边的代码,其次对象移动也不是这两句能管用的

剑栈风樯各苦辛,别时冰雪到时春
2018-04-20 19:49
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
程序代码:
package 实验6;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Animals extends JPanel implements Runnable {
    Image backpic, rabbit, tortoise;
    static int x1 = 0, y1 = 20, x2 = 0, y2 = 150;
    int x = 0;
    int rabbitlength = 0;
    int tortoiselength = 0;
    int rabbittime = 0;
    int tortoisetime = 0;

    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 500, 300);
        rabbit = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/rabbit.jpg"));
        tortoise = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tortoise.jpg"));
        g.drawImage(rabbit, x1, y1, 30, 100, this);
        g.drawImage(tortoise, x2, y2, 30, 100, this);

    }

    public void run() {

        boolean flag = true;
        while (flag) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            String name = Thread.currentThread().getName();
            if (name.equals("兔子")) {
                rabbitlength = rabbitlength + 10;
                rabbittime = rabbittime + 5;
                x1 += 10;
                if (rabbitlength % 80 == 0) {
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("兔子休息");
                    rabbittime = rabbittime + 20;
                }
                System.out.println(name + "已经跑了" + rabbitlength + "米");
                if (rabbitlength == 300) {
                    System.out.println("兔子到达了终点,用时为:" + rabbittime + "秒");
                    flag = false;
                }
                repaint();
            }
            if (name.equals("乌龟")) {
                tortoiselength = tortoiselength + 5;
                tortoisetime = tortoisetime + 3;
                x2 += 5;
                System.out.println(name + "已经跑了" + tortoiselength + "米");
                if (tortoiselength == 300) {
                    System.out.println("乌龟到达了终点,用时为:" + tortoisetime + "秒");
                    flag = false;
                }
                repaint();

            }

        }
    }
}

public class RabbitTortoiseRace2 {

    public static void main(String args[]) {
        Animals rabbit = new Animals();
        Animals tortoise = new Animals();
        new Thread(rabbit, "兔子").start();
        new Thread(tortoise, "乌龟").start();
        JFrame f = new JFrame("龟兔赛跑游戏");
        f.add(tortoise);
        f.add(rabbit);
        f.setSize(400, 300);
        f.setVisible(true);
    }

}
把乌龟放在前面,兔子就可以跑到终点了

假如人生没有梦想,和咸鱼有什么区别!
2018-04-20 21:35
静水且流深
Rank: 5Rank: 5
等 级:贵宾
威 望:11
帖 子:60
专家分:319
注 册:2017-7-7
收藏
得分:0 
回复 4楼 林月儿
好久不见,上次书没还你,有空一起吃饭啊?
程序代码:
package com.huawei.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Animal {
    protected int x;
    protected int y;
    protected int width;
    protected int height;
    protected int length;
    protected int time;
    private String name;
    public Animal(String name) {
        height=100;
        this.name = name;
    }
    public boolean wantRest(int time) {
        if(length % 80 == 0){
            this.time += time; 
            System.out.println(name+"休息");
            return true;
        }
        return false;
    }
    public void info() {
        System.out.println(name+"已经跑了"+length+"米");
    }
    public boolean gameOver(int distance) {
        if(width+length >= distance){
            System.out.println(name+"到达了终点,用时为:"+time+"秒");
            return true;
        }
        return false;
    }
}
class Rabbit extends Animal{
    public Rabbit(String name) {
        super(name);
        width = 50;
        y = 20;
    }
    public void move(){
        x += 10;
        length += 10;
        time += 5;
    }
    
}
class Tortoise extends Animal{
    public Tortoise(String name) {
        super(name);
        width = 60;
        y = 150;
    }
    public void move(){
        x += 5;
        length += 5;
        time += 3;
    }
}

 
public class RaceTest extends JPanel{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    // 兔子,乌龟图片
    private Image rabbitImg = Toolkit.getDefaultToolkit()
            .getImage(Panel.class.getResource("/rabbit.jpg"));
    private Image tortoiseImg = Toolkit.getDefaultToolkit()
            .getImage(Panel.class.getResource("/tortoise.jpg"));
    //d
    private Rabbit rabbit = new Rabbit("兔子");
    private Tortoise tortoise = new Tortoise("乌龟");
    private JFrame jfame = new JFrame("龟兔赛跑游戏");
    // 线程名称
    private static final String STRING_TORTOISE = "TORTOISE";
    private static final String STRING_RABBIT = "RABBIT";
    // 场地宽高
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 300;
    @Override
    public void paint(Graphics g) {   
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
        g.drawImage(rabbitImg, rabbit.x, rabbit.y, rabbit.width, rabbit.height, this); 
        g.drawImage(tortoiseImg, tortoise.x, tortoise.y, tortoise.width, tortoise.height, this);
    }
    class RaceThread implements Runnable {
        @Override
        public void run() {
            while(true) {
                rest(100);
                String threadName = Thread.currentThread().getName();
                // "兔子"
                if(STRING_RABBIT.equals(threadName)){
                    rabbit.move();
                    if(rabbit.wantRest(20)){
                        rest(2000);           
                    }
                    rabbit.info();
                    if(rabbit.gameOver(DEFAULT_WIDTH)){
                        return;
                    }
                // "乌龟"
                } else {
                    tortoise.move();
                    tortoise.info();
                    if(tortoise.gameOver(DEFAULT_WIDTH)){
                        return;
                    }
                }
                repaint();
            }
        }
        private void rest(int interval){
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }    
        }
    }
    private void start(){
        jfame.add(this);
        jfame.setSize(DEFAULT_WIDTH+20, DEFAULT_HEIGHT);
        jfame.setLocation(200, 200);
        jfame.setVisible(true);
        jfame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String args[]) {
        RaceTest race = new RaceTest();
        race.start();
        Thread rabbitThread = new Thread(race.new RaceThread());
        Thread tortoiseThread = new Thread(race.new RaceThread());
        rabbitThread.setName(STRING_RABBIT);
        tortoiseThread.setName(STRING_TORTOISE);
        rabbitThread.start();
        tortoiseThread.start();
    }
}

不过是爱情,又能走多久
2018-04-20 21:59
快速回复:请教大神 如何让兔子和乌龟一起动
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043141 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved