新普京网站-澳门新普京 > 计算机 > 右移动的坦克澳门新普京:

右移动的坦克澳门新普京:

2020/01/05 13:36

//SignalCanvas
import java.awt.*;

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

public class SignalCanvas extends Canvas{
    int red,green,yellow,x,y,r;
    SignalCanvas(){
        setBackground(Color.BLUE);
    }
    public void setRed(int red){
        this.red=red;
    }
    public void setGreen(int green){
        this.green=green;
    }
    public void setYellow(int yellow){
        this.yellow=yellow;
    }
    public void setPosition(int x,int y){
        this.x=x;
        this.y=y;
    }
    
    public void setRadius(int r){
        this.r=r;
    }
    public void paint(Graphics g){
        if(red==1){
            g.setColor(Color.red);
        }else if(green==1){
            g.setColor(Color.green);
        }else if(yellow==1){
            g.setColor(Color.yellow);
        }
        g.fillOval(x,y,2*r,2*r);
    }
}

源代码献上:

 

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

//SignalFrame
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class SignalFrame extends Frame implements ItemListener{
    Choice choice;
    SignalCanvas signal=null;
    String itemRed="红灯",itemYellow="黄灯",itemGreen="绿灯";
    public SignalFrame(){
        choice = new Choice();
        choice.add(itemRed);
        choice.add(itemYellow);
        choice.add(itemGreen);
        choice.addItemListener(this);
        
        add(choice,BorderLayout.NORTH);
        try {
            Class cs =Class.forName("SignalCanvas");
            signal=(SignalCanvas)cs.newInstance();
            add(signal,BorderLayout.CENTER);
        }
        catch (Exception e) {
            add(new Label("您还没有编写SignalCanvas类"),BorderLayout.CENTER);
        
        }
        setBounds(100,100,360,300);
        setVisible(true);
        validate();
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

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

    }
    
    public void itemStateChanged(ItemEvent e){
        String item =choice.getSelectedItem(); //choice返回被选中的条目
        int w=signal.getBounds().width;
        int h=signal.getBounds().height;
        
        int m=Math.min(w,h);
        signal.setRadius(m/6);
        if(item.equals(itemRed)){
            if(signal!=null){
                signal.setRed(1);
                signal.setYellow(0);
                signal.setGreen(0);
                signal.setPosition(w/3,0);
                signal.repaint();
            }
        }
        else if(item.equals(itemYellow)){
            if(signal!=null){
                signal.setRed(0);
                signal.setYellow(1);
                signal.setGreen(0);
                signal.setPosition(w/3,h/3);
                signal.repaint();
            }
        }else if(item.equals(itemGreen)){
            if(signal!=null){
                signal.setRed(0);
                signal.setYellow(0);
                signal.setGreen(1);
                signal.setPosition(w/3,2*h/3);
                signal.repaint();
            }
        }
    }
}

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);

//主程序
public class SignalMainClass{
    
    public static void main(String[] agrs){
        SignalFrame frame;
        frame=new SignalFrame();
        frame.setTitle("信号灯");
    }
}

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

上一篇:稳定的排序,一段c语言代码 下一篇:【澳门新普京】客户管理,MySQL分界面工具Sqlyog用法