swing 中的事件 和移动算法 集合入门

swing的事件
对于swing来说,我们如果想操作swing上的组件,我们需要获取键盘和鼠标的操作,所以java就提供了一套叫做事件的机制,
一个叫操作事件,一个叫键盘事件。一个叫鼠标事件,键盘事件事件的原理是通过获取操作系统的按键操作。然后通过吧对事件信息组装成一个对象传递给我们的事件操作类当中的方法。我们首先需要对我们的java窗体进行事件的监听。

this.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub

            }
        });

二位平面的移动算法:
package com.xingxue.view;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Plane {
public int x;
public int y=249;
public int width = 90;
public int height = 50;
public int speed = 2;
//移动算法 趋势
public boolean top;
public boolean bottom;
public boolean left;
public boolean right;

// top == true left == true;
public void superMove() {
// if(top == true && left == false && bottom == false && right == false) {
if(top&&!left&&!right&&!bottom) {
this.y-= this.speed;
}
if(left == true) {
this.x-= this.speed;
}
if(right == true) {
this.x+= this.speed;
}
if(bottom == true) {
this.y+= this.speed;
}

    if(top == true && left== true) {
        this.x-= this.speed;
        this.y-= this.speed;
    }
    if(top == true && right== true) {
        this.x+= this.speed;
        this.y-= this.speed;
    }
    if(bottom == true && left== true) {
        this.x-= this.speed;
        this.y+= this.speed;
    }
    if(bottom == true && right== true) {
        this.x+= this.speed;
        this.y+= this.speed;
    }



}




public void move(int key) {
    switch(key) {
    case 65: //A
        this.x -= this.speed;
        break;
    case 83: // S
        this.y += this.speed;
        break;
    case 87: //W
        this.y -= this.speed;
        break;
    case 68: //D
        this.x += this.speed;
        break;
    }
}


public void drawMe(Graphics g, JPanel jp) {
    //Icon
    ImageIcon icon = new ImageIcon("img/myPlane9.gif");
    //Image
    Image img = icon.getImage();

    g.drawImage(img, x, y, jp);
}

}

加载敌机:
1、敌机的数量是N架, 我们原来的方式是创建一个对象,画出来, 如果采用这种方式,我们的代码重复量会很大,所以我们决定采用数组来装N个敌机,我们先吧敌机床架你出来存入数组, 在从数组取出来画到界面。
但是但是但是利用数组我们发现数组长度不可变,容量不可获取。所以实际开发中数据局限性太大了,使用起来太难受了。所以java设计了一个很优秀的数据结构叫做集合【高级数组】,

List Set Map
List: 是一个接口,是不能使用的,但是定义了很多规范,供子类实现,所以我们要找他的子类ArrayList。
ArrayList是一个大小可变的数组
add:添加元素
get:获取元素
size: 切记不可以叫长度,而是表示里面已经存储了多少个数据

        ArrayList goulist = new ArrayList();
        for(int i=0; i<5; i++) {
            Dog dog = new Dog( 4,"xiaohua" + i,"红色");
            goulist.add(dog);
        }

        for(int i=0; i<goulist.size(); i++) {
            Dog dog = (Dog) goulist.get(i);
            System.out.println(dog.getName());
        }

对于集合来说。我们研究3个方法, 增加add 查询get 删除remove

增加方法由两个特殊的, 一个增加元素到指定的下标 add(index, object)
把两个数组拼接起来 addAll( list );

ArrayList集合的底层就是一个Object的数组来进行实现的,通过数组的数据的拷贝和数组容量的扩容来进行数据的操作

数组集合还又一个特殊的类叫Vector == 和我们的ArrayList功能一模一样。唯一的区别,Vector一个线程安全,ArrayList一个不安全

ArrayList是一个连续的数组结构,所以查询是特别快的,删除和增加很麻烦, 所以为了提升删除、增加的效率,新开发了另外一个链表集合LinkedList

栈、数组、链表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值