首页 > 代码库 > java培训第一天--画板
java培训第一天--画板
1 package day1; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.Panel; 7 import java.awt.event.MouseAdapter; 8 import java.awt.event.MouseEvent; 9 10 import javax.swing.JFrame; 11 import javax.swing.JPanel; 12 13 //import org.omg.CORBA.PUBLIC_MEMBER; 14 15 //继承 16 public class day1_test extends JPanel{ 17 /** 18 * 19 */ 20 private static final long serialVersionUID = 1L; 21 22 //定义颜色 23 public Color colorRed = new Color(255,0,0); 24 public Color colorBlue = new Color(0,0,255); 25 public Color colorGreen = new Color(0, 255, 0); 26 public Color rectcolor = colorRed; 27 int x_axis = 0; 28 int y_axis = 0; 29 int paint_size = 0; 30 boolean isbegin = true; 31 boolean isclear = false; 32 //画板界面:画框 画布 33 public static void main(String args[]) 34 { 35 //画框 声明居然和C#一样 36 JFrame day1_frame = new JFrame(); 37 //变量用来指代内存中的一块空间用来存放不同的数据 38 day1_frame.setVisible(true); 39 //set size 40 day1_frame.setSize(1300, 720); 41 day1_frame.setResizable(false); 42 //set position 43 day1_frame.setLocationRelativeTo(null); 44 //close programe 45 day1_frame.setDefaultCloseOperation(2); 46 //画布 47 //JPanel day1_panel = new JPanel(); 48 //创建自己的画布 49 day1_test mypanel = new day1_test(); 50 //添加panel 51 //mypanel.setSize(1300, 55); 52 day1_frame.add(mypanel);//这里调用了paint 53 //在panel上面再建立panel 54 //方法的重写 paint 55 //这里调用timepaint 56 mypanel.TimePaint(); 57 } 58 59 /** 60 * 自己定义的方法 61 * **/ 62 63 64 public void paint(Graphics gr) 65 { 66 //画颜色选择区域 67 //画笔 68 //左上角x,y坐标 69 gr.setColor(colorRed); 70 gr.fillRect(0, 50, 1300,2); 71 gr.setColor(colorBlue); 72 gr.fillRect(0, 0, 1300, 2); 73 //第一个按钮绿色 74 gr.setColor(colorGreen); 75 gr.fillRect(0, 0, 100, 50); 76 77 gr.setColor( new Color(122,245,255)); 78 gr.fillRect(200, 0, 100, 50); 79 80 gr.setColor(colorRed); 81 gr.fillRect(400, 0, 100, 50); 82 83 gr.setColor(Color.ORANGE); 84 gr.fillRect(600, 0, 100, 50); 85 86 gr.setColor(Color.pink); 87 gr.fillRect(800, 0, 100, 50); 88 89 gr.setColor(Color.orange); 90 gr.drawRect(1000,0 , 100, 50); 91 //font参数 字体 风格 字号 92 gr.setFont(new Font("楷体", 0, 30)); 93 gr.drawString("clear", 1015, 35); 94 95 gr.drawRect(1200, 0, 100, 50); 96 gr.drawString("CLA", 1215, 35); 97 98 //画线,两个点 99 gr.setColor(Color.orange); 100 gr.drawLine(0, 55, 1300, 55); 101 //画线 102 if(!isbegin) 103 { 104 gr.setColor(rectcolor); 105 if(y_axis>55) 106 { 107 gr.fillRect(x_axis, y_axis, 10, 10); 108 } 109 } 110 //清屏的画法 111 if(isclear) 112 { 113 gr.setColor(getBackground()); 114 gr.fillRect(0, 55, 1300, 665); 115 isclear = false; 116 } 117 } 118 119 //定义鼠标事件 120 public void TimePaint() 121 { 122 MouseAdapter MA = new MouseAdapter() { 123 //鼠标事件:定义鼠标事件的类型 124 @Override 125 //鼠标拖动事件,拖动鼠标激发 126 public void mouseDragged(MouseEvent e) { 127 /* 128 * 需要repaint方法 129 * 不断刷新鼠标的坐标赋值给方框坐标 130 * */ 131 //获取鼠标位置 132 133 //基本类型 (结构简单int,共有8种:int[4],float[2],bool,char,string,)引用类型(结构复杂,比如画布。。。) 134 int locationx = e.getX(); 135 int locationy = e.getY(); 136 x_axis = locationx; 137 y_axis = locationy; 138 isbegin = false; 139 repaint(); 140 } 141 142 @Override 143 public void mouseClicked(MouseEvent e) { 144 int locationx = e.getX(); 145 int locationy = e.getY(); 146 if(locationx>0&&locationx<100&&locationy>0&&locationy<50) 147 { 148 rectcolor = colorGreen; 149 } 150 else if (locationx>200&locationx<300&locationy>0&locationy<50) { 151 rectcolor = new Color(122,245,255); 152 } 153 else if(locationx>400&locationx<500&locationy>0&locationy<50) 154 { 155 156 rectcolor = colorRed; 157 } 158 else if(locationx>600&locationx<700&locationy>0&locationy<50) 159 { 160 rectcolor = Color.ORANGE; 161 } 162 else if(locationx>800&locationx<900&locationy>0&locationy<50) 163 { 164 rectcolor = Color.pink; 165 } 166 else if(locationx>1000&locationx<1100&locationy>0&locationy<50) 167 { 168 rectcolor = getBackground(); 169 } 170 else if(locationx>1200&locationx<1300&locationy>0&locationy<50) 171 { 172 isclear = true; 173 //立即执行清屏 174 repaint(); 175 rectcolor = colorRed; 176 } 177 } 178 }; 179 addMouseMotionListener(MA);//动态监听 180 addMouseListener(MA);//静态监听 181 } 182 }
完全不想学java....
java培训第一天--画板
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。