代码如下:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author BruceLong
* 绘制王八,并设置键盘监听事件
* */
public class TurtleMove {
public static void main(String[] args) {
//1. 创建一个窗体,大小1366*768
JFrame frame = new JFrame();
frame.setSize(1366, 768);
/*//画一个板子
JPanel panel = new JPanel();
//设置面板的背景颜色
panel.setBackground(Color.BLACK);
//设置窗体添加面板
frame.add(panel);*/
/**创建一个面板,并设置面板的背景颜色
* 并添加到窗体上去*/
XiaoWangBa xwb = new XiaoWangBa();
xwb.setBackground(Color.LIGHT_GRAY);
//窗体添加键盘监听事件,监听对象是面板
frame.addKeyListener(xwb);
frame.add(xwb);
//3.设置窗体标题为:“画王八-个人签名”
frame.setTitle("画王八-WangYunlong");
//4.设置窗体关闭按钮功能
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//5.设置窗体大小不可更改
frame.setResizable(false);
//2.设置窗体可见
frame.setVisible(true);
}
}
//面板类
class XiaoWangBa extends JPanel implements KeyListener{
//王八的坐标位置
int x = 400,y = 200;
//王八四条腿(foot)的坐标
int fx1 = 400,fy1 = 200;
int fx2 = 400,fy2 = 200;
int fx3 = 400,fy3 = 200;
int fx4 = 400,fy4 = 200;
//移动变量
int f = 0;
//绘制方法,先输入paint再alt+/
@Override //JFrame frame
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
//1.第一步先设置画笔的颜色
//2.第二步再对画布进行画图设置坐标
/**画脑袋*/
//设置画笔颜色
//g.setColor(Color.yellow);
// Red Green Blue
g.setColor(new Color(0, 100, 0));
//绘制实心椭圆
g.fillOval(x, y, 70, 100);
/**画眼睛*/
//设置画笔颜色
g.setColor(Color.black);
g.fillOval(x+10, y+30, 10, 10);
g.fillOval(x+50, y+30, 10, 10);
/* //眼睛发亮
g.setColor(Color.white);
g.fillOval(x+15, y+30, 4, 4);
g.fillOval(x+55, y+30, 4, 4);*/
/**画尾巴*/
g.setColor(new Color(0, 100, 0));
g.fillOval(x, y+290, 60, 80);
//设置画笔颜色和背景颜色相同
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x+20, y+300, 60, 80);
/**画王八四条腿*/
//左上
g.setColor(new Color(0, 100, 0));
g.fillOval(fx1-85, fy1+80, 70, 70);
//右上
g.setColor(new Color(0, 100, 0));
g.fillOval(fx2+85, fy2+80, 70, 70);
//左下
g.setColor(new Color(0, 100, 0));
g.fillOval(fx3-85, fy3+235, 70, 70);
//右下
g.setColor(new Color(0, 100, 0));
g.fillOval(fx4+85, fy4+235, 70, 70);
/**绘制王八壳*/
g.setColor(new Color(0, 50, 0));
g.fillOval(x-66, y+70, 200, 240);
/**绘制龟纹*/
g.setColor(Color.black);
g.drawOval(x-66, y+70, 200, 240);
//(上)左中右
g.setColor(Color.yellow);
g.fillOval(x-40, y+130, 50, 30);
g.setColor(Color.BLUE);
g.fillOval(x+15, y+80, 30, 50);
g.setColor(Color.yellow);
g.fillOval(x+60, y+130, 50, 30);
//中间
g.setColor(new Color(255, 0, 0));
g.fillOval(x+10, y+155, 45, 45);
//(下)左中右
g.setColor(Color.BLUE);
g.fillOval(x-45, y+195, 50, 30);
g.setColor(Color.yellow);
g.fillOval(x+20, y+220, 30, 50);
g.setColor(Color.BLUE);
g.fillOval(x+70, y+195, 50, 30);
//画直线(上)
g.setColor(Color.BLACK);
g.drawLine(x-10, y+82, x+20, y+158);
g.setColor(Color.BLACK);
g.drawLine(x+80, y+82, x+43, y+158);
//画直线(中间)
g.setColor(Color.BLACK);
g.drawLine(x-65, y+180, x+10, y+180);
g.setColor(Color.BLACK);
g.drawLine(x+133, y+180, x+55, y+180);
//画直线(下)
g.setColor(Color.BLACK);
g.drawLine(x-10, y+298, x+20, y+198);
g.setColor(Color.BLACK);
g.drawLine(x+80, y+298, x+43, y+198);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// 按键盘事件
//测试上键
if(e.getKeyCode()==KeyEvent.VK_UP && fy1>0) {
if(fy1<fy2) {
f = 1;
}else {
f = 0;
}
y=y-10;
if(f==0) {
fy1-=20;
fy4-=20;
}if(f==1) {
fy2-=20;
fy3-=20;
}
System.out.println("测试上键");
}
//如果按的是下键
if(e.getKeyCode()==KeyEvent.VK_DOWN) {
System.out.println("123");
if(fy1<fy2) {
f = 1;
}else {
f = 0;
}
y+=10;
if(f == 1) {
fy1+=20;
fy4+=20;
}
if(f == 0) {
fy2+=20;
fy3+=20;
}
}
//如果按的是左键
if(e.getKeyCode()==KeyEvent.VK_LEFT &&fx1>99) {
if(fx1>fx2) {
f = 1;
}else {
f = 0;
}
x-=10;
if(f == 1) {
fx1-=20;
fx4-=20;
}
if(f == 0) {
fx2-=20;
fx3-=20;
}
}
//如果按的是右键
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
if(fx1<fx2) {
f = 1;
}else {
f = 0;
}
x+=10;
if(f == 1) {
fx1+=20;
fx4+=20;
}
if(f == 0) {
fx2+=20;
fx3+=20;
}
}
/*if(e.getKeyCode()==KeyEvent.VK_DOWN&& fy1>0) {
y=y+10;
if(fy1<=fy2) {
fy1+=20;
fy4+=20;
}else {
fy2+=20;
fy3+=20;
}
System.out.println("测试下键");
}
if(e.getKeyCode()==KeyEvent.VK_LEFT && fx1>0 ) {
x-=10;
if(fx1>=fx2) {
fx1-=20;
fx4-=20;
}else {
fx2-=20;
fx3-=20;
}
System.out.println("测试左键");
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT && fx1>0) {
x+=10;
if(fx1<=fx2) {
fx1+=20;
fx4+=20;
}else {
fx2-=20;
fx3-=20;
}
System.out.println("测试右键");
}*/
//重新绘制paint方法(重新调用paint方法)
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
运行结果为: