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

📄 drawutil.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		int iBottom = size+yOffset<0 ? 0 : size+yOffset;
		
		// set colors
		int[] gradient = DrawUtil.getGradient( innerColor, outerColor, size );
		
		// walk over the text and look for non-transparent Pixels	
		for (int ix=-size+1; ix<size; ix++){
			for (int iy=-size+1; iy<size; iy++){
				//int gColor=gradient[ Math.max(Math.abs(ix),Math.abs(iy))];
				//int gColor=gradient[(Math.abs(ix)+Math.abs(iy))/2];

				// compute the color and draw all shadowPixels with offset (ix, iy)
				//#if polish.cldc1.1 
					//# int r = (int) Math.sqrt(ix*ix+iy*iy); // TODO: this might be a bit slowly
				//#elif polish.cldc1.0 
					int r = (Math.abs(ix)+Math.abs(iy))/2; // TODO: this looks a bit uncool
				//#endif
				if ( r<size) {
					int gColor = gradient[ r ];
					
					for (int col=iLeft,row; col<width/*+iLeft*/-iRight; col++) { 
						for (row=iTop;row<height-iBottom/*+iTop*/-1;row++){
							
							// draw if an opaque pixel is found and the destination is less opaque then the shadow
							if (argbData[row*(width /*+ size*2*/) + col]>>>24==0xFF 
									&& argbData[(row+yOffset+iy)*(width /* size*2*/) + col+xOffset+ix]>>>24 < gColor>>>24)
							{
								argbData[(row+yOffset+iy)*(width /*+ size*2*/) + col+xOffset+ix]=gColor;
							}
						}
					}
				}
			}
		}

	} 
	
	static int COLOR_BIT_MASK	= 0x000000FF;
	public static byte[][] FILTER_GAUSSIAN_2 = // a small and fast gaussian filtermatrix
									 {{1,2,1},
									  {2,4,2},
									  {1,2,1}};
	public static byte[][] FILTER_GAUSSIAN_3 = // a gaussian filtermatrix
	       			        {{0,1,2,1,0},
	       					 {1,3,5,3,1},
	       					 {2,5,9,5,2},
	       					 {1,3,5,3,1},
	       					 {0,1,2,1,0}};
	
	/**
	 * Performs a convolution of an image with a given matrix. 
	 * @param filterMatrix a matrix, which should have odd rows an colums (not neccessarily a square). The matrix is used for a 2-dimensional convolution. Negative values are possible.  
	 * @param brightness you can vary the brightness of the image measured in percent. Note that the algorithm tries to keep the original brightness as far as is possible.
	 * @param argbData the image (RGB+transparency)
	 * @param width of the given Image
	 * @param height of the given Image
	 * Be aware that the computation time depends on the size of the matrix.
	 */
	public final static void applyFilter(byte[][] filterMatrix, int brightness, int[] argbData, int width, int height) {
		
		// check whether the matrix is ok
		if (filterMatrix.length % 2 !=1 || filterMatrix[0].length % 2 !=1 ){
			 throw new IllegalArgumentException();
		}
		
		int fhRadius=filterMatrix.length/2+1;
		int fwRadius=filterMatrix[0].length/2+1;
		int currentPixel=0;
		int newTran, newRed, newGreen, newBlue;
		
		// compute the bightness 
		int divisor=0;
		for (int fCol, fRow=0; fRow < filterMatrix.length; fRow++){
			for (fCol=0; fCol < filterMatrix[0].length; fCol++){
				divisor+=filterMatrix[fRow][fCol];
			}
		}
		// TODO: if (divisor==0), because of negativ matrixvalues
		if (divisor==0) {
			return; // no brightness
		}
		
		// copy the neccessary imagedata into a small buffer
		int[] tmpRect=new int[width*(filterMatrix.length)];
		System.arraycopy(argbData,0, tmpRect,0, width*(filterMatrix.length));
		
		for (int fCol, fRow, col, row=fhRadius-1; row+fhRadius<height+1; row++){
			for (col=fwRadius-1; col+fwRadius<width+1; col++){
				
				// perform the convolution
				newTran=0; newRed=0; newGreen=0; newBlue=0;
				
				for (fRow=0; fRow<filterMatrix.length; fRow++){
					
					for (fCol=0; fCol<filterMatrix[0].length;fCol++){

						// take the Data from the little buffer and skale the color 
						currentPixel = tmpRect[fRow*width+col+fCol-fwRadius+1];
						if (((currentPixel >>> 24) & COLOR_BIT_MASK) != 0) {
							newTran	+= filterMatrix[fRow][fCol] * ((currentPixel >>> 24) & COLOR_BIT_MASK);
							newRed	+= filterMatrix[fRow][fCol] * ((currentPixel >>> 16) & COLOR_BIT_MASK);
							newGreen+= filterMatrix[fRow][fCol] * ((currentPixel >>> 8) & COLOR_BIT_MASK);
							newBlue	+= filterMatrix[fRow][fCol] * (currentPixel & COLOR_BIT_MASK);
						}
						
					}
				}
				
				// calculate the color	
				newTran = newTran * brightness/100/divisor;
				newRed  = newRed  * brightness/100/divisor;
				newGreen= newGreen* brightness/100/divisor;
				newBlue = newBlue * brightness/100/divisor;
			
				newTran =Math.max(0,Math.min(255,newTran));
				newRed  =Math.max(0,Math.min(255,newRed));
				newGreen=Math.max(0,Math.min(255,newGreen));
				newBlue =Math.max(0,Math.min(255,newBlue));
				argbData[(row)*width+col]=(newTran<<24 | newRed<<16 | newGreen <<8 | newBlue);
				
			}
			
			// shift the buffer if we are not near the end
			if (row+fhRadius!=height) { 
				System.arraycopy(tmpRect,width, tmpRect,0, width*(filterMatrix.length-1));	// shift it back
				System.arraycopy(argbData,width*(row+fhRadius), tmpRect,width*(filterMatrix.length-1), width);	// add new data
			}
		}
		
	}
	/**
	 * This class is used for fadeEffects (FadeTextEffect and FadinAlienGlowEffect).
	 * The you can set a start and an end color as well as some durations.
	 * 
	 * Note: stepsIn has to be the same as  stepsOut or 0!
	 * 
	 * @author Simon Schmitt
	 */
	public static class FadeUtil{
		public final int FADE_IN =1;
		public final int FADE_OUT=2;
		public final int FADE_LOOP=3;
		public final int FADE_BREAK=0;
		
		public int[] gradient;
		public boolean changed;
		
		public int startColor	=0xFF0080FF;
		public int endColor	=0xFF80FF00;
		
		public int steps;
		public int delay=0; 				// time till the effect starts
		public int stepsIn=5,stepsOut=5;  	// fading duration
		public int sWaitTimeIn=10; 		// time to stay faded in
		public int sWaitTimeOut=0; 		// time to stay faded out
		public int mode=this.FADE_LOOP;
		
		public int cColor;
		public int cStep;
		
		private void initialize(){
			//System.out.println(" init");

			this.cStep=0;
			
			switch (this.mode){
			case FADE_OUT:
				this.stepsIn=0;
				this.sWaitTimeIn=0;
				this.cColor=this.endColor;
				break;
			case FADE_IN:
				this.stepsOut=0;
				this.sWaitTimeOut=0;
				this.cColor=this.startColor;
				break;
			default://loop
				this.cColor=this.startColor;
			}

			this.cStep-=this.delay;
			
			this.steps= this.stepsIn+this.stepsOut+this.sWaitTimeIn+this.sWaitTimeOut;
			
			this.gradient = DrawUtil.getGradient(this.startColor,this.endColor,Math.max(this.stepsIn, this.stepsOut));

			
		}
		
		public boolean step(){
			this.cStep++;
			
			// (re)define everything, if something changed 
			if (this.gradient==null | this.changed) {
				initialize();
			} 
			this.changed=false;
			
			// exit, if no animation is neccessary
			if (this.mode==this.FADE_BREAK){
				return false; 
			}
			// we have to ensure that a new picture is drawn
			if (this.cStep<0){
				return true;
			}
			
			// set counter to zero (in case of a loop) or stop the engine, when we reached the end
			if (this.cStep==this.steps){
				this.cStep=0;
				
				if (this.mode!=this.FADE_LOOP) {
					this.mode=this.FADE_BREAK;
					return true;
				}
			}
			
			if (this.cStep<this.stepsIn){	
				// fade in
				this.cColor=this.gradient[this.cStep];	
				//System.out.println("  [in] color:"+this.cStep);
				return true;
				
			} else if (this.cStep<this.stepsIn+this.sWaitTimeIn){
				// have a break
				if (this.cColor!=this.endColor){
					this.cColor=this.endColor;
					return true;
				}
				
				//System.out.println("  color:end color");
				
			} else if( this.cStep<this.stepsIn+this.sWaitTimeIn+this.stepsOut){ 
				// fade out 
				this.cColor=this.gradient[this.stepsIn+this.sWaitTimeIn+this.stepsOut-this.cStep-1];
				//System.out.println("  [out] color:"+(this.stepsIn+this.sWaitTimeIn+this.stepsOut-this.cStep-1));
				return true;
				
			} else { 
				// have another break
				if (this.cColor!=this.startColor){
					this.cColor=this.startColor;
					return true;
				}
				//System.out.println("  color:start color");
			} 
			
			// it sees as if we had no change...
			return false;
		}
	}

	/**
	 * Draws a translucent line on MIDP 2.0+ and Nokia-UI-API devices.
	 * Note that on pure MIDP 1.0 devices without support for the Nokia-UI-API the translucency is ignored.
	 * 
	 * @param color the ARGB color
	 * @param x1 horizontal start position 
	 * @param y1 vertical start position
	 * @param x2 horizontal end position
	 * @param y2 vertical end position
	 * @param g the graphics context
	 */
	public static void drawLine( int color, int x1, int y1, int x2, int y2, Graphics g) {
		//#if polish.api.nokia-ui && !polish.Bugs.TransparencyNotWorkingInNokiaUiApi && !polish.Bugs.TransparencyNotWorkingInDrawPolygon
			//# int[] xPoints = new int[] { x1, x2 };
			//# int[] yPoints = new int[] { y1, y2 };
			//# DirectGraphics dg = DirectUtils.getDirectGraphics(g);
			//# dg.drawPolygon(xPoints, 0, yPoints, 0, 2, color );
		//#elifdef polish.midp2
			//# if (y2 < y1 ) {
				//# int top = y2;
				//# y2 = y1;
				//# y1 = top; 
			//# }
			//# if (x2 < x1) {
				//# int left = x2;
				//# x1 = x2;
				//# x2 = x1;
				//# x1 = left;
			//# }
//# //			int[] rgb = new int[]{ color };
//# //			if (y1 == y2) {
//# //				int start = Math.max( x1, 0);
//# //				for (int i = start; i < x2; i++ ) {
//# //					g.drawRGB(rgb, 0, 0, start + i, y1, 1, 1, true ); 
//# //				}
//# //			} else if (x1 == x2) {
//# //				int start = Math.max( y1, 0);
//# //				for (int i = start; i < y2; i++ ) {
//# //					g.drawRGB(rgb, 0, 0, x1, start + i, 1, 1, true ); 
//# //				}				
//# //			}
//# 			
			//# if (x1 == x2 || y1 == y2) {
//# //				int[] rgb = new int[]{ color };
//# //				g.drawRGB( rgb, 0, 0, x1, y1, x2 - x1, y2 - y1, true );
				//# int width = x2 - x1;
				//# if (width == 0) {
					//# width = 1;
				//# }
				//# int height = y2 - y1;
				//# if (height == 0) {
					//# height = 1;
				//# }
				//# int[] rgb = new int[ Math.max( width, height )];
				//# for (int i = 0; i < rgb.length; i++) {
					//# rgb[i] = color;
				//# }
				//# g.drawRGB( rgb, 0, 0, x1, y1, width, height, true );
			//# }
		//#else
			g.setColor( color );
			g.drawLine(x1, y1, x2, y2);
		//#endif
	}
	
}

⌨️ 快捷键说明

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