长沙网站制作公司报价,php网站是什么数据库文件,域名注册万网,小程序登录入口代码Java制作简单画板#xff0c;包括两个类#xff0c;一个主要画板类Drawpad#xff0c;一个画板监听器DrawListener类。
1、Drawpad类#xff0c;包括画板#xff0c;画板功能设计#xff0c;保存图片等
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2…Java制作简单画板包括两个类一个主要画板类Drawpad一个画板监听器DrawListener类。
1、Drawpad类包括画板画板功能设计保存图片等
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 package Java课程设计; import java.awt.Graphics; import javax.imageio.ImageIO; import javax.print.DocFlavor.STRING; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import java.awt.AWTException; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Shape; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.filechooser.FileNameExtensionFilter; public class Drawpad { ?? ? static Color color1; public static void main(String[] args) { ?? ?Drawpad dp new Drawpad(); ?? ?dp.initUI(); ?? ?? } ? ? ?//创建一个JFrame图形窗口 ?? ?public void initUI() { ?? ??? ?JFrame jf new JFrame(); ?? ??? ?jf.setTitle(创意画图板(勿拖动)); ?? ??? ?jf.setSize(1500,1000); ?? ??? ?jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时退出 ?? ??? ?jf.setLocationRelativeTo(null);//居中不用定位窗口大小 ?? ??? ?//创建字体之后所有的字体为该字体 ?? ??? ?Font fnew Font(方正仿宋简体, Font.BOLD, 20); ?? ??? ?//创建画笔监听器 ?? ??? ?DrawListener ?dl new DrawListener(); ?? ??? ?//创建读取图片BufferedImage(将图片加载到drawPanel面板中)和画笔g画笔g为在保存图片上进行图画。 ?? ??? ??? ? ? ?BufferedImage bi new BufferedImage(1300,800, BufferedImage.TYPE_INT_ARGB); ?? ??? ??? ? ? ?Graphics2D g bi.createGraphics(); ?? ??? ??? ? ? ?//初始化时填充白色 ?? ??? ??? ? ? ?g.setColor(Color.WHITE); ?? ??? ??? ? ? ?//先将图片填充为白色 ?? ??? ??? ??? ?g.fillRect(0, 0, 1300,800); ?? ??? ??? ??? ? ?? ??? ??? ??? ? ?? ??? ?//设置增加菜单栏包括保存和新建两个按钮 ?? ??? ?JMenuBar boxnew JMenuBar(); ?? ??? ?//在窗体上加菜单条,做一个菜单条,是菜单条不是工具栏 ?? ??? ?//创建menubtn1保存按钮并加上监听器以图片的形式保存绘画板上的内容 ?? ??? ?JButton menubtn1new JButton(保存); ?? ??? ?//为保存按钮注册监听器 ?? ??? ? ?menubtn1.addActionListener(new ActionListener(){ ? ? ??? ??? ??? ?Override ? ? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ? ? ??? ??? ??? ??? ?//创建文件保存窗口 ? ? ??? ??? ??? ??? ?JFileChooser fnew JFileChooser(保存); ? ? ??? ??? ??? ??? ?int returnVal f.showSaveDialog(null); ? ? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?File?? ?file1null; ?? ??? ??? ??? ??? ?if(returnVal JFileChooser.APPROVE_OPTION) { ?? ??? ??? ??? ??? ? ? ?file1 f.getSelectedFile(); ?? ??? ??? ??? ??? ??? ?String name f.getName(file1); ?? ??? ??? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ??? ?ImageIO.write(bi, PNG, new File(f.getCurrentDirectory(),name.png)); ?? ??? ??? ??? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ??? ??? ??? ?//需抛出异常 ?? ??? ??? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ? ? ??? ??? ??? ?} ? ? ??? ??? ? }); ?? ??? ? /*JButton menubtn2new JButton(打开); ?? ??? ? ?//为打开按钮注册监听器 ?? ??? ? ?menubtn1.addActionListener(new ActionListener(){ ? ??? ??? ??? ?Override ? ??? ??? ??? ?//获取当前画笔粗细 ? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ? ??? ??? ??? ??? ?BufferedImage bimg null; ? ??? ??? ??? ??? ?JFileChooser fnew JFileChooser(打开); ??? ??? ??? ??? ?int returnVal f.showOpenDialog(null); ??? ??? ??? ??? ? ?? ??? ??? ??? ?File?? ?file1null; ?? ??? ??? ??? ?if(returnVal JFileChooser.APPROVE_OPTION) { ?? ??? ??? ??? ? ? ?file1 f.getSelectedFile(); ?? ??? ??? ??? ??? ?String name f.getName(file1); ?? ??? ??? ??? ??? ?try { ? ? ? ? ? ? ? ? ? ? ?? ?? ??? ??? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ? ??? ??? ??? ??? ? ? ??? ??? ??? ??? ? ? ??? ??? ??? ?} ? ??? ??? ? });*/ ?? ??? ? ? ?? ??? ?//创建menubtn3退出按钮并加上监听器退出程序 ?? ??? ? ?JButton menubtn3new JButton(退出); ?? ??? ? ?menubtn3.addActionListener(new ActionListener(){ ?? ? ? ?? ??? ??? ?Override ?? ? ? ?? ??? ??? ?//获取当前画笔粗细 ?? ? ? ?? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ? ? ?? ??? ??? ??? ?int retJOptionPane.showConfirmDialog(null, 你确定要退出吗, 确认退出, JOptionPane.YES_NO_OPTION); ?? ? ? ?? ??? ??? ??? ?if(retJOptionPane.YES_OPTION){ ?? ? ? ?? ??? ??? ??? ??? ?//“确认”退出程序 ?? ? ? ?? ??? ??? ??? ??? ?System.exit(0); ?? ? ? ?? ??? ??? ??? ?} ?? ? ? ?? ??? ??? ?} ?? ? ? ?? ??? ? }); ?? ??? ? ?box.add(menubtn1); ?? ??? ? // box.add(menubtn2); ?? ??? ? ?box.add(menubtn3); ?? ??? ?//jf.setJMenuBar(box); ?? ??? ? ?? ??? ?jf.setJMenuBar(box); ?? ??? ? ?? ??? ?//jf用BorderLayout布局 ?? ??? ? ?? ??? ?//北边,画板模式功能栏 ?? ??? ?JPanel funcPanelnew JPanel(); ?? ??? ?jf.add(funcPanel,BorderLayout.NORTH); ?? ??? ? ?? ??? ?//中间,画布 ?? ??? ?JPanel drawPanelnew JPanel(); ?? ??? ?jf.add(drawPanel,BorderLayout.CENTER); ?? ??? ?drawPanel.setPreferredSize(new Dimension(1000,700)); ?? ??? ?drawPanel.setBackground(dl.background); ?? ??? ?//一定要在画布上加上监听器!!1若画布没有加上监听器无法显示 ?? ??? ?drawPanel.addMouseListener(dl); ?? ??? ?drawPanel.addMouseMotionListener(dl); ?? ??? ? ?? ??? ?//南边,为画笔颜色选择按钮 ?? ??? ?JPanel colorPanelnew JPanel(); ?? ??? ?jf.add(colorPanel,BorderLayout.SOUTH); ?? ??? ? ?? ??? ?//右边,为选择背景颜色按钮、画笔粗细选择按钮 ?? ??? ?JPanel backgroundPanelnew JPanel(); ?? ??? ?jf.add(backgroundPanel,BorderLayout.EAST); ?? ??? ?backgroundPanel.setPreferredSize(new Dimension(150,1000)); ?? ??? ? ?? ??? ?//左边,获取当前状态如:背景颜色、画笔颜色、画笔性质 ?? ??? ?JPanel nowPanelnew JPanel(); ?? ??? ?jf.add(nowPanel,BorderLayout.WEST); ?? ??? ?nowPanel.setPreferredSize(new Dimension(180,1000)); ?? ??? ? ?? ??? ?//左边放入当前状态Panel ?? ??? ?nowPanel.setBackground(Color.WHITE); ?? ??? ? JLabel label2new JLabel(当前背景颜色); ?? ??? ? ?label2.setFont(f); ?? ??? ? ? nowPanel.add(label2); ?? ??? ? ? //放入当前背景颜色 ?? ??? ? ? JButton nowbackgroundColornew JButton(); ? ? ? ? ? ?nowbackgroundColor.setPreferredSize(new Dimension(60,60)); ? ? ? ? ? ?nowbackgroundColor.setBackground(Color.WHITE);//背景初始化为灰色 ?? ??? ? ? nowPanel.add(nowbackgroundColor); ?? ??? ? ? //放入当前画笔 ?? ??? ? ? JLabel label3new JLabel(请选择画笔模式); ?? ??? ??? ? ?label3.setFont(f); ?? ??? ??? ? ? nowPanel.add(label3); ?? ??? ? ? //放入当前画笔颜色 ?? ??? ? ? JButton nowColornew JButton(); ? ? ? ? ? ?nowColor.setPreferredSize(new Dimension(60,60)); ? ? ? ? ? ?nowColor.setBackground(Color.BLACK);//画笔颜色初始化为黑色色 ?? ??? ? ? nowPanel.add(nowColor); ?? ??? ? ? ?? ??? ??? ?//获取当前画笔模式 ?? ??? ??? ?JLabel label4new JLabel(当前画笔模式); ?? ??? ??? ? ?label4.setFont(f); ?? ??? ??? ? ? nowPanel.add(label4); ?? ??? ??? ? ? JTextField textnew JTextField(dl.btncontent); //获得选择画笔模式的按钮内容得到当前画笔模式 ?? ??? ??? ? ? text.setPreferredSize(new Dimension (160,60)); ?? ??? ??? ? ? text.setFont(f); ?? ??? ??? ? ? text.setEditable(false); ?//不可改 ?? ??? ??? ?nowPanel.add(text); ?? ??? ??? ?//获取当前画笔粗细状态 ?? ??? ??? ?JLabel label6new JLabel(当前画笔粗细(中)); ?//默认粗细为中 ?? ??? ??? ? ?label6.setFont(f); ?? ??? ??? ? ? nowPanel.add(label6); ?? ??? ??? ? ? JTextField text1new JTextField(请选择画笔粗细); ?? ??? ??? ? ? text1.setPreferredSize(new Dimension (160,60)); ?? ??? ??? ? ? text1.setFont(f); ?? ??? ??? ? ? text1.setEditable(false); //不可编辑 ?? ??? ??? ?nowPanel.add(text1); ?? ??? ??? ?//输入需要添加的文字 ?? ??? ??? ?JLabel label7new JLabel(请输入文字:); ?? ??? ??? ? ?label7.setFont(f); ?? ??? ??? ? ? nowPanel.add(label7); ?? ??? ??? ? ? JTextField text2new JTextField(); ?? ??? ??? ? ? text2.setPreferredSize(new Dimension (160,60)); ?? ??? ??? ? ? text2.setFont(f); ?? ??? ??? ? ? nowPanel.add(text2);? ?? ??? ??? ? ? JLabel label8new JLabel(请输入文字样式:); ?? ??? ??? ??? ? ?label8.setFont(f); ?? ??? ??? ??? ? ? nowPanel.add(label8); ?? ??? ??? ??? ? ? JTextField text3new JTextField(方正仿宋简体); ?? ??? ??? ??? ? ? text3.setPreferredSize(new Dimension (160,60)); ?? ??? ??? ??? ? ? text3.setFont(f); ?? ??? ??? ??? ? ? nowPanel.add(text3); ?? ??? ??? ??? ? ? JLabel label9new JLabel(请输入文字大小:); ?? ??? ??? ??? ??? ? ?label9.setFont(f); ?? ??? ??? ??? ??? ? ? nowPanel.add(label9); ?? ??? ??? ??? ??? ? ? JTextField text4new JTextField(20); ?? ??? ??? ??? ??? ? ? text4.setPreferredSize(new Dimension (160,60)); ?? ??? ??? ??? ??? ? ? text4.setFont(f); ?? ??? ??? ??? ??? ? ? nowPanel.add(text4); ?? ??? ??? ?//为获取文字内容加一个按钮并加上监听器 ?? ??? ??? ? ? JButton getcontentnew JButton(获取文字); ?? ??? ??? ? ? getcontent .setFont(f); ?? ??? ??? ??? ?getcontent.setBackground(Color.YELLOW); ?? ??? ??? ??? ?getcontent.addActionListener(new ActionListener(){ ?? ??? ??? ??? ??? ?Override ?? ??? ??? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ??? ??? ? String contenttext2.getText(); ?? ??? ??? ??? ??? ??? ?String modetext3.getText(); ?? ??? ??? ??? ??? ??? ?String sizetext4.getText(); ?? ??? ??? ??? ??? ??? ?dl.modemode; //获取文字样式 ?? ??? ??? ??? ??? ??? ? ? dl.contentcontent; //获取文字内容 ?? ??? ??? ??? ??? ??? ? ? dl.sizesize; //获取文字大小 ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ? }); ?? ??? ??? ??? ?nowPanel.add(getcontent); ?? ??? ??? ??? ? ?? ??? ??? ??? ?//最后在当前状态画板中加一个清除画布内容的功能 ?? ??? ??? ??? ?JButton clearnew JButton(清除); ?? ??? ??? ??? ? ?clear.setFont(f); ?? ??? ??? ??? ??? ?clear.setBackground(Color.RED); ?? ??? ??? ??? ??? ?clear.addActionListener(dl); ?? ??? ??? ??? ??? ?nowPanel.add(clear); ?? ??? ??? ??? ??? ? ?? ??? ?//添加按钮到北边每个按钮写两行代码太多通过数组方式添加按钮 ?? ??? ??? ??? ?//加入标签选择画笔模式 ?? ??? ??? ??? ?JLabel labelh new JLabel(选择画笔模式); ?? ??? ??? ??? ?labelh.setFont(f); ?? ??? ??? ??? ?funcPanel.add(labelh); ?? ??? ??? ??? ?//将按钮名字保存在数组中后依次存储 ?? ??? ?String[] btnstr {画笔,直线,矩形,填充矩形,圆,填充圆,弧线,喷枪,波形,分形,长方体,九宫格递归,文字,橡皮}; ?? ??? ?//将画笔状态按钮防置panel中 ?? ??? ?for( int i0;ibtnstr.length;i) { ?? ??? ??? ?JButton btnnew JButton(btnstr[i]); ?? ??? ??? ?funcPanel.add(btn); ?? ??? ??? ?btn .setFont(f); ?? ??? ??? ?btn.setBackground(Color.white); ?? ??? ??? ?//加上画笔监听器 ?? ??? ??? ?btn.addActionListener(dl); ?? ??? ??? ?//加上监听器获取当前 画笔模式 ?? ??? ??? ?btn.addActionListener(new ActionListener(){ ?? ??? ??? ??? ?Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ??? ?text.setText(btn.getText()); //在当前模式加入选取的画笔模式 ?? ??? ??? ??? ?} ?? ??? ??? ? }); ?? ??? ??? ? ?? ??? ?}; ?? ??? ? ?? ??? ?//在BrderLayout布局SOUTH添加选择颜色按钮 ?? ??? ?JLabel label new JLabel(选择画笔(橡皮)颜色); ?? ??? ?label.setFont(f); ?? ??? ?colorPanel.add(label); ?? ??? ? ?? ??? ? //添加颜色按钮 ?? ??? ?Color[] colorArray { Color.BLUE, Color.GREEN, Color.RED,? ? ? ? ? ? ? ? ? Color.BLACK,Color.ORANGE,Color.PINK,Color.CYAN, ? ? ? ? ? ? ? ? Color.MAGENTA,Color.DARK_GRAY,Color.GRAY, ? ? ? ? ? ? ? ? Color.LIGHT_GRAY,Color.YELLOW,Color.WHITE}; ?? ??? ? ?? ??? ?//在布局管理器中添加颜色按钮 ? ? ? ? for( int i0;icolorArray.length;i) { ?? ??? ??? ? ? ? ? ? ?? ?JButton button new JButton(); ? ? ? ? ? ? button.setBackground(colorArray[i]); ? ? ? ? ? ? button.setPreferredSize(new Dimension(50, 50)); ? ? ? ? ? ? button.addActionListener(dl); ? ? ? ? ? ? colorPanel.add(button); ? ? ? ? ? ? //获取当前状态的画笔颜色 ? ? ? ? ? ? button.addActionListener(new ActionListener(){ ?? ??? ??? ??? ?Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ??? ?nowColor.setBackground(button.getBackground()); ?//在当前画笔颜色按钮加入选择的按钮颜色 ?? ??? ??? ??? ?} ?? ??? ??? ? }); ?? ??? ?}; ?? ??? ? ?? ? ?funcPanel.setBackground(Color.gray); ?? ? ? ?? ? ?//添加背景主板颜色按钮并设置监听器背景颜色为按钮颜色 ?? ? ?JLabel label1new JLabel(选择背景颜色); ?? ? ?label1.setFont(f); ?? ? ? backgroundPanel.add(label1); ?? ? ?Color[] backgroundArray { Color.GREEN, Color.RED, ? ? ? ? ? Color.ORANGE,Color.PINK,Color.CYAN, ? ? ? ? ? ? ? Color.MAGENTA,Color.DARK_GRAY,Color.GRAY, ? ? ? ? ? ? ? Color.LIGHT_GRAY,Color.YELLOW,Color.WHITE,Color.BLACK}; ?? ? ?//将按钮加入进去 ?? ? ?for( int i0;ibackgroundArray.length;i) { ?? ??? ??? ? ? ? ? ?? ?JButton button new JButton(); ? ? ? ? ? button.setBackground(backgroundArray[i]); ? ? ? ? ? button.setPreferredSize(new Dimension(50, 50)); ? ? ? ? ? backgroundPanel.add(button); ? ? ? ? ? //添加监听器按下按钮改变背景颜色同时体现当前状态 ?? ??? ?button.addActionListener(new ActionListener(){ ?? ??? ??? ?Override ?? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ?drawPanel.setBackground(button.getBackground()); //将背景颜色改为选取的背景颜色 ?? ??? ??? ??? ?color1button.getBackground(); ?? ??? ??? ??? ?dl.backgroundcolor1; ?//将背景颜色传给DrawListener中的变量 ?? ??? ??? ? ? ?System.out.println(color1); ?? ??? ??? ??? ?g.setColor(color1); ?? ??? ??? ??? ?g.fillRect(0, 0, 1300,800); ?//图片画笔填充背景颜色 ?? ??? ??? ??? ?nowbackgroundColor.setBackground(button.getBackground()); ?? ??? ??? ?} ?? ??? ? }); ?? ??? ?}; ?? ??? ? ?? ??? ?//添加选择画笔粗细的按钮,可选择画笔的粗细 ?? ??? ?JLabel label5new JLabel(选择画笔粗细); ?? ??? ? ?label5.setFont(f); ?? ??? ? ? backgroundPanel.add(label5); ?? ??? ? ? String[] Size{细,中,粗}; ?? ??? ? ? //选择画笔模式的按钮 ?? ??? ? ? for(int i0;i3;i){ ?? ??? ??? ? ? JButton graphsizenew JButton(Size[i]); ?? ??? ??? ? ? graphsize.setFont(new Font(宋体, Font.BOLD, 15)); ?? ??? ??? ? ? graphsize.setBackground(Color.WHITE); ?? ??? ? ? ? ? graphsize.setPreferredSize(new Dimension(50, 50)); ?? ??? ? ? ? ? backgroundPanel.add(graphsize); ? ? ? ? ? ? ? ?graphsize.addActionListener(dl); ? ? ? ? ? ? ? ?graphsize.addActionListener(new ActionListener(){ ? ? ? ??? ??? ??? ?Override ? ? ? ??? ??? ??? ?//获取当前画笔粗细 ? ? ? ??? ??? ??? ?public void actionPerformed(ActionEvent e) { ? ? ? ??? ??? ??? ??? ?text1.setText(graphsize.getText()); //获取当前画笔模式 ? ? ? ??? ??? ??? ?} ? ? ? ??? ??? ? }); ?? ??? ? ? } ?? ??? ?jf.setVisible(true); ?? ??? ?// 获取这个界面的属性, 画笔 g ?? ??? ?//Graphics2D g (Graphics2D) drawPanel.getGraphics(); ?? ??? ?//drawPanel.paintComponent(g); ?? ??? ? Graphics2D g1 (Graphics2D) drawPanel.getGraphics(); ?? ??? ? ?? ??? ?//为画笔添加监听器 ?? ??? ?drawPanel.addMouseListener(dl); ?? ??? ?dl.g ?g1;// 右传左? ?? ??? ?dl.g3 g;// 右传左 ?? ??? ? ?? ?} }
2、DrawListner类画板功能监听器
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 package Java课程设计; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseMotionListener; import java.awt.geom.AffineTransform; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import java.awt.Color; import java.util.ArrayList; import java.util.Random; import Java课程设计.Drawpad; public class DrawListener implements MouseListener,ActionListener,MouseMotionListener { ?? ?//获取画笔 ?? ?Graphics2D g; ?? ?//获取在保存图片上的画笔 ?? ??? ?Graphics2D g3; ?? ?//获取按钮内容 ?? ?String btnstr; ?? ?Color backgroundColor.white; //背景颜色默认为白色 ?? ?Color graphcolorColor.BLACK; //画笔颜色默认为黑色 ?? ?JButton btn; ?? ?int x1, y1, x2, y2;// 声明坐标变量? ?? ?int x3400; ?? ?int y30; ?? ?int graphsize3;//默认为中等画笔 ?? ?String btncontent画笔; //默认画笔模式为画笔 ?? ?String content; ?//获取文字中的文字内容 ?? ?String mode方正仿宋简体; ?//文字样式默认为“方正仿宋简体” ?? ?String size20; ?? ? ?? ?//九宫格递归方法,画出九宫格 ?? ?public void dg(int x,int y,int width,int height) { ?? ??? ?//九宫格函数九宫格的实现 ?? ??? ? if(width3) { ?? ??? ??? ??? ?return; ?? ??? ??? ? ? ?} ?? ??? ?if(width90) { ?? ??? ?g.fillRect(xwidth/3, yheight/3, width/3, height/3); ?? ??? ?g3.fillRect(xwidth/3, yheight/3, width/3, height/3); ?? ??? ?dg(x, y, width/3, height/3); ?? ??? ?dg(xwidth/3, y, width/3, height/3); ?? ??? ?dg(x(width/3)*2, y, width/3, height/3); ?? ??? ?dg(x, yheight/3, width/3, height/3); ?? ??? ?dg(x, y(height/3)*2, width/3, height/3); ?? ??? ? ?? ??? ?dg(xwidth/3, yheight/3, width/3, height/3); ?? ??? ?dg(xwidth/3, y(height/3)*2, width/3, height/3); ?? ??? ? ?? ??? ?dg(x(width/3)*2, yheight/3, width/3, height/3); ?? ??? ?dg(x(width/3)*2, y(height/3)*2, width/3, height/3); ?? ??? ? ?? ??? ?} ?? ? ?//九宫格的实现 ?? ? ? else { ?? ??? ? ? g.drawOval(xwidth/3, yheight/3, width/3, height/3); ?? ??? ? ? g3.drawOval(xwidth/3, yheight/3, width/3, height/3); ?? ??? ? ? dg(x, y, width/3, height/3); ?? ??? ? ? dg(xwidth/3, y, width/3, height/3); ?? ??? ??? ?dg(x(width/3)*2, y, width/3, height/3); ?? ??? ??? ?dg(x, yheight/3, width/3, height/3); ?? ??? ??? ?dg(x, y(height/3)*2, width/3, height/3); ?? ??? ??? ? ?? ??? ??? ?dg(xwidth/3, yheight/3, width/3, height/3); ?? ??? ??? ?dg(xwidth/3, y(height/3)*2, width/3, height/3); ?? ??? ??? ? ?? ??? ??? ?dg(x(width/3)*2, yheight/3, width/3, height/3); ?? ??? ??? ?dg(x(width/3)*2, y(height/3)*2, width/3, height/3); ?? ? ? }?? ? ?? ? ?? ?} ?? ?//判断是颜色按钮还是画笔按钮,改变的全部是画笔按钮 ?? ?public void actionPerformed(ActionEvent e) { ?? ??? ?btnstre.getActionCommand(); ?//获取按钮的文字内容 ?? ??? ?//g.setColor(Color.black); ?? ??? ?//如果为颜色按钮将画笔改颜色 ?? ??? ?if(btnstr.equals(清除)){ ?? ??? ??? ?//重新填充背景同时将画笔置为背景颜色 ?? ??? ??? ? System.out.println(background); ?? ??? ??? ?g.setColor(background);//保存图片画笔填充背景颜色 ?? ? ? ??? ?g.fillRect(0, 0, 1300, 800); ?? ? ? ??? ?g3.setColor(background);//画笔重新填充背景 ?? ? ? ??? ?g3.fillRect(0, 0, 1300, 800); ?? ? ? ??? ?g.setColor(graphcolor); ?? ? ? ??? ?g3.setColor(graphcolor); ?? ??? ?} ?? ??? ?else{ ?? ??? ?if(btnstr.equals()) { ?? ??? ??? ?//获取点击内容,将其内容强制转换成JButton ?? ??? ? ? btn(JButton) e.getSource(); ?? ??? ??? ?//获取颜色按钮颜色 ?? ??? ? ? graphcolorbtn.getBackground(); ?? ??? ??? ? ?? ??? ?} ?? ??? ?//若为画笔粗细获取粗细大小 ?? ??? ?else if(btnstr.equals(细)){ ?? ??? ??? ?graphsize1; ?//画笔大小为细大小size为1 ?? ??? ?} ?? ??? ?else if(btnstr.equals(中)){ ?? ??? ??? ?graphsize3; ?? ??? ?} ?? ??? ?else if(btnstr.equals(粗)){ ?? ??? ??? ?graphsize5; ?? ??? ?} ?? ??? ?else{ ?? ??? ??? ?btncontentbtnstr; //获取画笔模式按钮的内容 ?? ??? ?} ?? ??? ?} ?? ?} ?? ?//鼠标点击方法 ?? ?Override ?? ?public void mouseClicked(MouseEvent e) { ?? ??? ?System.out.println(点击); ?? ?} ? ?//鼠标按下方法 ?? ?Override ?? ?public void mousePressed(MouseEvent e) { ?? ??? ?System.out.println(按下); ?? ??? ?x1e.getX(); ?? ??? ?y1 e.getY(); ?? ?} ? ? //重写鼠标释放时的方法 ?? ?Override ?? ?public void mouseReleased(MouseEvent e) { ?? ??? ?g.setColor(graphcolor);//获取保存画笔的颜色 ?? ??? ?g3.setColor(graphcolor); //获取画板画笔的颜色 ?? ??? ? ?? ??? ?x2e.getX(); ?? ??? ?y2 e.getY(); ?? ??? ?//选取画笔模式为直线时 ?? ??? ?if(btncontent.equals(直线)) { ?? ??? ??? ?g.setStroke(new BasicStroke(graphsize)); //保存画笔进行画图 ?? ??? ?g.drawLine(x1, y1, x2, y2);//画笔画直线 ?? ??? ?g3.setStroke(new BasicStroke(graphsize));//置画笔大小 ?? ??? ?g3.drawLine(x1, y1, x2, y2); ?? ??? ?} ?? ??? ?//选取画笔模式为波形时 ?? ??? ?else if(btncontent.equals(波形)) { ?? ??? ??? ?//波形函数 ?? ??? ??? ?g.setStroke(new BasicStroke(graphsize)); //置画笔大小 ?? ??? ??? ?g3.setStroke(new BasicStroke(graphsize)); ?? ??? ??? ?double x4 0,y4 0; ?? ??? ??? ?double a21.40,b21.56,c21.40,d2-6.56; ?? ??? ??? ?//波形函数 ?? ??? ??? ?for(int i0;i5000;i) { ?? ??? ??? ??? ?double x5Math.sin(a2*x4)-Math.cos(b2*y4); ?? ??? ??? ??? ?double y5Math.sin(c2*x4)-Math.cos(d2*y4); ?? ??? ??? ??? ?x4x5; ?? ??? ??? ??? ?y4y5; ?? ??? ??? ??? ?int px(int)(x5*100x1); ?? ??? ??? ??? ?int py(int)(y5*100y1); ?? ??? ??? ??? ?//画波形 ?? ??? ??? ??? ?g.drawLine(px, py, px, py); ?? ??? ??? ??? ?g3.drawLine(px, py, px, py); ?? ??? ??? ??? ?} ?? ??? ?} ?? ??? ?//选取画笔模式为矩形时 ?? ??? ?else if(btncontent.equals(矩形)) { ?? ??? ??? ?g.setStroke(new BasicStroke(graphsize)); //获取矩形画笔的大小 ?? ??? ??? ?g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));//画矩形 ?? ??? ??? ?g3.setStroke(new BasicStroke(graphsize));? ?? ??? ??? ?g3.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); ?? ??? ?} ?? ??? ?//选取的画笔模式为填充矩形 ?? ??? ?else if(btncontent.equals(填充矩形)){ ?? ??? ??? ?//画填充矩形 ?? ??? ??? ?g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); ?? ??? ??? ?g3.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); ?? ??? ?} ?? ??? ?//长方体函数 ?? ??? ?else if(btncontent.equals(长方体)){ ?? ??? ??? ?g.setStroke(new BasicStroke(graphsize));//获取长方体画笔大小 ?? ??? ??? ? g.setColor(btn.getBackground());//将画笔颜色置选择画笔颜色按钮颜色 ?? ??? ??? ? //长方体函数 ?? ? ? ? ? ? ? ?g.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2)); ?? ? ? ? ? ? ? ?g3.setStroke(new BasicStroke(graphsize)); ?? ??? ??? ??? ? g3.setColor(btn.getBackground()); ?? ??? ? ? ? ? ? ? ?g3.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2)); ?? ? ? ? ? ? ? ?int a,b,c,d; ?? ? ? ? ? ? ? ?aMath.min(x1, x2); ?? ? ? ? ? ? ? ?bMath.max(x1, x2); ?? ? ? ? ? ? ? ?cMath.min(y1, y2); ?? ? ? ? ? ? ? ?dMath.max(y1, y2); ?? ? ? ? ? ? ? ?int m(int)((b-a)*Math.cos(Math.PI/4)*Math.sin(Math.PI/4)); ?? ? ? ? ? ? ? ?int n(int)((b-a)*Math.cos(Math.PI/4)*Math.sin(Math.PI/4)); ?? ? ? ? ? ? ? ?//顶面 ?? ? ? ? ? ? ? ?g.setColor(btn.getBackground()); ?? ? ? ? ? ? ? ?g.fillPolygon(new int[] {a, am, bm,b},new int[] {c,c-n,c-n,c},4); ?? ? ? ? ? ? ? ?//右侧面 ?? ? ? ? ? ? ? ?g.setColor(btn.getBackground()); ?? ? ? ? ? ? ? ?g.fillPolygon(new int[] {b, b, bm,bm},new int[] {c,d,d-n,c-n},4); ?? ? ? ? ? ? ? ?g3.setColor(btn.getBackground()); ?? ? ? ? ? ? ? ?g3.fillPolygon(new int[] {a, am, bm,b},new int[] {c,c-n,c-n,c},4); ?? ? ? ? ? ? ? ?//右侧面 ?? ? ? ? ? ? ? ?g3.setColor(btn.getBackground()); ?? ? ? ? ? ? ? ?g3.fillPolygon(new int[] {b, b, bm,bm},new int[] {c,d,d-n,c-n},4); ?? ??? ?} ?? ??? ?//分形函数 ?? ??? ?else if(btncontent.equals(分形)){ ?? ??? ??? ?g.setStroke(new BasicStroke(graphsize)); ?//获取画笔大小 ?? ??? ??? ?g3.setStroke(new BasicStroke(graphsize)); ?? ??? ??? ?double x 0,y 0; ?? ??? ??? ?//分形函数实现 ?? ??? ??? ?double a1-1.8,b-2.0,c-0.5,d-0.9; ?? ??? ??? ?for(int i0;i5000;i) { ?? ??? ??? ?double x3Math.sin(a1*y)-c*Math.cos(a1*x); ?? ??? ??? ?double y3Math.sin(b*x)-d*Math.cos(b*y); ?? ??? ??? ?xx3; ?? ??? ??? ?yy3; ?? ??? ??? ?int px(int)(x3*100x1); ?? ??? ??? ?int py(int)(y3*100y1); ?? ??? ??? ?g.drawLine(px, py, px, py); ?? ??? ??? ?g3.drawLine(px, py, px, py); ?? ??? ?} ?? ??? ?} ?? ??? ?//画圆 ?? ? ? ?else if(btncontent.equals(圆)) { ?? ? ? ??? ?g.setStroke(new BasicStroke(graphsize));//获取画笔大小 ?? ??? ??? ?g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));//画圆 ?? ??? ??? ?g3.setStroke(new BasicStroke(graphsize)); ?? ??? ??? ?g3.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); ?? ??? ?} ?? ??? ?//画填充圆 ?? ? ? ?else if(btncontent.equals(填充圆)){ ?? ? ? ??? ?g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));//画填充圆 ?? ? ? ??? ?g3.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); ?? ? ? ?} ?? ??? ?//当选取模式为文字 ?? ? ? ?else if(btncontent.equals(文字)){ ?? ? ? ??? ?//获取画笔大小 ?? ? ? ??? ?g.setStroke(new BasicStroke(15)); ? ? ? ? Font font new Font(mode, Font.BOLD, Integer.parseInt(size)); //获得文字内容文字大小文字样式 ? ? ? ? ? ? ?g.setFont(font); //在画笔中置文字样式和大小 ?? ? ? ??? ?g.drawString(content, x1, y1); //写上文字内容 ?? ? ? ??? ?g3.setStroke(new BasicStroke(15)); ?? ? ? ??? ? g3.setFont(font);//放入文字样式和大小 ?? ? ? ??? ?g3.drawString(content, x1, y1); ?? ? ? ?} ?? ??? ?//当画笔模式为弧线时 ?? ? ? ?else if(btncontent.equals(弧线)){ ?? ? ? ??? ?g.setStroke(new BasicStroke(graphsize));//获取画笔大小 ?? ? ? ??? ?//弧线函数 ?? ??? ??? ? g.drawArc(x1, y1, 100, 60, 0, 180); ?? ??? ??? ? g3.setStroke(new BasicStroke(graphsize)); ?? ??? ??? ? g3.drawArc(x1, y1, 100, 60, 0, 180); ?? ? ? ?} ?? ??? ?//九宫格递归调用九宫格函数 ?? ? ? ?else if(btncontent.equals(九宫格递归)) { ?? ? ? ??? ?//九宫格递归实现 ?? ? ? ??? ? ?dg(0,50,600,600); ?? ? ? ? ?} ?? ??? ?System.out.println(释放); ?? ??? ? ?? ?} ?? ?Override ?? ?//鼠标进入方法 ?? ?public void mouseEntered(MouseEvent e) { ?? ??? ?System.out.println(进入); ?? ?} ?? ?Override ?? ?//鼠标离开界面方法 ?? ?public void mouseExited(MouseEvent e) { ?? ??? ?System.out.println(离开); ?? ?} ?? ?Override ?? ?public void mouseMoved(MouseEvent e) { ?? ??? ? ?? ?} ?? ?//重写鼠标移动函数 ?? ?Override ?? ?public void mouseDragged(MouseEvent e) { ?? ??? ?g.setColor(graphcolor); //获取画笔颜色 ?? ??? ?g3.setColor(graphcolor); ?? ??? ?// TODO Auto-generated method stub ?? ??? ?x2e.getX(); ?? ??? ?y2 e.getY(); ?? ??? ?//当为画笔时 ?? ??? ?if(btncontent.equals(画笔)){ ?? ??? ??? ? ?? ??? ?g.setStroke(new BasicStroke(graphsize));?? ?//获取当前画笔大小?? ??? ? ?? ??? ?//画笔实现 ?? ??? ??? ??? ?g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ?? ??? ??? ??? ?g.drawLine(x1, y1, x2, y2); ?? ??? ??? ??? ?g3.setStroke(new BasicStroke(graphsize));?? ??? ??? ? ?? ??? ??? ??? ?g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ?? ??? ??? ??? ?g3.drawLine(x1, y1, x2, y2); ?? ??? ??? ??? ?x1 x2; ?? ??? ??? ??? ?y1 y2; ?? ??? ??? ??? ?} ?? ??? ?//橡皮擦 ?? ??? ? if(btncontent.equals(橡皮)){ ?? ??? ??? ? //将画笔颜色置为背景颜色 ?? ??? ??? ? g.setColor(background); ?? ??? ??? ? g3.setColor(background); ?? ??? ??? ?g.setStroke(new BasicStroke(30));?? ?//将橡皮擦的大小置大小为30?? ??? ??? ??? ??? ??? ? ?? ??? ??? ?g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ?? ??? ??? ?g.drawLine(x1, y1, x2, y2); ?? ??? ??? ? ?? ??? ??? ?g3.setStroke(new BasicStroke(30));?? ??? ??? ??? ??? ??? ??? ? ?? ??? ??? ?g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ?? ??? ??? ?g3.drawLine(x1, y1, x2, y2); ?? ??? ??? ?x1 x2; ?? ??? ??? ?y1 y2; ?? ?? ??? ??? ?//使用过后将画笔颜色重新置为原来颜色 ?? ??? ??? ?g.setColor(graphcolor); ?? ??? ??? ?g3.setColor(graphcolor); ?? ? ? ?} ?? ??? ? //喷枪函数 ?? ??? ?? ?? ? ? ?else if(btncontent.equals(喷枪)){ ?? ??? ??? ??? ?g.setStroke(new BasicStroke(graphsize));?? ? ?//不用加粗获取画笔大小?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ?g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ?? ??? ??? ??? ?g3.setStroke(new BasicStroke(graphsize));?? ? ?//不用加粗?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ?g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ?? ??? ??? ??? ?//喷枪实现函数 ?? ??? ??? ??? ?for(int k0;k20;k){ ?? ??? ??? ??? ??? ?Random inew Random(); ? ? ?? ?? ??? ??? ??? ??? ?int ai.nextInt(10); ?? ??? ??? ??? ??? ?int bi.nextInt(20); ?? ??? ??? ??? ??? ?g.drawLine(x2a, y2b, x2a, y2b); ?? ??? ??? ??? ??? ?g3.drawLine(x2a, y2b, x2a, y2b); ?? ? ? ?} ?? ? ? ?} ?? ??? ? ?? ?} }
画板演示: 保存图片: