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

📄 imageeffect.java

📁 利用Java寫的智慧盤拼圖遊戲(Applet型式)。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

/* This class changes the alpha value of an image array.
 * All the pixels in the image contains Alpha, Red, Green, Blue (ARGB) colors
 * where each of the color is a value from 0 to 255.
 * If Alpha is 255 the pixel will be opaque, if its 0 the pixel is transparent.
 * 0xFFFF0000 = 11111111 11111111 00000000 00000000 - this is a red opaque pixel.
 *
 * To get the RGB values from the array we can use the AND '&' operator.
 * (11111101 & 01111110) = 01111100, only the 1:s where & = 1 will get through.
 *
 * to change 11111111 to 11111111 00000000 00000000 00000000 we can use the
 * shift left operator '<<', (00000001 << 7) = 10000000 in dec (1<<7) = 128
 *
 * To change the alpha value we loop through all the pixels in the image.
 *
 * With the blend method its also possible to mask and dontmask specific colors.
*/

public class ImageEffect{
    
    // raw is the image array.
    public static BufferedImage setTransparency(Image image, int alphaInt){
    	int imgW, imgH;
    	int[] result;
  	    BufferedImage bufImage; //
  	
  	    imgW = image.getWidth(null);
  	    imgH = image.getHeight(null);
  	    bufImage = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
  	    Graphics2D g2d = bufImage.createGraphics();
  	    g2d.drawImage(image, 0,0, null);
  	    g2d.dispose();
  	
        result = new int[imgW*imgH];
  	    bufImage.getRGB(0, 0, imgW, imgH, result, 0, imgW);
  	    int alpha = alphaInt << 24;
  	    for (int k=0; k<result.length; ++k)
  	    {
  	    	result[k] = alpha + (result[k] & 0x00FFFFFF); 
  	    }
  	
  	    bufImage.setRGB(0, 0, imgW, imgH, result, 0, imgW);
  	System.out.println("Called!");    
  	    return (bufImage);
    }
    public static BufferedImage setTransparency(Image image, double alphaDouble){
    	return setTransparency( image,  ((int)(alphaDouble*255)) & 0xFF );
    }
    public static BufferedImage setTransparencyColor(Image image, int color){
    	int imgW, imgH;
    	int[] result;
  	    BufferedImage bufImage; //
  	
  	    imgW = image.getWidth(null);
  	    imgH = image.getHeight(null);
  	    bufImage = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
  	    Graphics2D g2d = bufImage.createGraphics();
  	    g2d.drawImage(image, 0,0, null);
  	    g2d.dispose();
  	
        result = new int[imgW*imgH];
  	    bufImage.getRGB(0, 0, imgW, imgH, result, 0, imgW);
  	    int clr = color & 0x00FFFFFF;  // AARRGGBB
  	    for (int k=0; k<result.length; ++k)
  	    {
  	    	if ( ((result[k] & 0x00FFFFFF) ^ clr)==0 )  // same color
  	    	  result[k] &= 0x00FFFFFF;
  	    	else result[k] |= 0xFF000000;
  	    }
  	
  	    bufImage.setRGB(0, 0, imgW, imgH, result, 0, imgW);
  	System.out.println("Called!");    
  	    return (bufImage);
    }
    public static Image scaleImage(Image img, double scaleX, double scaleY)
	{ int imgW, imgH, sW, sH, index, imgIndex;
	  int r, g, b, x, y, dx, dy;
	  int[] gray, result;
	  BufferedImage bufImage, bufImg; //
	
	  imgW = img.getWidth(null);
	  imgH = img.getHeight(null);
	
	  sW = (int) (imgW * scaleX);
	  sH = (int) (imgH * scaleY);	  
	
	  bufImage = new BufferedImage(sW, sH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
	  Graphics g2d = bufImage.createGraphics();
      g2d.drawImage(img, 0,0,sW,sH, 0,0,imgW,imgH, null);	
	  g2d.dispose();
		  
	  return (bufImage);  
	}
    public static Image magnifyImage(Image img, double scaleX, double scaleY)
	{ int imgW, imgH, sW, sH, index, imgIndex;
	  double r, g, b, x, y, dx, dy;
	  int[] gray, result;
	  boolean isBoundary;
	  BufferedImage bufImage, resultImage; //
	
	  imgW = img.getWidth(null);
	  imgH = img.getHeight(null);
	  bufImage = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
	  Graphics2D g2d = bufImage.createGraphics();
	  g2d.drawImage(img, 0,0, null);
	
	  g2d.dispose();
	
	  sW = (int) (imgW * scaleX);
	  sH = (int) (imgH * scaleY);
	  
	  gray = new int[imgH*imgW];
	  result = new int[sH*sW];
	  bufImage.getRGB(0, 0, imgW, imgH, gray, 0, imgW);
	
	  resultImage = new BufferedImage(sW, sH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
	  	  
	  for (int row=0; row<sH; ++row)
	     { index = row * sW;
	       for (int col=0; col<sW; ++col)
	          { dx = col / scaleX;
	            dy = row / scaleY;
	          	x = (int)dx;  dx -= x;
	            y = (int)dy;  dy -= y;
	            isBoundary = false;
	            if ( y>=imgH-1 ) 
	            { //System.out.println("y="+y);
	              y = imgH-1;
	              isBoundary = true;
	            }
	            if ( x>=imgW-1 )
	            { //System.out.println("x="+x);
	              x = imgW-1;
	              isBoundary = true;
	            }
	            imgIndex = (int) (y*imgW + x);
	            if ( !isBoundary )
	            {  	r = ((gray[imgIndex       ]>>16) & 0xFF)*(1-dx)*(1-dy)+
	                	((gray[imgIndex     +1]>>16) & 0xFF)*(  dx)*(1-dy)+
	                	((gray[imgIndex+imgW  ]>>16) & 0xFF)*(1-dx)*(  dy)+
	                	((gray[imgIndex+imgW+1]>>16) & 0xFF)*(  dx)*(  dy) ;
	                g = ((gray[imgIndex       ]>> 8) & 0xFF)*(1-dx)*(1-dy)+
	                	((gray[imgIndex     +1]>> 8) & 0xFF)*(  dx)*(1-dy)+
	                	((gray[imgIndex+imgW  ]>> 8) & 0xFF)*(1-dx)*(  dy)+
	                	((gray[imgIndex+imgW+1]>> 8) & 0xFF)*(  dx)*(  dy) ; 
	                b = (gray[imgIndex       ] & 0xFF)*(1-dx)*(1-dy)+
	                    (gray[imgIndex     +1] & 0xFF)*(  dx)*(1-dy)+
	                    (gray[imgIndex+imgW  ] & 0xFF)*(1-dx)*(  dy)+
	                    (gray[imgIndex+imgW+1] & 0xFF)*(  dx)*(  dy) ; 
	                result[index] = 0xFF000000 + (((int)r)<<16) + (((int)g)<<8) + (int)b;
	            }
	            else
	            {
	            	result[index] = gray[imgIndex];
	            }
	            ++index;
	          }
	     }
	  resultImage.setRGB(0, 0, sW, sH, result, 0, sW); 
	  return (resultImage);  
	}
	// the following function added by Rich on Nov. 21, 2005.
    public static void gridImage(int[] rawInt, int[] toInt, int imgW, int imgH, int imgRow, int imgCol)
    { int blkW, blkH, toW, idx, index, rawIdx, toIdx;
    
      blkW = imgW / imgCol;
      blkH = imgH / imgRow;
      toW  = imgW + 2*imgCol;
      
      for (int row=0; row<imgRow; ++row)
         { for (int col=0; col<imgCol; ++col)
              { rawIdx = row*blkH*imgW + col*blkW;
                index  = (row*(blkH+2)+1)*toW + (col*(blkW+2) + 1) ;
              	for (int r=0; r<blkH; ++r)
                   { for (int c=0; c<blkW; ++c)
                        { toInt[index+c] = rawInt[rawIdx+c];
                        }
                     rawIdx += imgW;
                     index  +=  toW;
                   }
              }
         }
    }
    public static BufferedImage gridImage(Image image, int imgRow, int imgCol, Color color)
    { int imgW, imgH, blkW, blkH, toW, toH;
      BufferedImage bufImage, bufImg; //
    
      imgW = image.getWidth(null);
      imgH = image.getHeight(null);
      blkW = imgW / imgCol;
      blkH = imgH / imgRow;
      toW  = imgW + 2*imgCol;
      toH  = imgH + 2*imgRow;
      
      bufImage = new BufferedImage(toW, toH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
      Graphics2D g2d = (Graphics2D) bufImage.createGraphics();
      
      g2d.setColor(color);
      g2d.fillRect(0, 0, toW, toH);
      for (int row=0; row<imgRow; ++row)
      { for (int col=0; col<imgCol; ++col)
           { g2d.drawImage(image,
        		           col*(blkW+2)+1, row*(blkH+2)+1, (col+1)*(blkW+2)-1, (row+1)*(blkH+2)-1,
        		           col*blkW, row*blkH, (col+1)*blkW, (row+1)*blkH,
        		           null
                          );
           }
      }
      
      g2d.dispose();  // release g2d
       
      return (bufImage);
    }
    // more compact version
    public static BufferedImage toGray(Image image)
    { int h, w;
      BufferedImage bufImage, bufImg; //

      h = image.getHeight(null);
      w = image.getWidth(null);
      bufImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
      Graphics2D g2d = (Graphics2D) bufImage.createGraphics();
      g2d.drawImage(image, 0,0, null);
      g2d.dispose();  // release g2d
      // another new BufferedImage bufImg for showing result
      
      WritableRaster raster    = bufImage.getRaster();
      //WritableRaster rasterImg = bufImg.getRaster();

      int value, r, g, b, gray;
            
      for (int y=0; y<h; y++)
         { for (int x=0; x<w; x++)
              {  r = raster.getSample(x, y, 0);  // get the red value
                 g = raster.getSample(x, y, 1);
                 b = raster.getSample(x, y, 2);              
                 gray = (int)(r*0.299 + g*0.587 + b*0.114);
                 raster.setSample(x, y, 0, gray);  // get the red value
                 raster.setSample(x, y, 1, gray);
                 raster.setSample(x, y, 2, gray);   
              }
         }
      return (bufImage);
     
    }
    public static BufferedImage alphaButtonize(Image image, int imgRow, int imgCol, int depth)
    { int blkW, blkH;
      int imgW, imgH;
      double facLight=1.4, facDark=0.7;
      byte dark[],light[];
      int r, g, b;
      
      BufferedImage resultImage;
      
      imgW = image.getWidth(null);
      imgH = image.getHeight(null);
     
      resultImage = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
      Graphics2D g2d = (Graphics2D) resultImage.createGraphics();
      g2d.drawImage(image, 0,0, null);
      g2d.dispose();  // release g2d
      
      blkW = imgW / imgCol;
      blkH = imgH / imgRow;
            
      dark = new byte[256]; light = new byte[256]; // generate the lookup tables
      for (int k=0; k<256; ++k)
      {
    	  dark[k] = (byte)(k*facDark);
    	  g = (int)(k*facLight);
    	  if ( g>255 ) light[k]=(byte)255;
    	  else light[k]=(byte)g;
      }
      
      WritableRaster raster = resultImage.getRaster();
      int sx, sy;
      // 
      int w = blkW * imgCol;
      int h = blkH * imgRow; 
     
      for (int row=0; row<h; row+=blkH)
      { 
        for (int col=0; col<w; col+=blkW)
           { sx = col;
             sy = row;
             for (int y=0; y<depth; ++y) // top
             { 	 for (int x=y; x<blkW-y; ++x)
            	 {  r = raster.getSample(sx+x, sy+y, 0);  // get the red value
                    g = raster.getSample(sx+x, sy+y, 1);
                    b = raster.getSample(sx+x, sy+y, 2);
                    r = light[r];
                    g = light[g];
                    b = light[b];
                    raster.setSample(sx+x, sy+y, 0, r);  // get the red value
                    raster.setSample(sx+x, sy+y, 1, g);
                    raster.setSample(sx+x, sy+y, 2, b);  
            	 }
             }
             for (int x=0; x<depth; ++x) // left
             { 	 for (int y=x; y<blkH-x; ++y)
            	 {  r = raster.getSample(sx+x, sy+y, 0);  // get the red value
                    g = raster.getSample(sx+x, sy+y, 1);
                    b = raster.getSample(sx+x, sy+y, 2);
                    r = light[r];
                    g = light[g];
                    b = light[b];
                    raster.setSample(sx+x, sy+y, 0, r);  // get the red value
                    raster.setSample(sx+x, sy+y, 1, g);
                    raster.setSample(sx+x, sy+y, 2, b);
            	 }
             }
             sx += blkW-1;
             sy += blkH-1;
             for (int y=0; y<depth; ++y) // bottom
             { 	 for (int x=y; x<blkW-y; ++x)
            	 {  r = raster.getSample(sx-x, sy-y, 0);  // get the red value
                    g = raster.getSample(sx-x, sy-y, 1);
                    b = raster.getSample(sx-x, sy-y, 2);
                    r = dark[r];
                    g = dark[g];
                    b = dark[b];
                    raster.setSample(sx-x, sy-y, 0, r);  // get the red value
                    raster.setSample(sx-x, sy-y, 1, g);
                    raster.setSample(sx-x, sy-y, 2, b);            		 
            	 }
             }
             for (int x=0; x<depth; ++x) // right
             { 	 for (int y=x; y<blkH-x; ++y)
            	 {  r = raster.getSample(sx-x, sy-y, 0);  // get the red value
                    g = raster.getSample(sx-x, sy-y, 1);
                    b = raster.getSample(sx-x, sy-y, 2);
                    r = dark[r];
                    g = dark[g];
                    b = dark[b];
                    raster.setSample(sx-x, sy-y, 0, r);  // get the red value
                    raster.setSample(sx-x, sy-y, 1, g);
                    raster.setSample(sx-x, sy-y, 2, b);
               	 }
             }
          }
      }
            
      return (resultImage);   
    }
    public static BufferedImage buttonize(Image image, int imgRow, int imgCol, int depth)
    { int blkW, blkH;
      int imgW, imgH;
      
      byte dark=(byte)150, light=(byte)222;
      int r, g, b;
      
      BufferedImage resultImage;
      
      imgW = image.getWidth(null);
      imgH = image.getHeight(null);
     
      resultImage = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); // 32-bits color image
      Graphics2D g2d = (Graphics2D) resultImage.createGraphics();
      g2d.drawImage(image, 0,0, null);
      g2d.dispose();  // release g2d
      
      blkW = imgW / imgCol;
      blkH = imgH / imgRow;

⌨️ 快捷键说明

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