首页 技术 正文
技术 2022年11月15日
0 收藏 951 点赞 3,816 浏览 4069 个字

唉,本人学习进度缓慢,但依然会坚持不懈!有感兴趣的朋友可以在下面留言。

源代码献上:

/*
* 画出我的坦克,使他可以上下左右移动
*/
package com.test4;

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

public class MytankGame2 extends JFrame{
MyPanel mp = null;
public static void main(String[] args){
MytankGame2 mtg = new MytankGame2();
}
//构造函数
public MytankGame2(){
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(270, 500);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

class MyPanel extends JPanel implements KeyListener{
Mytank mt = null;
public MyPanel(){
mt = new Mytank(10,10);
}
//重新paint函数
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 300, 400);

this.drawTank(mt.getX(), mt.getY(), 0, this.mt.direct, g);
}
//画出tank函数
public void drawTank(int x,int y,int type,int direct,Graphics g){
switch(type){
case 0:
g.setColor(Color.orange);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch(direct){
case 0://上
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y-5, x+15, y+5);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 1://下
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+15, y+35);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 3://左
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+35, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 2://右
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x-5, y+15, x+15, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
}
}

public void keyTyped(KeyEvent e) {

}
//对键按下做处理
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
//设置我的坦克
this.mt.setDirect(0);
this.mt.moveup();
}else if(e.getKeyCode() == KeyEvent.VK_A){
this.mt.setDirect(2);
this.mt.moveleft();
}else if(e.getKeyCode() == KeyEvent.VK_D){
this.mt.setDirect(3);
this.mt.moveright();
}else if(e.getKeyCode() == KeyEvent.VK_S){
this.mt.setDirect(1);
this.mt.movedown();
}
this.repaint();
}

public void keyReleased(KeyEvent e) {

}
}

/*
* 画出我的坦克,使他可以上下左右移动
*/
package com.test4;

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

public class MytankGame2 extends JFrame{
MyPanel mp = null;
public static void main(String[] args){
MytankGame2 mtg = new MytankGame2();
}
//构造函数
public MytankGame2(){
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(270, 500);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

class MyPanel extends JPanel implements KeyListener{
Mytank mt = null;
public MyPanel(){
mt = new Mytank(10,10);
}
//重新paint函数
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 300, 400);

this.drawTank(mt.getX(), mt.getY(), 0, this.mt.direct, g);
}
//画出tank函数
public void drawTank(int x,int y,int type,int direct,Graphics g){
switch(type){
case 0:
g.setColor(Color.orange);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch(direct){
case 0://上
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y-5, x+15, y+5);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 1://下
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+15, y+35);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 3://左
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+35, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 2://右
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x-5, y+15, x+15, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
}
}

public void keyTyped(KeyEvent e) {

}
//对键按下做处理
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
//设置我的坦克
this.mt.setDirect(0);
this.mt.moveup();
}else if(e.getKeyCode() == KeyEvent.VK_A){
this.mt.setDirect(2);
this.mt.moveleft();
}else if(e.getKeyCode() == KeyEvent.VK_D){
this.mt.setDirect(3);
this.mt.moveright();
}else if(e.getKeyCode() == KeyEvent.VK_S){
this.mt.setDirect(1);
this.mt.movedown();
}
this.repaint();
}

public void keyReleased(KeyEvent e) {

}
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860