笔记04

会动的乌龟:(java代码)

package New03;

import javax.swing.JFrame;
/**

  • JFrame----窗体—相框

  • JPanel----画布—相片

  • 导包:

  • 1.手动导包 import……

  • 2.自动导包 ctrl+shift+o

  • 3.点击红色的叉号

  • */
    public class TurtlePro {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    //1.创建一个窗体
    JFrame frame = new JFrame();

     //2.给窗体添加标题
     frame.setTitle("杰尼龟");
     
     //3.给窗体添加大小
     frame.setSize(1400,800);
     
     //4.窗体剧中
     frame.setLocationRelativeTo(null);
     //类名.静态常量 减少内存的占用
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     TJpanel tj = new TJpanel();
     frame.addMouseListener(tj);
     tj.addMouseListener(tj);
     
     frame.addKeyListener(tj);
     tj.addKeyListener(tj);
     
     
     //添加画布到画板上
     frame.add(tj);
     //5.显示窗体
     frame.setVisible(true);	
    

    }

}

package New03;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class TJpanel extends JPanel implements KeyListener,MouseListener,Runnable{

//Grapgics g
int x = 300;
int y = 200;
//脚的位置
int k1 = 300,t1 = 200;
int k2 = 300,t2 = 200;
int k3 = 300,t3 = 200;
int k4 = 300,t4 = 200;
//加水的波纹
int[] a = new int[300];//300个x坐标
int[] b = new int[300];//300个y坐标
//定义一个方向position,当position==0的时候乌龟的左上脚和右下脚同时向前移动
int position = 0;
//构造方法:方法名和类同名;无任何返回值类型;无参构造
public TJpanel(){
	//随机产生位置
	for(int i = 0;i < a.length; i++){
		a[i] = (int)(Math.random() * 1400);
		b[i] = (int)(Math.random() * 800);
	}
	Thread t = new Thread(this);
	t.start();
}
public void paint(Graphics g) {
	// TODO Auto-generated method stub
	super.paint(g);
	this.setBackground(Color.GRAY);
	//给画笔设置颜色
	/*g.setColor(Color.cyan);
	//画矩形
	g.drawRect(30, 30, 160, 180);
	//实心
	g.setColor(Color.pink);
	g.fillRect(100, 200, 50, 100);

	g.setColor(Color.yellow);
	//画圆
	g.fillOval(300, 300, 100, 150);
	g.setColor(Color.pink);
	g.drawOval(500, 500, 100, 100);
	g.drawOval(570, 500, 100, 100);
	g.drawOval(640, 500, 100, 100);
	g.drawOval(710, 500, 100, 100);

	//直线
	g.drawLine(132, 456, 45, 45);*/

	//绿龟

	//画波纹
	g.setColor(Color.blue);
	for(int i = 0;i < a.length;i++){
		g.drawString("~~~~~~~ ", a[i], b[i]);
	}
	//加头
	g.setColor(Color.cyan);
	g.fillOval(x, y, 70, 100);
	//加眼睛
	g.setColor(Color.black); 
	g.fillOval(x+30, y+30,10, 15);
	g.fillOval(x+50, y+30,10, 15);
	//加舌头
	g.setColor(Color.red);
	g.fillOval(x+30, y, 8, 6);
	//加龟壳
	g.setColor(Color.green);
	g.fillOval(x-66, y+66, 200 ,230 );

	//画脚
	g.setColor(Color.cyan);
	g.fillOval(k1-85, t1+75, 60, 70);

	g.setColor(Color.cyan);
	g.fillOval(k2+85, t2+75, 60, 70);

	g.setColor(Color.cyan);
	g.fillOval(k3-85, t3+235, 60, 70);

	g.setColor(Color.cyan);
	g.fillOval(k4+85, t4+235, 60, 70);

	//加文字
	g.setColor(Color.black);
	//利用font创建字体
	Font font =new Font(Font.DIALOG,Font.BOLD,30);
	g.setFont(font);
	g.drawString("韩敏", x-20, y+180);



}
@Override
public void run() {
	// TODO Auto-generated method stub
	//System.out.println("线程启动");
	while(true){
		for (int i = 0; i < a.length; i++) {
			a[i]--;
			if (a[i]<0) {
				a[i] = 1400;
			}
		}
		try {
			Thread.sleep(10);
		} catch (Exception e) {
			// TODO: handle exception
		}
		repaint();//加速的
	}
}
@Override
public void keyTyped(KeyEvent e) {
	// TODO Auto-generated method stub

}
@Override
public void keyPressed(KeyEvent e) {
	// TODO Auto-generated method stub
	//System.out.println("点击了一下");
	if(e.getKeyCode() == KeyEvent.VK_UP){
		System.out.println("点击了上方键");
		y = y-10;
		if(position == 0){
			t1 = t1 - 20;
			t4 = t4 - 20;
		}
		if(position == 1){
			t2 = t2 - 20;
			t3 = t3 - 20;
		}
		if(t1=t2){
			if(x !=k1){
				x = k1 =k2 =k3 =k4;
			}position =0;
		}
	}
	if(e.getKeyCode() == KeyEvent.VK_DOWN){
		System.out.println("点击了下方键");
		y = y+10;
		if(position == 0){
			t1 = t1 - 20;
			t4 = t4 - 20;
		}
		if(position == 1){
			t2 = t2 - 20;
			t3 = t3 - 20;
		}
		if(t1>t2){
			if(x !=k1){
				x = k1 =k2 =k3 =k4;
			}position =1;
		}
		if(t1<=t2){
			if(x !=k1){
				x = k1 =k2 =k3 =k4;
			}position =0;
		}
	}
	if(e.getKeyCode() == KeyEvent.VK_LEFT){
		System.out.println("点击了左方键");
		x = x-10;
	}
	if(e.getKeyCode() == KeyEvent.VK_RIGHT){
		System.out.println("点击了右方键");
		x = x+10;
	}
	repaint();
}
@Override
public void keyReleased(KeyEvent e) {
	// TODO Auto-generated method stub

}
@Override
public void mouseClicked(MouseEvent e) {
	// TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent e) {
	// TODO Auto-generated method stub

	x = e.getX();
	y = e.getY();
	k1 =e.getX();
	t1 =e.getY();
	k2 =e.getX();
	t2 =e.getY();
	k3 =e.getX();
	t3 =e.getY();
	k   4 =e.getX();
	t4 =e.getY();


	repaint();

}
@Override
public void mouseReleased(MouseEvent e) {
	// TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent e) {
	// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
	// TODO Auto-generated method stub

}

//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//
//	}

}

你可能感兴趣的:(笔记04)