⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 robot.java

📁 Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套 代码
💻 JAVA
字号:
/*	Robot objects display an image like a robot and move about a test range.    Author:  David Riley -- Jan, 2005 */    /** class invariant: (direction == down  or  direction == up *                      or direction == left  or  direction == right) *                  and  0 <= column <= 11 *                  and  0 <= row <= 8   */    import java.awt.Toolkit.*;import java.awt.*;import javax.imageio.ImageIO;import java.io.*;import javax.swing.*;import java.awt.event.*;public class Robot extends JComponent implements java.awt.image.ImageObserver, ActionListener {	private java.awt.Image content;    private RoadCourse theCourse;    private final int up = 0;    private final int right = 1;    private final int down = 2;    private final int left = 3;    private int direction;    private int column, row;    private Timer clock;    private int xDelta, yDelta, tickCount;	/**	pre:    a proper JPEG image file must be in file /graphics/RoboDown.jpg in the     *          same folder as the current .class file     *  post:   an pylon image is instantiated     *          and  getX() == 0   and  getY() == 0        *          and  getWidth() == 72  AND  getHeight() == 72     *          and  direction == right     *          and  column == 0 (leftmost column on test range)     *          and  row == 0 (top rown on test range)      *          and  theCourse == rc      */	public Robot(RoadCourse rc)  {		super();		setBounds(0, 0, 144, 144);        setImage("graphics/RoboRight.jpg");        direction = right;        column = 0;        row = 0;        theCourse = rc;        clock = new Timer(40, this);	} 	/**	post:   Upon repaint the image displayed will be given by the      *          file with complete pathname specified as s      *          (Note that the file should be a gif or jpeg encoding.)       */	private void setImage(String s)  {        java.net.URL url = getClass().getResource(s);  // for applets        if (url == null)   {            url = getClass().getResource("/"+s);            if (url == null)                try {  // for applications                    content = ImageIO.read(new File(s));                    System.out.println("hereh");                } catch(IOException ioe) {                    ioe.printStackTrace();                }            else                content = getToolkit().getImage(url);        } else            content = getToolkit().getImage(url);	}        /** post:   direction is changed to represent a 90 degree clockwise turn */    public void rotateClockwise() {        direction = (direction + 1) % 4;        if (direction == up)            setImage("graphics/RoboUp.jpg");        else if (direction == right)            setImage("graphics/RoboRight.jpg");        else if (direction == down)            setImage("graphics/RoboDown.jpg");        else if (direction == left)            setImage("graphics/RoboLeft.jpg");        repaint();    }        /** post:   canStep()@pre implies the robot steps forward one unit */    public void moveByOneStep() {        if (canStep()) {            tickCount = 0;            if (direction == up) {                theCourse.moveRobotFromTo(row, column, row-1, column);                row--;                xDelta = 0;                yDelta = -1;                clock.start();            } else if (direction == right) {                theCourse.moveRobotFromTo(row, column, row, column+1);                column++;                xDelta = 1;                yDelta = 0;                clock.start();            } else if (direction == down) {                theCourse.moveRobotFromTo(row, column, row+1, column);                row++;                xDelta = 0;                yDelta = 1;                clock.start();            } else if (direction == left) {                theCourse.moveRobotFromTo(row, column, row, column-1);                column--;                xDelta = -1;                yDelta = 0;                clock.start();            }        }    }    /** post:   result == the robot can step forward in the current direction */    private boolean canStep() {        return (direction == up  &&  theCourse.canMoveUp(row,column))                || (direction == right  &&  theCourse.canMoveRight(row,column))                || (direction == down  &&  theCourse.canMoveDown(row,column))                 || (direction == left  &&  theCourse.canMoveLeft(row,column));    }    	public void paint(Graphics g)  {		g.drawImage(content, 0, 0, getWidth(), getHeight(), this);	}         public void actionPerformed(ActionEvent e) {        setLocation(getX()+xDelta, getY()+yDelta);        repaint();        tickCount++;        if (tickCount == 72)            clock.stop();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -