学习Java随记之swing编程(1)

    最近在学习swing编程,依据老师的教学视频学习使用java.awt.*、javax.swing.*、java.awt.event.*包进行简单的tank大战游戏编程,学习了JFrame、JPanel等容器和组件的使用,学习使用Graphics的各种方法绘制图形,以及一些事件监听和处理方法。

    JFrame这个类在目前的学习中都是继承使用,然后调用三板斧把他显示出来,例如:

public class MyTankGame extends JFrame {//继承JFrame
        ....
    public static void main(String[] args) {
        MyTankGame mtg = new MyTankGame();

    }
        public MyTankGame() {
        //三板斧
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭即退出,防内存泄漏
        this.setVisible(true);
    }
}

    坦克是使用Graphics作为画笔画2个长矩形(作为坦克的履带。也可用6个小矩形连接组成的大矩形代替更好看),1个短矩形,1个圆,1条线组合起来,其中矩形使用fill3DRect方法以显示边框,

    for (int i = 0; i < 5; i++) {
    g.fill3DRect(x, y + 6 * i, 5, 6, false);
    g.fill3DRect(x + 15, y + 6 * i, 5, 6, false);
    }
    g.fill3DRect(x + 5, y + 5, 10, 20, false);
    g.fillOval(x + 4, y + 10, 10, 10);
    g.drawLine(x+10, y+1, x+10, y+10);    

然后通过repaint方法刷新坦克实现坦克的移动,其中涉及到事件的监听和事件处理方法,Java的事件处理机制为委派处理模型,有事件源、事件、事件监听接口完成,即可在画坦克的类JPanel调用KeyListener接口监听JFrame,在JFrame中注册JPanel的监听,这样JPanel就能监听键盘的输入,然后根据需要重写KeyListener的keyPressed、keyReleased等方法,使用(e的类型也是KeyEvent)e.getKeyCode()==KeyEvent.VK_····进行判断进行进一步的处理。

public class MyTankGame extends JFrame {

    MyPanel mp = null;

    public static void main(String[] args) {
        MyTankGame mtg = new MyTankGame();

    }

    public MyTankGame() {
        mp = new MyPanel();
        mp.setBackground(Color.black);//设置背景色
        this.add(mp);
        
        this.addKeyListener(mp);//注册监听
        ·····
    }

}
class MyPanel extends JPanel implements KeyListener {

        ········
        @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int speed=2;
        //移动坦克并改变方向
        if((e.getKeyCode()==KeyEvent.VK_S)){
            ty+=speed;
            h.setY(ty);
            h.setDirect(-1);
        }else if(·····){
                    ·····
                }
        }
}

至此一个可以自由移动的坦克就出来了。

效果图如下:

完整代码如下:

/**
 * 自由移动的小坦克
 */

package tankGame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyTankGame extends JFrame {

    MyPanel mp = null;

    public static void main(String[] args) {
        MyTankGame mtg = new MyTankGame();

    }

    public MyTankGame() {
        mp = new MyPanel();
        mp.setBackground(Color.black);
        this.add(mp);
        
        this.addKeyListener(mp);//注册监听
        
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

}

class MyPanel extends JPanel implements KeyListener {
    int tx=10,ty=10;
    Hero h = null;

    public MyPanel() {
        h = new Hero(tx, ty);
        h.setDirect(1);
    }

    public void paint(Graphics g) {
        super.paint(g);
        this.drawTank(g, h.getX(), h.getY(), h.getDirect(), 1);

    }

    public void drawTank(Graphics g, int x, int y, int direct, int type) {
        switch (type) {
        case 0:
            g.setColor(Color.cyan);
            break;
        case 1:
            g.setColor(Color.yellow);
            break;
        default:
            break;
        }
        switch (direct) {//判断坦克的方向
        case 1://向上
            for (int i = 0; i < 5; i++) {
                g.fill3DRect(x, y + 6 * i, 5, 6, false);
                g.fill3DRect(x + 15, y + 6 * i, 5, 6, false);
            }
            g.fill3DRect(x + 5, y + 5, 10, 20, false);
            g.fillOval(x + 4, y + 10, 10, 10);
            g.drawLine(x+10, y+1, x+10, y+10);
            break;
        case -1://向下
            for (int i = 0; i < 5; i++) {
                g.fill3DRect(x, y + 6 * i, 5, 6, false);
                g.fill3DRect(x + 15, y + 6 * i, 5, 6, false);
            }
            g.fill3DRect(x + 5, y + 5, 10, 20, false);
            g.fillOval(x + 4, y + 10, 10, 10);
            g.drawLine(x+10, y+20, x+10, y+29);
            break;
        case 2://向右
            for (int i = 0; i < 5; i++) {
                g.fill3DRect(x+6*i, y , 5, 6, false);
                g.fill3DRect(x+6*i, y +15, 5, 6, false);
            }
            g.fill3DRect(x + 5, y + 5, 20, 10, false);
            g.fillOval(x + 10, y + 4, 10, 10);
            g.drawLine(x+20, y+9, x+29, y+9);
            break;
        case -2://向左
            for (int i = 0; i < 5; i++) {
                g.fill3DRect(x+6*i, y , 5, 6, false);
                g.fill3DRect(x+6*i, y +15, 5, 6, false);
            }
            g.fill3DRect(x + 5, y + 5, 20, 10, false);
            g.fillOval(x + 10, y + 4, 10, 10);
            g.drawLine(x+1, y+9, x+10, y+9);
            break;
        default:
            break;
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int speed=2;
        //移动坦克并改变方向
        if((e.getKeyCode()==KeyEvent.VK_S)){
            ty+=speed;
            h.setY(ty);
            h.setDirect(-1);
        }else if((e.getKeyCode()==KeyEvent.VK_W)){
            ty-=speed;
            h.setY(ty);
            h.setDirect(1);
        }else if((e.getKeyCode()==KeyEvent.VK_A)){
            tx-=speed;
            h.setX(tx);
            h.setDirect(-2);
        }else if((e.getKeyCode()==KeyEvent.VK_D)){
            tx+=speed;
            h.setX(tx);
            h.setDirect(2);
        }             
        this.repaint();
    }

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

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }
}
class Tank{
    private int x,y,direct;
    public int getDirect() {
        return direct;
    }
    public void setDirect(int direct) {
        this.direct = direct;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Tank(int x,int y){
        this.x=x;
        this.y=y;
    }
}
class Hero extends Tank{

    public Hero(int x, int y) {
        super(x, y);        
    }

}
View Code
原文地址:https://www.cnblogs.com/hijackhou/p/8179849.html