java swing有趣编程_学习Java随记之swing编程(1)

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

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

public class MyTankGame extends JFrame {//继承JFrame

....public static voidmain(String[] args) {

MyTankGame mtg= newMyTankGame();

}publicMyTankGame() {//三板斧

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 extendsJFrame {

MyPanel mp= null;public static voidmain(String[] args) {

MyTankGame mtg= newMyTankGame();

}publicMyTankGame() {

mp= newMyPanel();

mp.setBackground(Color.black);//设置背景色this.add(mp);this.addKeyListener(mp);//注册监听

·····

}

}class MyPanel extends JPanel implementsKeyListener {

········

@Overridepublic voidkeyPressed(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(·····){

·····

}

}

}

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

效果图如下:

db229cf943cf54ff55eb19dfdfde49a9.png

完整代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

packagetankGame;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MyTankGame extendsJFrame {

MyPanel mp= null;public static voidmain(String[] args) {

MyTankGame mtg= newMyTankGame();

}publicMyTankGame() {

mp= newMyPanel();

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 implementsKeyListener {int tx=10,ty=10;

Hero h= null;publicMyPanel() {

h= newHero(tx, ty);

h.setDirect(1);

}public voidpaint(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, inttype) {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;

}

}

@Overridepublic voidkeyPressed(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();

}

@Overridepublic voidkeyReleased(KeyEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidkeyTyped(KeyEvent e) {//TODO Auto-generated method stub

}

}classTank{private intx,y,direct;public intgetDirect() {returndirect;

}public void setDirect(intdirect) {this.direct =direct;

}public intgetX() {returnx;

}public void setX(intx) {this.x =x;

}public intgetY() {returny;

}public void setY(inty) {this.y =y;

}public Tank(int x,inty){this.x=x;this.y=y;

}

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

}

}

View Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值