zoukankan      html  css  js  c++  java
  • Java 07 example

    留下两个例子作为参考,

    1. 追逐小方块的例子

    2. HashMap 和 Iterator 的例子

    Example one:

    aimage

    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);
                }
            }
        }
  • 相关阅读:
    可以使用多少列创建索引?
    如何显示前 50 行?
    简单描述 MySQL 中,索引,主键,唯一索引,联合索引的区别,对数据库的性能有什么影响-从读写两方面?
    实践中如何优化 MySQL ?
    列的字符串类型可以是什么?
    MySQL 里记录货币用什么字段类型好 ?
    什么是通用 SQL 函数?
    对于关系型数据库而言,索引是相当重要的概念?
    为表中得字段选择合适得数据类型?
    SQL 注入漏洞产生的原因?如何防止?
  • 原文地址:https://www.cnblogs.com/moveofgod/p/3769928.html
Copyright © 2011-2022 走看看