Java 07 example
留下两个例子作为参考,
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);
}
}
}
Java 07 example的更多相关文章
- Effective Java 07 Avoid finallizers
NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical ...
- .Net转Java.07.IDEA和VS常用操作、快捷键对照表
功能 IDEA 2017.1 快捷键 Visual Studio 2015 快捷键 文档 格式化整个文档 Ctrl+Alt+L Ctrl+E,D 或者 Ctrl+K,D 文件 显示最近的 ...
- 从零开始学JAVA(07)-使用SpringMVC4写helloworld
一.关于开发环境 Eclipse IDE for Java EE Developers Jdk 1.7 (32位版本) SpringMVC 4.1.5.RELEASE apache-tomcat-7. ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- Java性能优化权威指南-读书笔记(一)-操作系统性能监控工具
一:CPU 1. 用户态CPU是指执行应用程序代码的时间占总CPU时间的百分比. 系统态CPU是指应用执行操作系统调用的时间占总CPU时间的百分比.系统态CPU高意味着共享资源有竞争或者I/O设备之间 ...
- 检查Linux服务器性能
如果你的Linux服务器突然负载暴增,告警短信快发爆你的手机,如何在最短时间内找出Linux性能问题所在? 概述通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. • uptime• ...
- 60,000毫秒内对Linux的性能诊断效的方法
转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0225/168.html?1456484140 60,000 毫秒内对 Linux 的性能诊断 当你 ...
- 数据库ORM框架GreenDao
常用的数据库: 1). Sql Server2). Access3). Oracle4). Sysbase5). MySql6). Informix7). FoxPro8). PostgreSQL9) ...
- Linux系统性能10条命令监控
Linux系统性能10条命令监控 概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 p ...
随机推荐
- UNIX网络编程读书笔记:recv和send函数
这两个函数类似于标准的read和write函数,不过需要一个额外的参数. #include <sys/socket.h> ssize_t recv(int sockfd, void *bu ...
- Android获取手机位置代码实现
1.项目Src下创建...service包,然后新建GPSService类 package com.zebra.mobilesafe.service; import java.io.IOExcepti ...
- JVM是怎么工作的?
了解JVM的工作机制能够更好的帮助我们理解java语言本身,规避各种可能的错误.所以,今天趁此机会好好复习一下.来看看JVM是怎么工作的. 一.啥是JVM JVM可以理解为用来运行java程序的一种运 ...
- adb详解
adb详解 分类: android开发工具相关 2012-10-24 18:27 2822人阅读 评论(0) 收藏 举报 ADB全称Android Debug Bridge,是android sdk里 ...
- 【剑指offer】面试题30:最小的K个数
import random def partition(data, start, end): if end <= start: return start index = random.randi ...
- CDH quick start VM 中运行wordcount例子
需要注意的事情: 1. 对于wordcount1.0 ,按照http://www.cloudera.com/content/cloudera/en/documentation/HadoopTutori ...
- jquery导航,按钮等特效 - apycom
http://apycom.com/
- C#:将空间数据加载到树视图控件
自己 整理了 下 代码 测试了下 还行... #region 操作树视图控件 /// <summary> /// 自定义需要的类型 /// </summary> enum Da ...
- Mysql 数据库数值类型详解
MySQL 支持所有标准SQL 中的数值类型,其中包括严格数值类型(INTEGER.SMALLINT.DECIMAL 和NUMERIC),以及近似数值数据类型(FLOAT.REAL 和DOUBLE P ...
- Ubuntu安装deb软件包错误(依赖关系问题)解决
执行命令 sudo dpkg -i XXX.deb 返回依赖关系错误提示 执行 sudo apt-get -f install 这条命令将自动安装需要的依赖包. 再次执行命令 sudo dpkg -i ...