有一次在课上无意中看了东哥演示的隐形画图板,感觉比较有趣,于是头脑发热,回去就马上动手,话了 一晚上时间,编出一个比较丑陋的所谓的隐形画图板。其实说吧,这个画图板就是一个骗人的东西,它的原理就是先把当前屏幕图片截下来,然后再画到窗体上,把窗体大小设置成屏幕大小,去掉窗体上的关闭按钮,就得到了一个隐形画图板了。接下来逐步介绍怎么实现这个画图板的几个窗用的功能。
一、将窗体设置成屏幕大小,该过程使用了Java中的一个工具Toolkit 代码如下:
Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dm = toolkit.getScreenSize(); this.setSize(dm);二、去掉窗体的关闭按钮,用到了Java自带的一个窗体修饰方法setUndecorated(true),参数true表示不可修饰,得到一个空白窗体,代码很简单,就一行:
this.setUndecorated(true);
三、获取当前屏幕背景图片,并保存到工程下,该过程用到了Java自带的Robot和ImageIO方法;具体代码实现如下:
Rectangle rect = new Rectangle(dm); Robot robot = new Robot(); BufferedImageimage=robot.createScreenCapture(rect); try { ImageIO.write(image, "jpg", new File("C:\\Users\\Administrator\\workspace\\隐形画图板\\img\\" + "a" + ".jpg")); } catch (IOException ef) { // TODO Auto-generated catch block ef.printStackTrace(); } g.drawImage(image, 0 , 0, dm.width, dm.height,Color.white, null); } catch (AWTException ea) { // TODO Auto-generated catch block ea.printStackTrace(); }
//主窗体 package cjl.deaw1; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; /** * 画图板界面类 * * @author Administrator * */ public class DrawUI extends JFrame { BufferedImage image; Image image1; public static void main(String args[]) { new DrawUI(); } /** * 初始化界面的方法 */ public DrawUI() { Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dm = toolkit.getScreenSize(); JPanel panel=new JPanel(); panel.setBackground(Color.WHITE); panel.setPreferredSize(dm); this.setSize(dm); FlowLayout fl = new FlowLayout(); this.setUndecorated(true); this.setLayout(fl); this.setAlwaysOnTop(true); this.setDefaultCloseOperation(3); this.add(panel ); this.setVisible(true); // 从窗体上获取画布对象 // 获取窗体在屏幕上所占据的区域,这块区域是允许改变颜色的 Graphics g = this.getGraphics(); // int [] x={100,150,300}; // int [] y={100,200,300}; // g.drawPolygon(x, y, 3); // Graphics2D g2d = (Graphics2D) this.getGraphics(); // 创建鼠标监听器对象 DrawListener dlis = new DrawListener(g,this); // 给窗体加上鼠标监听器 this.addMouseListener(dlis); try { Rectangle rect = new Rectangle(dm); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(rect); try { ImageIO.write(image, "jpg", new File("C:\\Users\\Administrator\\workspace\\隐形画图板\\img\\" + "a" + ".jpg")); } catch (IOException ef) { // TODO Auto-generated catch block ef.printStackTrace(); } g.drawImage(image, 0 , 0, dm.width, dm.height,Color.white, null); } catch (AWTException ea) { // TODO Auto-generated catch block ea.printStackTrace(); } } }
//监听器 package cjl.deaw1; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JOptionPane; /** * 画板的监听器,实现鼠标监听器接口 * * @author Administrator * */ public class DrawListener implements MouseListener { private int x1, y1, x2, y2; private Graphics gr; public float stroke; DrawUI drawUI; public DrawListener(Graphics gr, DrawUI drawUI) { this.gr = gr; this.drawUI=drawUI; } public void mousePressed(MouseEvent e) { // 获取鼠标按下时候光标的位置 x1 = e.getX(); y1 = e.getY(); if (e.isMetaDown()) { JOptionPane.showMessageDialog(drawUI, "你你确定要退出吗?"); System.exit(0); } if (e.isControlDown()) System.out.println(); // draw(x1, y1, 100, 1); } public void mouseReleased(MouseEvent e) { // 获取鼠标释放时候光标的位置 x2 = e.getX(); y2 = e.getY(); for (int i = 0; i <= 50; i++) { gr.setColor(new Color(155 + 2 * i, 100 + 2 * i, 40 + 2 * i)); gr.fillOval(Math.min(x1, x2) + i, Math.min(y1, y2), 100 - 2 * i, 160 - 2 * i); } } public void mouseEntered(MouseEvent e) { // System.out.println("mouseEntered"); } public void mouseExited(MouseEvent e) { // System.out.println("mouseExited"); } public void mouseClicked(MouseEvent e) { // System.out.println("mouseClicked"); } public void draw(double x0, double y0, int L, int n) { double x1 = 0, y1 = 0; if (n <= 0) { return; } else { for (int i = 0; i <= 800; i++) { try { Thread.sleep(5); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (i <= 200) { L = i; gr.setColor(new Color(255 - i, i, i)); gr.fillOval((int) (x0 + L), (int) y0, 10, 10); } if (i >= 200 && i <= 400) { L = i - 200; gr.setColor(new Color(455 - i, i - 200, i - 150)); gr.fillOval((int) (x0 + 200), (int) y0 + L, 10, 10); } if (i >= 400 && i <= 600) { L = 600 - i; gr.setColor(new Color(655 - i, i - 400, i - 350)); gr.fillOval((int) x0 + L, (int) y0 + 200, 10, 10); } if (i >= 600 && i <= 800) { L = 800 - i; gr.setColor(new Color(855 - i, i - 600, i - 550)); gr.fillOval((int) x0, (int) y0 + L, 10, 10); } x1 = Math.sin((x0 + i) / 4) * 100; y1 = Math.sin((y0 + i) / 4) * 100; // System.out.println("x1:" + x1 + "\n" + "y1;" + y1); } } draw(y1 + 140, y1 + 140, L, n - 1); } }