今天学习了画一个坦克,学习资料为传智博客韩顺平老师的视频
下面为坦克的源代码:
package photoes;
import java.awt.*;
import javax.swing.*;
public class Tanke extends JFrame{
Mypanel mp=null;
public static void main(String[] args)
{
Tanke tanke1=new Tanke();
}
public Tanke()
{
mp=new Mypanel();
this.add(mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//我的面板
class Mypanel extends JPanel
{
//定义一个我的坦克
Hero hero=null;
public Mypanel()
{
hero=new Hero(100,100);
}
//重写paint
public void paint(Graphics g){
super.paint(g);
//画出背景
g.fillRect(0, 0, 400, 300);
this.drawTanke(hero.getX(),hero.getY(),g,0,0);
}
//画出坦克
public void drawTanke(int x, int y,Graphics g,int direct,int type)
{
//判断是什么类型的坦克
switch(type)
{
case 0:
g.setColor(Color.green);
break;
case 1:
g.setColor(Color.yellow);
break;
}
//判断方向
switch(direct)
{
//向上
case 0 :
//画出我的坦克
//1.画出左边的矩形
g.fill3DRect(x, y, 5 , 30,false);
//2.画出右边的矩形
g.fill3DRect(x+17,y , 5, 30,false);
//3.画出中间矩形
g.fill3DRect(x+6, y+5,10 ,20,false );
//4.画出圆形
g.fillOval(x+6,y+10,10,10);
//5.画线
g.drawLine(x+11, y, x+11, y+15);
}
}
}
//坦克类
class Tank
{
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;
}
int x=0;//横坐标
int y=0;//纵坐标
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);
}
}
这是我自己画的笑脸,嘿嘿
package Image;
import java.awt.*;
import javax.swing.*;
public class Wuguui extends JFrame{
Mypanel1 mypanel1=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Wuguui wuguigui=new Wuguui();
}
public Wuguui()
{
mypanel1=new Mypanel1();
this.add(mypanel1);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Mypanel1 extends JPanel
{
Myguigui myguigui=null;
public Mypanel1()
{
myguigui=new Myguigui(100,100);
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 400);
drawwugui(myguigui.getX(),myguigui.getY(), g,1,0);
}
public void drawwugui(int x,int y,Graphics g,int type,int direct)
{
switch(type)
{
case 0:g.setColor(Color.white);
break;
case 1:g.setColor(Color.green);
break;
}
switch(direct)
{
case 0:
System.out.print("1");
g.drawOval(x,y,30,30);
g.drawLine(x+5, y+10, x+7, y+8);
g.drawLine(x+7, y+8, x+9, y+8);
g.drawLine(x+9, y+8, x+11, y+10);
g.drawLine(x+19, y+10, x+21, y+8);
g.drawLine(x+21, y+8, x+23, y+8);
g.drawLine(x+23, y+8, x+25, y+10);
g.drawLine(x+8, y+20, x+13, y+25);
g.drawLine(x+13, y+25, x+18, y+25);
g.drawLine(x+18, y+25, x+23, y+20);
}
}
}
class Wugui
{
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;
}
int x=0,y=0;
public Wugui(int x,int y){
this.x=x;
this.y=y;
};
}
class Myguigui extends Wugui
{
public Myguigui(int x,int y){
super(x,y);
}