package day1; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Panel; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPanel; //import org.omg.CORBA.PUBLIC_MEMBER; //继承 public class day1_test extends JPanel{ /** * */ private static final long serialVersionUID = 1L; //定义颜色 public Color colorRed = new Color(255,0,0); public Color colorBlue = new Color(0,0,255); public Color colorGreen = new Color(0, 255, 0); public Color rectcolor = colorRed; int x_axis = 0; int y_axis = 0; int paint_size = 0; boolean isbegin = true; boolean isclear = false; //画板界面:画框 画布 public static void main(String args[]) { //画框 声明居然和C#一样 JFrame day1_frame = new JFrame(); //变量用来指代内存中的一块空间用来存放不同的数据 day1_frame.setVisible(true); //set size day1_frame.setSize(1300, 720); day1_frame.setResizable(false); //set position day1_frame.setLocationRelativeTo(null); //close programe day1_frame.setDefaultCloseOperation(2); //画布 //JPanel day1_panel = new JPanel(); //创建自己的画布 day1_test mypanel = new day1_test(); //添加panel //mypanel.setSize(1300, 55); day1_frame.add(mypanel);//这里调用了paint //在panel上面再建立panel //方法的重写 paint //这里调用timepaint mypanel.TimePaint(); } /** * 自己定义的方法 * **/ public void paint(Graphics gr) { //画颜色选择区域 //画笔 //左上角x,y坐标 gr.setColor(colorRed); gr.fillRect(0, 50, 1300,2); gr.setColor(colorBlue); gr.fillRect(0, 0, 1300, 2); //第一个按钮绿色 gr.setColor(colorGreen); gr.fillRect(0, 0, 100, 50); gr.setColor( new Color(122,245,255)); gr.fillRect(200, 0, 100, 50); gr.setColor(colorRed); gr.fillRect(400, 0, 100, 50); gr.setColor(Color.ORANGE); gr.fillRect(600, 0, 100, 50); gr.setColor(Color.pink); gr.fillRect(800, 0, 100, 50); gr.setColor(Color.orange); gr.drawRect(1000,0 , 100, 50); //font参数 字体 风格 字号 gr.setFont(new Font("楷体", 0, 30)); gr.drawString("clear", 1015, 35); gr.drawRect(1200, 0, 100, 50); gr.drawString("CLA", 1215, 35); //画线,两个点 gr.setColor(Color.orange); gr.drawLine(0, 55, 1300, 55); //画线 if(!isbegin) { gr.setColor(rectcolor); if(y_axis>55) { gr.fillRect(x_axis, y_axis, 10, 10); } } //清屏的画法 if(isclear) { gr.setColor(getBackground()); gr.fillRect(0, 55, 1300, 665); isclear = false; } } //定义鼠标事件 public void TimePaint() { MouseAdapter MA = new MouseAdapter() { //鼠标事件:定义鼠标事件的类型 @Override //鼠标拖动事件,拖动鼠标激发 public void mouseDragged(MouseEvent e) { /* * 需要repaint方法 * 不断刷新鼠标的坐标赋值给方框坐标 * */ //获取鼠标位置 //基本类型 (结构简单int,共有8种:int[4],float[2],bool,char,string,)引用类型(结构复杂,比如画布。。。) int locationx = e.getX(); int locationy = e.getY(); x_axis = locationx; y_axis = locationy; isbegin = false; repaint(); } @Override public void mouseClicked(MouseEvent e) { int locationx = e.getX(); int locationy = e.getY(); if(locationx>0&&locationx<100&&locationy>0&&locationy<50) { rectcolor = colorGreen; } else if (locationx>200&locationx<300&locationy>0&locationy<50) { rectcolor = new Color(122,245,255); } else if(locationx>400&locationx<500&locationy>0&locationy<50) { rectcolor = colorRed; } else if(locationx>600&locationx<700&locationy>0&locationy<50) { rectcolor = Color.ORANGE; } else if(locationx>800&locationx<900&locationy>0&locationy<50) { rectcolor = Color.pink; } else if(locationx>1000&locationx<1100&locationy>0&locationy<50) { rectcolor = getBackground(); } else if(locationx>1200&locationx<1300&locationy>0&locationy<50) { isclear = true; //立即执行清屏 repaint(); rectcolor = colorRed; } } }; addMouseMotionListener(MA);//动态监听 addMouseListener(MA);//静态监听 } }
完全不想学java....