Java画王八 - Wayne53777/Java- GitHub Wiki

1 package cn.zwz.day02;

import javax.swing.JFrame; /**

  • JFrame----窗体

  • JPanel----画布

  • 导包

  • 1、手动导包 import。。。

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

  • 3、点击红色的叉号

  • */ public class TurtlePro { public static void main(String[] args) {

    //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.add(tj);

    //5.显示窗体 frame.setVisible(true); }

} 2 package cn.zwz.day02;

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;

@SuppressWarnings("serial") public class TJPanel extends JPanel implements MouseListener,KeyListener,Runnable{ //@Override /*public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); //给画笔设置颜色 g.setColor(Color.red); //画矩形 g.drawRect(20, 150, 400, 600); //画实心 g.fillRect(120, 200, 160, 80); //画圆或椭圆 g.setColor(Color.blue); g.drawOval(40, 300, 100, 100); g.fillOval(30, 450, 200, 100); //奥迪车标 g.drawOval(40, 600, 30, 30); g.drawOval(60, 600, 30, 30); g.drawOval(80, 600, 30, 30); g.drawOval(100, 600, 30, 30); //画线段 g.drawLine(80, 80, 200, 200);

}

}*/

//乌龟的初始位置 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]; int[] b= new int[300]; //构造方法:方法名与类名相同,无返回值类型。 public TJPanel() { for(int i = 0; i<a.length;i++) { a[i]=(int) (Math.random()*1400);//(0-0.99999)*1400 1,399.99999 b[i]=(int) (Math.random()*800); } } @Override public void paint(Graphics g) { //alt+/ 快捷 // TODO Auto-generated method stub super.paint(g); //画波纹 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, 10, 20);
g.fillOval(x+50, y+30, 10, 20);

//加舌头
g.setColor(Color.red);
g.fillOval(x+30 ,y-20, 10, 30);

//画脚
//左上
g.setColor(Color.orange);
g.fillOval(k1-85, t1+76, 60,70);
//右上
g.setColor(Color.orange);
g.fillOval(k2+85, t2+76, 60,70);
//左下
g.setColor(Color.orange);
g.fillOval(k3-85, t3+235, 60,70);
//右下
g.setColor(Color.orange);
g.fillOval(k4+85, t3+235, 60,70);
//加乌龟壳
g.setColor(Color.green);
g.fillOval(x-66, y+66, 200, 230);
g.fillOval(x+20, y+200, 20, 130);
//壳加文字
		g.setColor(Color.red);
	
		//设置字体大小
		Font font=new Font(Font.DIALOG,Font.BOLD,30);
		g.setFont(font);
		g.drawString("王八", x-10, y+180);

/* //给画笔设置颜色 g.setColor(Color.red); //画矩形 g.drawRect(30, 100, 400, 600); //画实心 g.fillRect(120, 200, 160, 80); //画圆或椭圆 g.setColor(Color.blue); g.drawOval(40, 300, 100, 100); g.fillOval(30, 450, 200, 100); //奥迪车标 g.drawOval(40, 600, 30, 30); g.drawOval(60, 600, 30, 30); g.drawOval(80, 600, 30, 30); g.drawOval(100, 600, 30, 30); //画线段 g.drawLine(20, 50, 400, 800);

*/

}

@Override public void mouseClicked(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 mousePressed(MouseEvent e) { // TODO Auto-generated method stub x=e.getX(); y=e.getY(); repaint();//重绘 重复调用paint方法 k1=e.getX(); t1=e.getY(); k2=e.getX(); t2=e.getY(); k3=e.getX(); t3=e.getY(); k4=e.getX(); t4=e.getY();

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

}

}