1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class MagnifierWindow extends JFrame {
  6.  
  7.     private Container container = getContentPane();
  8.  
  9.     /* menu items */
  10.     private JPopupMenu popupMenu  = new JPopupMenu();
  11.     private JMenuItem menuRefresh = new JMenuItem("Refresh");
  12.     private JMenuItem menuHelp    = new JMenuItem("Help");
  13.     private JMenuItem menuExit    = new JMenuItem("Exit");
  14.  
  15.     private int setX;
  16.     private int setY;
  17.     private int absoluteX;
  18.     private int absoluteY;
  19.     /* on mouse press */
  20.     private int relativeX;
  21.     private int relativeY;
  22.  
  23.     Timer time;
  24.  
  25.     private boolean mousePressed;
  26.  
  27.     private int magnifierSize = 300;
  28.  
  29.     private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize);
  30.  
  31.     private int updateScreenDelay = 500;
  32.  
  33.     /* class to register mouse press */
  34.     private class MouseClicks extends MouseAdapter {
  35.         public void mousePressed(MouseEvent e){
  36.             if (e.getButton() == MouseEvent.BUTTON1){
  37.                 mousePressed = true;
  38.                 relativeX = e.getX();
  39.                 relativeY = e.getY();
  40.             }
  41.             /* check in mousePressed for cross-platform functionality */
  42.             if (e.isPopupTrigger()){
  43.                 popupMenu.show(e.getComponent(), e.getX(), e.getY());
  44.             }
  45.         }
  46.         public void mouseReleased(MouseEvent e){
  47.             mousePressed = false;
  48.             if (e.isPopupTrigger()){
  49.                 popupMenu.show(e.getComponent(), e.getX(), e.getY());
  50.             }
  51.         }
  52.     }
  53.  
  54.     /* class to register mouse motion */
  55.     private class MouseMotion extends MouseMotionAdapter {
  56.         public void mouseDragged(MouseEvent e){
  57.             if (mousePressed == true){
  58.                 absoluteX = MagnifierWindow.this.getLocationOnScreen().x + e.getX();
  59.                 absoluteY = MagnifierWindow.this.getLocationOnScreen().y + e.getY();
  60.                 setX      = absoluteX - relativeX;
  61.                 setY      = absoluteY - relativeY;
  62.                 magnifierPanel.setMagnifierPosition(setX, setY);
  63.                 /* move the magnifier to mouse location */
  64.                 setLocation(setX, setY);
  65.             }
  66.         }
  67.     }
  68.  
  69.     public MagnifierWindow(){
  70.         super();
  71.  
  72.         /* hide the JFrame */
  73.         setUndecorated(true);
  74.  
  75.         container.add(magnifierPanel);
  76.  
  77.         addMouseListener(new MouseClicks());
  78.         addMouseMotionListener(new MouseMotion());
  79.  
  80.         menuRefresh.addActionListener(
  81.                 new ActionListener() {
  82.                     @Override
  83.                     public void actionPerformed(ActionEvent e) {
  84.                         updateScreen();
  85.                     }
  86.                 }
  87.         );
  88.  
  89.         menuExit.addActionListener(
  90.                 new ActionListener() {
  91.                     @Override
  92.                     public void actionPerformed(ActionEvent e) {
  93.                         System.exit(0);
  94.                     }
  95.                 }
  96.         );
  97.         popupMenu.add(menuRefresh);
  98.         popupMenu.add(menuExit);
  99.  
  100.         time = new Timer(1000, new ActionListener() {
  101.             @Override
  102.             public void actionPerformed(ActionEvent e) {
  103. //                setVisible(false);
  104.                 magnifierPanel.getScreen();
  105. //                setVisible(true);
  106.             }
  107.         });
  108.         time.start();
  109.  
  110.         updateSize(magnifierSize);
  111.         setVisible(true);
  112.     }
  113.  
  114.     public void updateSize(int magnifierSize){
  115.         magnifierPanel.setMagnifierSize(magnifierSize);
  116.         setSize(magnifierSize, magnifierSize);
  117.         validate();
  118.     }
  119.  
  120.     public void updateScreen(){
  121.         setVisible(false);
  122.         try{
  123.             Thread.sleep(updateScreenDelay);
  124.         } catch (InterruptedException e){
  125.  
  126.         }
  127.         magnifierPanel.getScreen();
  128.         setVisible(true);
  129.     }
  130. }