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
| import java.awt.*; import javax.swing.*; import java.awt.image.*; import javax.imageio.*; import java.awt.event.*;
class InvisiblePanel extends JPanel { public void paint(Graphics g) { paintChildren(g); } }
public class BackgroundWindow extends JFrame implements MouseListener, MouseMotionListener { final int delay = 1; int x, y, w, h, oldX, oldY, mouseX = 0, mouseY = 0; long time; BufferedImage image, bgImage, bgBuffer, windowBuffer, nativeImage; Robot rob; public BackgroundWindow(String picture) { x = 0; y = 0; try{ image = ImageIO.read(getClass().getResource(picture)); } catch (Throwable e) {image = null;} w = image.getWidth(); h = image.getHeight(); setSize(w, h); addMouseListener(this); addMouseMotionListener(this); setContentPane(new InvisiblePanel()); } public void paint(Graphics windowGraphics) { if(nativeImage == null || bgImage == null) { nativeImage = getGraphicsConfiguration().createCompatibleImage(w, h); windowBuffer = getGraphicsConfiguration().createCompatibleImage(w, h); setVisible(false); try{ Thread.sleep(100); rob = new Robot(getGraphicsConfiguration().getDevice()); } catch(Exception e) {System.exit(1);} bgImage = rob.createScreenCapture(new Rectangle(x, y, w, h)); bgBuffer = rob.createScreenCapture(new Rectangle(x, y, w, h)); setVisible(true); } Graphics g = windowBuffer.getGraphics(); nativeImage.getGraphics().drawImage(bgImage, 0, 0, this); nativeImage.getGraphics().drawImage(image, 0, 0, this); g.drawImage(nativeImage, 0, 0, this); getContentPane().paint(g); windowGraphics.drawImage(windowBuffer, 0, 0, this); } public void repaint() { bgImage = null; super.repaint(); } public void setLocation(int newX, int newY) { if(bgBuffer != null) { int dx = newX - x; int dy = newY - y; bgBuffer.getGraphics().drawImage(bgImage, -dx, -dy, this); if(dx > 0) { int my = dy; if(my < 0) my = -my; Rectangle r = new Rectangle(x + w, y-my, dx, h+my*2); Image strip = rob.createScreenCapture(r); bgBuffer.getGraphics().drawImage(strip, w-dx, -dy-my, this); } else if(dx < 0) { int my = dy; if(my < 0) my = -my; Rectangle r = new Rectangle(x+dx, y-my, -dx, h+my*2); Image strip = rob.createScreenCapture(r); bgBuffer.getGraphics().drawImage(strip, 0, -dy-my, this); } if(dy > 0) { Rectangle r = new Rectangle(x, y+h, w, dy); Image strip = rob.createScreenCapture(r); bgBuffer.getGraphics().drawImage(strip, -dx, h-dy, this); } else if(dy < 0) { Rectangle r = new Rectangle(x, y+dy, w, -dy); Image strip = rob.createScreenCapture(r); bgBuffer.getGraphics().drawImage(strip, -dx, 0, this); } bgImage.getGraphics().drawImage(bgBuffer, 0, 0, this); this.x = newX; this.y = newY; paint(getGraphics()); } super.setLocation(x, y); pause(1); } public void mousePressed(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();} public void mouseReleased(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();} public void mouseClicked(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();} public void mouseEntered(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();} public void mouseExited(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();} public void mouseDragged(MouseEvent e){ long now = System.currentTimeMillis(); if(now - time < delay) return; time = now; int dx = e.getX() - mouseX; int dy = e.getY() - mouseY; setLocation(x + dx, y + dy); mouseX = e.getX() - dx; mouseY = e.getY() - dy; } public void mouseMoved(MouseEvent e){} static void pause(long time) { try{ Thread.sleep(time);} catch(Exception e){} } } |