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 ...
随机推荐
- Linux ${} 变量内容的提取和替换功能等
[root@localhost log]# var=/dir1/dir2/file.txt 1.对变量取值 [root@localhost log]# echo ${var} /dir1/dir2/f ...
- HTTP协议响应消息的常用状态码【转】
“100″ : Continue(继续) 初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新) “101″ : Switching Protocols(切换协议) 请求者已要求服 ...
- Spring Boot学习记录(二)–thymeleaf模板
自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好 ...
- JQuery 之CSS操作
JQuery 之CSS操作 设置 <p> 元素的颜色: 将所有段落的颜色设为红色 $(".btn1").click(function(){ $("p" ...
- taro 更新
更新 Taro 提供了更新命令来更新 CLI 工具自身和项目中 Taro 相关的依赖 更新 Taro CLI 工具 # taro $ taro update self # npm npm i -g @ ...
- Linux日志分析的实战专题
来自 日志也是用户应该注意的地方之一.不要低估日志文件对网络安全的重要作用,因为日志文件能够详细记录系统每天发生的各种各样的事件.用户可以通过日志文件 检查错误产生的原因,或者在受到攻击和黑客入侵 ...
- Spring事务属性具体解释
Spring.是一个Java开源框架,是为了解决企业应用程序开发复杂性由Rod Johnson创建的.框架的主要优势之中的一个就是其分层架构,分层架构同意使用者选择使用哪一个组件,同一时候为 J2EE ...
- Ubuntu14.04上深度学习Caffe库安装指南(CUDA7.5 + opencv3.1)
Ubuntu14.04上Caffe安装指南 安装的准备工作 首先,安装官方版Caffe时.假设要使用Cuda.须要确认自己确实有NVIDIA GPU. 安装Ubuntu时,将/boot 分区分大概20 ...
- 【BIRT】交叉报表中出现空值设置为默认值
在使用BIRT做交叉报表的时候,往往会出现有些维度下的值是空值,例如如下报表: 那么我们可以为这些空值给出默认值,例如"-" 具体操作如下: 点击上图的[Cross Tab]后选中 ...
- linux中echo的使用方法
1.echo命令我们经常使用的选项有两个,一个是-n,表示输出之后不换行.另外一个是-e,表示对于转义字符按对应的方式处理,假设不加-e那么对于转义字符会按普通字符处理. 2.echo输出时的转义字符 ...