小試菜刀(Swing放大鏡)

          哎呀..我那個(gè)近視啊...近視的我快連1厘米進(jìn)的電腦屏幕上的字都看不清了..
           怎么辦呢??當(dāng)然是用放大鏡啦~~~~~~  攝影機(jī) !!向我這看!!

          效果圖:
              

          開始代碼啦:

           

          package Magnifier;

          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;

          public class Magnifier extends JFrame
          {
              
          private Container container = getContentPane();

              

              private int setCoordinateX;

              
          private int setCoordinateY;

              
          private int absoluteCoordinateX;

              
          private int absoluteCoordinateY;

              
          private int relativeCoordinateXWhenMousePressed;

              
          private int relativeCoordinateYWhenMousePressed;

              
          //標(biāo)記鼠標(biāo)是否按下。如果按下則為true,否則為false
              private boolean mousePressedNow;

              
          // 放大鏡尺寸
              private int magnifierSize = 300;

              
          //放大鏡內(nèi)容面板
              private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize);

              
          //這個(gè)窗體就是放大鏡 你可以自己更改這個(gè)窗體..
              public Magnifier()
               
          {
                 setUndecorated(
          true); // 這個(gè)就是窗口的邊緣 false的話就失效果了
                 setResizable(false); 
                 container.add(magnifierPanel);
                 addMouseListener(
          new MouseFunctions());
                 addMouseMotionListener(
          new MouseMotionFunctions());
                 updateSize(magnifierSize);
                 
          this.setVisible(true);
               }


              
          public static void main(String arg[])
               
          {
              
          // JFrame 
               Magnifier magnifier = new Magnifier();
               magnifier.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               }

              
              
          public void updateSize(int magnifierSize)
               
          {
                  magnifierPanel.setMagnifierSize(magnifierSize 
          + 100);
                  setSize(magnifierSize 
          + 100, magnifierSize + 100);
                  validate();

               }


              
          private class MouseFunctions extends MouseAdapter
               
          {
              
          public void mousePressed(MouseEvent e)
               
          {
                  
          if (e.getClickCount() == 1)
                   
          {// 如果鼠標(biāo)左鍵點(diǎn)了一下,說明按住了窗體
                   mousePressedNow = true;
                   relativeCoordinateXWhenMousePressed 
          = e.getX();
                   relativeCoordinateYWhenMousePressed 
          = e.getY();
                   }

               }


              
          public void mouseReleased(MouseEvent e)
               
          {
                   mousePressedNow 
          = false;
               }

              }


              
          private class MouseMotionFunctions extends MouseMotionAdapter
               
          {
               
          public void mouseDragged(MouseEvent e)
               
          {
                
          if (mousePressedNow == true)
                
          {// 如果此時(shí)鼠標(biāo)按下了,說明在拖拽窗體
                 absoluteCoordinateX = Magnifier.this
                    .getLocationOnScreen().x
                     
          + e.getX();
                 absoluteCoordinateY 
          = Magnifier.this
                  .getLocationOnScreen().y
                  
          + e.getY();
                 setCoordinateX 
          = absoluteCoordinateX
                  
          - relativeCoordinateXWhenMousePressed;
                 setCoordinateY 
          = absoluteCoordinateY
                      
          - relativeCoordinateYWhenMousePressed;
                 magnifierPanel.setMagnifierLocation(setCoordinateX,
                      setCoordinateY);
                 setLocation(setCoordinateX, setCoordinateY);
                }

               }

               }

          }


          class MagnifierPanel extends JPanel
          {
              
          private Image screenImage;

              
          private int magnifierSize;

              
          private int locationX;

              
          private int locationY;

              
          private Robot robot;

               
          public MagnifierPanel(int magnifierSize)
               
          {
                 
          try
                    
          {
                      robot 
          = new Robot();
                    }

                 
          catch (AWTException e){
                 }

               

                 screenImage = robot.createScreenCapture(new Rectangle(00, Toolkit
                   .getDefaultToolkit().getScreenSize().width, Toolkit
                   .getDefaultToolkit().getScreenSize().height));
                
                
          this.magnifierSize = magnifierSize;
               }


              
          public void setMagnifierLocation(int locationX, int locationY)
               
          {
              
          //X坐標(biāo)
               this.locationX = locationX;
              
          //Y坐標(biāo)
               this.locationY = locationY;
               repaint();        
          // 注意重畫控件
               }


              
          public void setMagnifierSize(int magnifierSize)
               
          {
               
          this.magnifierSize = magnifierSize;
               }


              
          public void paintComponent(Graphics g)
               
          {
               
          super.paintComponent((Graphics2D) g);
              
          // 關(guān)鍵處理代碼
               g.drawImage(
                 screenImage,                 
          // 要畫的圖片
                 0,                    // 目標(biāo)矩形的第一個(gè)角的x坐標(biāo)     
                 0,                    // 目標(biāo)矩形的第一個(gè)角的y坐標(biāo)
                 magnifierSize,                 // 目標(biāo)矩形的第二個(gè)角的x坐標(biāo)
                 magnifierSize,                 // 目標(biāo)矩形的第二個(gè)角的y坐標(biāo)
                 locationX + (magnifierSize / 4),     // 源矩形的第一個(gè)角的x坐標(biāo)
                 locationY + (magnifierSize / 4),    // 源矩形的第一個(gè)角的y坐標(biāo)
                 locationX + (magnifierSize / 4 * 3),     // 源矩形的第二個(gè)角的x坐標(biāo)
                 locationY + (magnifierSize / 4 * 3),     // 源矩形的第二個(gè)角的y坐標(biāo)
                 this
               );
             }

          }


          posted on 2008-05-21 18:09 相信 閱讀(1155) 評論(4)  編輯  收藏 所屬分類: Swing文章

          評論

          # re: 小試菜刀(Swing放大鏡) 2008-05-21 19:11 lzw

          功能還不錯(cuò),就是效果比較差。  回復(fù)  更多評論   

          # re: 小試菜刀(Swing放大鏡) 2008-05-21 19:12 相信

          一起學(xué)習(xí)...我也是個(gè)Swing菜鳥  回復(fù)  更多評論   

          # re: 小試菜刀(Swing放大鏡) 2008-08-23 13:25 張相

          不錯(cuò)啊!以后要學(xué)習(xí)學(xué)習(xí)!  回復(fù)  更多評論   

          # re: 小試菜刀(Swing放大鏡) 2010-06-06 22:06 大師傅

          這個(gè)是有問題的 就是只能截取一次屏幕 我新開個(gè)界面什么的 顯示還是原來的 主要就在于Robot那里  回復(fù)  更多評論   

          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團(tuán)隊(duì)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業(yè)搞Swing的網(wǎng)站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 偃师市| 甘德县| 台安县| 天门市| 宝坻区| 潍坊市| 万全县| 尼玛县| 广灵县| 醴陵市| 敦煌市| 宁波市| 杨浦区| 治县。| 谷城县| 德阳市| 平原县| 衡阳市| 韶山市| 南昌县| 永丰县| 军事| 清水河县| 五家渠市| 昌吉市| 东方市| 定南县| 左贡县| 烟台市| 阳城县| 许昌县| 肥乡县| 万宁市| 湟中县| 商水县| 曲水县| 苏尼特左旗| 三门峡市| 甘泉县| 广灵县| 山阴县|