留下两个例子作为参考,

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<String, String> map) {
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> 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);
}
}
}

最新文章

  1. 用C3中的animation和transform写的一个模仿加载的时动画效果
  2. 用html5的canvas画布绘制贝塞尔曲线
  3. tomcat 设置jvm内存
  4. javascript判断手机浏览器版本信息
  5. 1.3为WebApi创建帮助文档
  6. 身份证号码查询与生成(C#源码)
  7. 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)
  8. bit,byte,char,string区别与基本类型认识
  9. 使用iOS8 WKWebView的浏览器模块,脉冲动画层-b
  10. 数据库神器:Navicat Premium
  11. jQuery菜单示例(全选,反选,取消)
  12. 201521123009 《Java程序设计》第10周学习总结
  13. centos7使用kubeadm安装部署kubernetes 1.14
  14. 使用 CODING 进行 Hexo 项目的持续集成
  15. TypeError: sequence item 1: expected str instance, int found
  16. day14(2)---列表推导式、生成器推导式、字典推导式、三元表达式
  17. PHP为JSON数据的API返回空数组或者空对象
  18. Redis 简介(官方翻译)
  19. 【linux】查看进程
  20. Throwable、Error、Exception、RuntimeException 区别 联系

热门文章

  1. Python-urllib学习记录
  2. C#基础视频教程3.3 常见控件类型和使用方法
  3. Github.com sshkey 生成与添加
  4. Java泛型中extends和super的理解
  5. Java注解总结
  6. DropDownList 添加“请选择”
  7. 用sp_lock诊断SQL Sever的死锁问题
  8. 转 springboot 教程
  9. Mysql 的位运算符详解,mysql的优先级
  10. 邁向 RHCE 之路 (Day26) - Apache 網頁伺服器