神龟优化
package cn.edtu.day;
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 java.security.Key;
import javax.swing.JPanel;
public class TJPanel extends JPanel implements KeyListener,MouseListener,Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
//头部原始位置
int x = 300;
int y = 200;
//脚的初始位置
int e1 = 300, f1 = 200;//左上
int e2 = 300, f2 = 200;//右上
int e3= 300, f3 = 200;//左下
int e4 = 300, f4 = 200;//右下
//加水的波纹
int[] a = new int[300];//300个x坐标
int[] b = new int[300];//300个y坐标
//构造方法:方法名和类同名:无任何返回值类型;无参构造
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();//启动线程
}
//Graphics g:画笔
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
//修改底部背景色
this.setBackground(Color.cyan);
//画波纹
g.setColor(Color.BLUE);
for (int i = 0; i < a.length; i++) {
g.drawString("~~~~~~~", a[i], b[i]);
}
//画头部
g.setColor(Color.GREEN);
g.fillOval(x, y, 70, 100);
//加眼睛
g.setColor(Color.BLACK);
g.fillOval(x+10, y+30, 12, 15);
g.fillOval(x+50, y+30, 12, 15);
//加舌头
g.setColor(Color.RED);
g.fillOval(x+30, y, 8, 6);
//画脚 左上
g.setColor(Color.GRAY);
g.fillOval(e1-90, f1+75, 60,70);
//右上
g.fillOval(e2+90, f2+75, 60, 70);
//左下
g.fillOval(e3-90, f3+235, 60, 70);
//右下
g.fillOval(e4+90, f4+235, 60, 70);
//加乌龟壳
g.setColor(Color.BLACK);
g.fillOval(x-66, y+66, 200,270);
//壳加文字
g.setColor(Color.BLUE);
Font font = new Font(Font.DIALOG,Font.BOLD, 30);
g.setFont(font);
g.drawString("神", x+20, y+150);
g.drawString("龟", x+20, y+280);
/*
//设置颜色
g.setColor(Color.RED);
//画矩形
g.drawRect(20, 150, 400,200);
//画实心
g.fillRect(130, 200, 120, 80);
//画圆和椭圆
g.drawOval(30, 250, 100, 100);
g.fillOval(130, 300,100, 120);
//画奥迪车标
g.drawOval(65, 60,20, 20);
g.drawOval(50, 60,20, 20);
g.drawOval(35, 60,20,20);
g.drawOval(20, 60,20, 20);
//画线段
g.drawLine(50, 150, 20, 10);
*/
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@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();
e1 = e.getX();
f1 = e.getY();
e2 = e.getX();
f2 = e.getY();
e3 = e.getX();
f3 = e.getY();
e4 = e.getX();
f4 = e.getY();
repaint();//重绘,重复调用paint方法
}
@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
}
@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 (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
窗体
package cn.edtu.day;
import javax.swing.JFrame;
/**
* JFrame--窗体--相框
* JFrame--画布--相片
* 导包:
* 1.手动导包:import...
* 2.自动导包:ctrl+shift+o
* 3.点击红色叉号:
* @author Administrator
*
*/
public class TurtlePro {
public static void main(String[] args){
//1.创建一个窗体
JFrame frame = new JFrame();
//2.加标题
frame.setTitle("傻龟");
//3.添加大小
frame.setSize(1500,900);
//4.窗体居中显示
frame.setLocationRelativeTo(null);
//类名:静态常量 减少内存使用
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//画布
TJPanel tj = new TJPanel();
//添加画布到面板
frame.add(tj);
//添加鼠标界面
frame.addMouseListener(tj);
tj.addMouseListener(tj);
//5.显示窗体
frame.setVisible(true);
}
}