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

📄 drawutil.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//#condition polish.midp || polish.usePolishGui

/*
 * Created on Nov 23, 2005 at 2:42:24 PM.
 * 
 * Copyright (c) 2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 * 
 * The implementation for filling a polygon on devices without Nokia-UI-API and without the BlackBerry API is based
 * upon JMicroPolygon: http://sourceforge.net/projects/jmicropolygon which is licensed under the Apache Software License
 */
package de.enough.polish.util;

import javax.microedition.lcdui.Graphics;

//#if polish.api.nokia-ui
	//# import com.nokia.mid.ui.DirectGraphics;
	//# import com.nokia.mid.ui.DirectUtils;
//#endif


/**
 * <p>Provides functions for drawing shadows, polygons, gradients, etc.</p>
 *
 * <p>Copyright (c) 2005, 2006, 2007 Enough Software</p>
 * <p>
 * The implementation for filling a polygon on devices without Nokia-UI-API and without the BlackBerry API is based
 * upon JMicroPolygon: http://sourceforge.net/projects/jmicropolygon, which is licensed under the Apache Software License
 * </p>
 * <pre>
 * history
 *        Nov 23, 2005 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public final class DrawUtil {
	
	/**
	 * Draws a (translucent) filled out rectangle.
	 * Please note that this method has to create temporary arrays each time it is called, using a TranslucentSimpleBackground
	 * is probably less resource intensiv.
	 * 
	 * @param x the horizontal start position
	 * @param y the vertical start position 
	 * @param width the width of the rectangle
	 * @param height the heigh of the rectanglet 
	 * @param color the argb color of the rectangle, when there is no alpha value (color & 0xff000000 == 0), the traditional g.fillRect() method is called
	 * @param g the graphics context
	 * @see de.enough.polish.ui.backgrounds.TranslucentSimpleBackground
	 */
	public final static void fillRect( int x, int y, int width, int height, int color, Graphics g ) {
		if ((color & 0xff000000) == 0) {
			g.setColor(color);
			g.fillRect(x, y, width, height);
			return;
		}
		//#ifdef tmp.useNokiaUi
			//# DirectGraphics dg = DirectUtils.getDirectGraphics(g);
			//# int[] xCoords = new int[4];
			//# xCoords[0] = x;
			//# xCoords[1] = x + width;
			//# xCoords[2] = x + width;
			//# xCoords[3] = x;
			//# int[] yCoords = new int[4];
			//# yCoords[0] = y;
			//# yCoords[1] = y;
			//# yCoords[2] = y + height;
			//# yCoords[3] = y + height;
			//# dg.fillPolygon( xCoords, 0, yCoords, 0, 4, color );
		//#elifdef polish.midp2
			//#ifdef polish.Bugs.drawRgbOrigin
				//# x += g.getTranslateX();
				//# y += g.getTranslateY();
			//#endif
//# 				
			//# // check if the buffer needs to be created:
			//# int[] buffer;
			//#if polish.Bugs.drawRgbNeedsFullBuffer
				//# buffer = new int[ width * height ];
				//# for (int i = buffer.length - 1; i >= 0 ; i--) {
					//# buffer[i] = color;
				//# }
			//#else
				//# buffer = new int[ width ];
				//# for (int i = buffer.length - 1; i >= 0 ; i--) {
					//# buffer[i] = color;
				//# }
			//#endif
			//# if (x < 0) {
				//# width += x;
				//# x = 0;
			//# }
			//# if (width <= 0) {
				//# return;
			//# }
			//# if (y < 0) {
				//# height += y;
				//# y = 0;
			//# }
			//# if (height <= 0) {
				//# return;
			//# }
			//#if polish.Bugs.drawRgbNeedsFullBuffer
				//# g.drawRGB(buffer, 0, width, x, y, width, height, true);
			//#else
				//# g.drawRGB(buffer, 0, 0, x, y, width, height, true);
			//#endif
		//#else
			g.setColor(color);
			g.fillRect(x, y, width, height);
		//#endif
	}


	/**
	 * Draws a polygon.
	 * 
	 * @param xPoints the x coordinates of the polygon
	 * @param yPoints the y coordinates of the polygon
	 * @param color the color of the polygon
	 * @param g the graphics context
	 */
	public static void drawPolygon( int[] xPoints, int[] yPoints, int color, Graphics g )
    {
		//#if polish.blackberry && polish.usePolishGui
			//# Object o = g; // this cast is needed, otherwise the compiler will complain
			              //# // that javax.microedition.lcdui.Graphics can never be casted
			              //# // to de.enough.polish.blackberry.ui.Graphics.
			//#if polish.useDefaultPackage
				//# net.rim.device.api.ui.Graphics graphics = g.g;
				//# graphics.setColor(color);
				//# graphics.drawPathOutline( xPoints, yPoints, null, null, true);
			//#else
				//# if ( o instanceof de.enough.polish.blackberry.ui.Graphics) {
					//# net.rim.device.api.ui.Graphics graphics = ((de.enough.polish.blackberry.ui.Graphics) o).g;
					//# graphics.setColor(color);
					//# graphics.drawPathOutline( xPoints, yPoints, null, null, true);
				//# }				
			//#endif
		//#elif polish.api.nokia-ui
			//# DirectGraphics dg = DirectUtils.getDirectGraphics(g);
			//# if ((color & 0xFF000000) == 0) {
				//# color |= 0xFF000000;
			//# }
			//# dg.drawPolygon(xPoints, 0, yPoints, 0, xPoints.length, color );
        //#else
        	// use default mechanism
	        int length = xPoints.length - 1;
			g.setColor( color );
	        for(int i = 0; i < length; i++) {
	            g.drawLine(xPoints[i], yPoints[i], xPoints[i + 1], yPoints[i + 1]);
	        }
	        g.drawLine(xPoints[length], yPoints[length], xPoints[0], yPoints[0]);
        //#endif
    }

	/**
	 * Draws a filled out polygon.
	 * 
	 * @param xPoints the x coordinates of the polygon
	 * @param yPoints the y coordinates of the polygon
	 * @param color the color of the polygon
	 * @param g the graphics context
	 */
	public final static void fillPolygon( int[] xPoints, int[] yPoints, int color, Graphics g ) {
		//#if polish.blackberry && polish.usePolishGui
			//# Object o = g; // this cast is needed, otherwise the compiler will complain
			              //# // that javax.microedition.lcdui.Graphics can never be casted
			              //# // to de.enough.polish.blackberry.ui.Graphics.
			//#if polish.useDefaultPackage
				//# net.rim.device.api.ui.Graphics graphics = g.g;
				//# graphics.setColor(color);
		  		//# graphics.drawFilledPath( xPoints, yPoints, null, null);
			//#else
				//# if ( o instanceof de.enough.polish.blackberry.ui.Graphics) {
					//# net.rim.device.api.ui.Graphics graphics = ((de.enough.polish.blackberry.ui.Graphics) o).g;
					//# graphics.setColor(color);
					//# graphics.drawFilledPath( xPoints, yPoints, null, null);
				//# }				
			//#endif
		//#elif polish.api.nokia-ui
			//# DirectGraphics dg = DirectUtils.getDirectGraphics(g);
			//# if ((color & 0xFF000000) == 0) {
				//# color |= 0xFF000000;
			//# }
			//# dg.fillPolygon(xPoints, 0, yPoints, 0, xPoints.length, color );
		//#else
			// ... use default mechanism by simple triangulation of the polygon. Holes within the polygon are not supported.
			// This code is based on JMicroPolygon: http://sourceforge.net/projects/jmicropolygon
			while (xPoints.length > 2) {
				// a, b & c represents a candidate triangle to draw.
				// a is the left-most point of the polygon
				int a = indexOfLeast(xPoints);
				// b is the point after a
				int b = (a + 1) % xPoints.length;
				// c is the point before a
				int c = (a > 0) ? a - 1 : xPoints.length - 1;
				// The value leastInternalIndex holds the index of the left-most
				// polygon point found within the candidate triangle, if any.
				int leastInternalIndex = -1;
				boolean leastInternalSet = false;
				// If only 3 points in polygon, skip the tests
				if (xPoints.length > 3) {
					// Check if any of the other points are within the candidate triangle
					for (int i=0; i<xPoints.length; i++) {
						if (i != a && i != b && i != c) {
							if (withinBounds(xPoints[i], yPoints[i],
											xPoints[a], yPoints[a],
											xPoints[b], yPoints[b],
											xPoints[c], yPoints[c])) 
							{
								// Is this point the left-most point within the candidate triangle?
								if (!leastInternalSet || xPoints[i] < xPoints[leastInternalIndex]) 
								{
									leastInternalIndex = i;
									leastInternalSet = true;
								}
							}
						}
					}
				}
				// No internal points found, fill the triangle, and reservoir-dog the polygon
				if (!leastInternalSet) {
					g.setColor( color );
					//#if polish.midp2
						//# g.fillTriangle(xPoints[a], yPoints[a], xPoints[b], yPoints[b], xPoints[c], yPoints[c]);
					//#else
						fillTriangle(xPoints[a], yPoints[a], xPoints[b], yPoints[b], xPoints[c], yPoints[c], g );
					//#endif
					int[][] trimmed = trimEar(xPoints, yPoints, a);
					xPoints = trimmed[0];
					yPoints = trimmed[1];
					// Internal points found, split the polygon into two, using the line between
					// "a" (left-most point of the polygon) and leastInternalIndex (left-most
					// polygon-point within the candidate triangle) and recurse with each new polygon
				} else {
					int[][][] split = split(xPoints, yPoints, a, leastInternalIndex);
					int[][] poly1 = split[0];
					int[][] poly2 = split[1];
					fillPolygon( poly1[0], poly1[1], color, g );
					fillPolygon( poly2[0], poly2[1], color, g );
					break;
				}
			}
		//#endif
	}
	
	/**
	 * Fills the specified triangle.
	 * 
	 * @param x1 the x coordinate of the first vertex of the triangle
	 * @param y1 the y coordinate of the first vertex of the triangle
	 * @param x2 the x coordinate of the second vertex of the triangle
	 * @param y2 the y coordinate of the second vertex of the triangle
	 * @param x3 the x coordinate of the third vertex of the triangle
	 * @param y3 the y coordinate of the third vertex of the triangle
	 * @param g the graphics context
	 */
	public static void fillTriangle(int x1,
            int y1,
            int x2,
            int y2,
            int x3,
            int y3, Graphics g) 
	{
		//#if polish.midp2
			//# g.fillTriangle(x1, y1, x2, y2, x3, y3);
		//#else
			int centerX = getCenter( x1, x2, x3 );
			int centerY = getCenter( y1, y2, y3 );;
			boolean isPositionMoved;
			do {
				// drawTriangle( x1, y1, x2, y2, x3, y3, g );
				g.drawLine( x1, y1, x2, y2 );
				g.drawLine( x2, y2, x3, y3 ); 
				g.drawLine( x3, y3, x1, y1 );
				
				isPositionMoved = false;
				if (x1 < centerX) {
					x1++;
					isPositionMoved = true;
				} else if (x1 > centerX) {
					x1--;
					isPositionMoved = true;
				}
				if (x2 < centerX) {
					x2++;
					isPositionMoved = true;
				} else if (x2 > centerX) {
					x2--;
					isPositionMoved = true;
				}
				if (x3 < centerX) {
					x3++;
					isPositionMoved = true;
				} else if (x3 > centerX) {
					x3--;
					isPositionMoved = true;
				}
				if (y1 < centerY) {
					y1++;
					isPositionMoved = true;

⌨️ 快捷键说明

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