博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 07 example
阅读量:6070 次
发布时间:2019-06-20

本文共 2637 字,大约阅读时间需要 8 分钟。

留下两个例子作为参考,

1. 追逐小方块的例子

2. HashMap 和 Iterator 的例子

 

Example one:

import acm.graphics.*;import acm.program.*;import java.awt.*;import java.awt.event.*;public class TargetSeeker extends GraphicsProgram {/* private constants */    private static final int TARGET_SIZE = 10;    private static final int SEEKER_SIZE = 20;    private static final int PAUSE_TIME = 10;        public void run() {        initTarget();        initSeeker();        addMouseListeners();                while (true) {            seek();        }    }        private void initTarget() {        targetSquare = new GRect(TARGET_SIZE, TARGET_SIZE);        targetSquare.setColor(Color.red);        targetSquare.setFilled(true);        targetX = getWidth() / 2;        targetY = getHeight() / 2;        add(targetSquare, targetX - TARGET_SIZE/2, targetY - TARGET_SIZE/2);    }        private void initSeeker() {        seeker = new GRect(SEEKER_SIZE, SEEKER_SIZE);        add(seeker, 0, 0);    }    private int moveAmount(double seekerPos, double targetPos) {        int amount = 0;        if (targetPos > seekerPos) {            amount = 1;        } else if (targetPos < seekerPos) {            amount = -1;        }        return amount;    }        private void seek() {        pause(PAUSE_TIME);        double seekerMidX = seeker.getX() + SEEKER_SIZE / 2;        int dx = moveAmount(seekerMidX, targetX);        double seekerMidY = seeker.getY() + SEEKER_SIZE / 2;        int dy = moveAmount(seekerMidY, targetY);                seeker.move(dx, dy);    }        public void mouseClicked(MouseEvent e) {        targetX = e.getX();        targetY = e.getY();        remove(targetSquare);        add(targetSquare, targetX - TARGET_SIZE/2, targetY - TARGET_SIZE/2);    }    /* private instance variable */    private int targetX;    private int targetY;    private GRect targetSquare;    private GRect seeker;}

 

Example two:

public void PrintMatchingKeys(HashMap
map) { ArrayList
keys = new ArrayList
(); Iterator
it = map.keySet().iterator(); while (it.hasNext()) { // keys is array list, it is the key (String type) of map keys.add(it.next()); } // Reset "it" iterator to allow us to iterate over keys again it = map.keySet().iterator(); while (it.hasNext()) { String key = it.next(); String value = map.get(key); // of course contain if (key.contains(value)) { println(key + ": " + value); } } }

转载于:https://www.cnblogs.com/moveofgod/p/3769928.html

你可能感兴趣的文章
NLB网路负载均衡管理器详解
查看>>
水平添加滚动条
查看>>
PHP中”单例模式“实例讲解
查看>>
VS2008查看dll导出函数
查看>>
VM EBS R12迁移,启动APTier . AutoConfig错误
查看>>
atitit.细节决定成败的适合情形与缺点
查看>>
iOS - Library 库
查看>>
MATLAB 读取DICOM格式文件
查看>>
spring事务管理(Transaction)
查看>>
django.contrib.auth登陆注销学习
查看>>
js执行本地exe文件的3种方法
查看>>
理解B树索引
查看>>
vi编辑器的命令集合
查看>>
Mysql利用binlog恢复数据
查看>>
解决 Windows启动时要求验证
查看>>
我的友情链接
查看>>
用yum安装mariadb
查看>>
一点IT"边缘化"的人的思考
查看>>
Gallery循环滑动
查看>>
Sql与C#中日期格式转换总结
查看>>