⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bigger.java

📁 实现图片的局部放大功能
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Bigger extends Applet implements MouseMotionListener {

   Graphics g;
   Image offi;
   Image back;

   String picture_name;
   MediaTracker imageTracker;
   int boxX = 0, boxY = 0;
   int boxW = 100, boxH = 100;
   int width, height;
   
   public void init(){
   
      g = getGraphics();
      picture_name = new String();
      picture_name = getParameter("picture_name");
      if(picture_name == null)
         picture_name = "beautiful.jpg";
      imageTracker = new MediaTracker(this);
         
      back = getImage(getCodeBase(),picture_name);
      offi = createImage(300,200);
      Graphics offg = offi.getGraphics();
      offg.drawImage(back, 0, 0, this);
      addMouseMotionListener(this);
   }
   
   public void mouseDragged(MouseEvent e){
      
   }


   public void mouseMoved(MouseEvent e){
      redrawGlass(boxX, boxY, e.getX(), e.getY());
      boxX = e.getX();
      boxY = e.getY();
      if(boxX > (width - boxW / 2))
         boxX = width - boxW / 2;
      if(boxY > (height - boxH / 2))
         boxY = height - boxH / 2;
      drawGlass();

   }
   
   void drawGlass(){
   
      Graphics temp;
      temp = g.create();
      temp.clipRect(boxX,boxY,boxW,boxH);
      temp.drawImage(back, -boxX, -boxY, width * 2, height * 2, null);
      g.setColor(Color.white);
      g.drawRect(boxX, boxY, boxW - 1, boxH - 1);
      
   }
   
   void redrawGlass(int oldX, int oldY, int newX, int newY){
   
      Graphics temp;
      
      temp = g.create();
      if(newX <= oldX && newY <= oldY){
         temp.clipRect(newX, newY + boxH, boxW + oldX - newX, oldY - newY);
         temp.drawImage(offi,0,0,null);
         temp = g.create();
         temp.clipRect(newX + boxW, newY, oldX - newX, boxH + oldY - newY);
         temp.drawImage(offi,0,0,null);
      }else if(newX > oldX && newY <= oldY){
         temp.clipRect(oldX, newY + boxH, boxW + newX - oldX, oldY - newY);
         temp.drawImage(offi,0,0,null);
         temp = g.create();
         temp.clipRect(oldX, newY,newX - oldX, boxH + oldY - newY);
         temp.drawImage(offi,0,0,null);
      }else if(newX > oldX && newY > oldY){
         temp.clipRect(oldX, oldY,boxW + newX - oldX, newY - oldY);
         temp.drawImage(offi,0,0,null);
         temp = g.create();
         temp.clipRect(oldX,oldY, newX - oldX, boxH + newY - oldY);
         temp.drawImage(offi,0,0,null);
      }else{
         temp.clipRect(newX, oldY,boxW + oldX - newX, newY - oldY);
         temp.drawImage(offi,0,0,null);
         temp = g.create();
         temp.clipRect(newX + boxW, oldY, oldX - newX, boxH + newY - oldY);
         temp.drawImage(offi,0,0,null);         
      }
   }
   public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h){
      if(flags ==  ALLBITS){
         width = back.getWidth(this);
         height = back.getHeight(this);

         offi = createImage(width + boxW / 2, height + boxH / 2);

         Graphics offg = offi.getGraphics();
         offg.setColor(Color.lightGray);
         offg.fillRect(0,0,width + boxW / 2, height + boxH / 2);

         offg.drawImage(back, 0, 0,this);
         repaint();
        
         return false;
        
      }else
         return true;
      
   }
   public void paint(Graphics g){

         g.drawImage(back, 0, 0, this);
         drawGlass();

   }
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -