唉,本人學習進度緩慢,但依然會堅持不懈!有感興趣的朋友可以在下麵留言。 源代碼獻上: /* * 畫出我的坦克,使他可以上下左右移動 */package com.test4; import javax.swing.*;import java.awt.*;import java.awt.event.*; ...
唉,本人學習進度緩慢,但依然會堅持不懈!有感興趣的朋友可以在下麵留言。
源代碼獻上:
/*
* 畫出我的坦克,使他可以上下左右移動
*/
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) {
}
}