Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 18th, 2012  |  syntax: Java  |  size: 3.67 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.geom.Area;
  6. import java.awt.geom.RoundRectangle2D;
  7. import java.awt.image.BufferedImage;
  8.  
  9. public class MagnifierPanel extends JPanel {
  10.  
  11.  
  12.     private Image screenImage;
  13.     private BufferedImage img;
  14.  
  15.     private int magnifierSize;
  16.     private int position_X;
  17.     private int position_Y;
  18.  
  19.     private Area rectangleFrame;
  20.     public Area rectanglePanel;
  21.  
  22.     private Color frameColor;
  23.     private Color panelColor;
  24.  
  25.     /* class for taking picture of the screen */
  26.     private Robot robot;
  27.  
  28.     public MagnifierPanel(int magnifierSize){
  29.  
  30.         try{
  31.             robot = new Robot();
  32.         } catch (AWTException e){}
  33.  
  34.         getScreen();
  35.     }
  36.  
  37.     public void setMagnifierPosition(int position_X, int position_Y){
  38.         this.position_X = position_X;
  39.         this.position_Y = position_Y;
  40.         updateMagnifierPicture();
  41.     }
  42.  
  43.     public void setImage(Image screenImage){
  44.         this.screenImage = screenImage;
  45.         updateMagnifierPicture();
  46.     }
  47.  
  48.     /* take picture of the current screen setup */
  49.     public void getScreen() {
  50.         screenImage = robot.createScreenCapture(new Rectangle(0,
  51.                                                               0,
  52.                                                               Toolkit.getDefaultToolkit().getScreenSize().width,
  53.                                                               Toolkit.getDefaultToolkit().getScreenSize().height)
  54.         );
  55.     }
  56.  
  57.  
  58.     /* set the size and color of the magnifier tool */
  59.     public void setMagnifierSize(int magnifierSize){
  60.         this.magnifierSize = magnifierSize;
  61.  
  62.         rectangleFrame = new Area(new Rectangle(0, 0, magnifierSize, magnifierSize));
  63.         rectanglePanel = new Area(new RoundRectangle2D.Double(10, 10, magnifierSize - 20, magnifierSize - 20, 30, 30));
  64.  
  65.         rectangleFrame.subtract(rectanglePanel);
  66.  
  67.         frameColor = new Color(8, 0, 151, 178);
  68.         panelColor = new Color(0, 248, 251, 46);
  69.  
  70.         /* set default size for this component */
  71.         setPreferredSize(new Dimension(magnifierSize,magnifierSize));
  72.  
  73.         if (getParent() != null){
  74.             getParent().repaint();
  75.         }
  76.  
  77.         updateMagnifierPicture();
  78.     }
  79.  
  80.     public void updateMagnifierPicture(){
  81.         if (getParent() != null){
  82.             getParent().repaint();
  83.         } else {
  84.             repaint();
  85.         }
  86.     }
  87.  
  88.     public void paintComponent(Graphics g){
  89.         super.paintComponent((Graphics2D)g);
  90.         drawMagnifier((Graphics2D)g);
  91.     }
  92.  
  93.     public void drawMagnifier(Graphics2D g){
  94.         g.setClip(rectangleFrame);
  95.         g.drawImage(screenImage,
  96.                 0,
  97.                 0,
  98.                 magnifierSize,
  99.                 magnifierSize,
  100.                 position_X,
  101.                 position_Y,
  102.                 position_X + magnifierSize,
  103.                 position_Y + magnifierSize,
  104.                 this);
  105.         g.setColor(frameColor);
  106.         g.fill(rectangleFrame);
  107.  
  108.         // clip the inner rectangle, so the zoom is applied only to it
  109.         g.setClip(rectanglePanel);
  110.         g.drawImage(screenImage,
  111.                 0,
  112.                 0,
  113.                 magnifierSize,
  114.                 magnifierSize,
  115.                 position_X + (magnifierSize/4),
  116.                 position_Y + (magnifierSize/4),
  117.                 position_X + (magnifierSize/4*3),    // set the magnifier zoom
  118.                 position_Y + (magnifierSize/4*3),    // set the magnifier zoom
  119.                 this);
  120.         g.setColor(panelColor);
  121.         g.fill(rectanglePanel);
  122.     }
  123. }
clone this paste RAW Paste Data