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

📄 util.java.svn-base

📁 利用J2ME编写的手机应用程序。 功能包括显示图片
💻 SVN-BASE
字号:
package wFramework;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Util 
{
	public static String[] splitMultiLineString(String text)
	{
		// count lines needed
		int numlines = 1;
		for (int i = 0; i < text.length(); i++)
			if (text.charAt(i) == '\n')
				numlines++;

		String output[] = new String[numlines];
		
		int l = 0;
		int s = 0;
		for (int i = 0; i < text.length(); i++)
			if (text.charAt(i) == '\n' || i == text.length() - 1)
			{
				output[l++] = text.substring(s, i);
				s = i + 1;
			}
		return output;
	}
	
	public static String insertLineshift(String text, int width, Font font)
	{
		String out = "";
		
		int s = 0;
		int g = 0;
		for (int i = 0; i < text.length(); i++)
		{
			char c = text.charAt(i);
			if (c == ' ' || c == '\t' || i == text.length() - 1)
			{
				if (font.substringWidth(text, s, i - s) < width)
				{
					g = i;
					if (i == text.length() - 1)
						out += text.substring(s, g + 1) + "\n";
				}
				else if (g > s)
				{
					out += text.substring(s, g) + "\n";
					s = g + 1;
					i = s;
				}
			}
		}
		return out;
	}
	
	public static int drawMultiLineString(Graphics g, String text, int x0, int y0, int width, int height)
	{
		String lines[] = splitMultiLineString(insertLineshift(text, width, g.getFont()));
		int y = y0;
		int h = 0;
		for (int i = 0; i < lines.length; i++)
		{
			if (lines[i] == null) continue;
			g.drawString(lines[i], x0, y, Graphics.LEFT | Graphics.TOP);
			y += g.getFont().getHeight();
			h += g.getFont().getHeight();
		}
		return h;
	}
	
	public static String amperscode(String in)
	{
		String out = "";
		for (int i = 0; i < in.length(); i++)
		{
			char c = in.charAt(i);
			if (c == '<')
				out += "&lt;";
			else if (c == '>')
				out += "&gt;";
			else if (c == '&')
				out += "&amp;";
			else
				out += String.valueOf(c);
		}
		return out;		
	}
	
	public static String urlencode(String in)
	{
		in = amperscode(in);
		String out = "";
		for (int i = 0; i < in.length(); i++)
		{
			char c = in.charAt(i);
			if ((c < 65 || c > 122) && (c > 57 || c < 48))
				out += "%" + Integer.toHexString(c);
			else
				out += String.valueOf(c);
		}
		return out;
	}
	
	private static void floodFill(int argb[], int width, int height, int x, int y, int color, int colorcheck)
	{
		if (x < 0 || y < 0 || x >= width || y >= height) return;
		int c = argb[x + (y * width)];
		if (c != colorcheck) return;		
		argb[x + (y * width)] = color;		
		floodFill(argb, width, height, x + 1, y, color, colorcheck);
		floodFill(argb, width, height, x, y + 1, color, colorcheck);
		floodFill(argb, width, height, x - 1, y, color, colorcheck);
		floodFill(argb, width, height, x, y - 1, color, colorcheck);
	}
	
	public static Image makePolygon(Polygon polygon, int bordercolor, int fillcolor)
	{
		Point min = polygon.getMin();
		Point max = polygon.getMax();
		int width = max.x - min.x + 1;
		int height = max.y - min.y + 1;
		
		int rgba[] = new int[width * height];
		
		Image img = Image.createImage(width, height);
		Graphics g = img.getGraphics();
		g.setColor(0xffffffff);
		g.fillRect(0, 0, width, height);
		g.setColor(bordercolor);
		for (int i = 0; i < polygon.getNumPoints() - 1; i++)
		{
			Point p0 = polygon.getPoint(i);
			Point p1 = polygon.getPoint(i + 1);
			g.drawLine(p0.x - min.x, p0.y - min.y, p1.x - min.x, p1.y - min.y);
		}
		img.getRGB(rgba, 0, width, 0, 0, width, height);
		floodFill(rgba, width, height, width / 2, height / 2, fillcolor, 0xffffffff);
		// replace all "greenspace" pixels with transparent ones
		for (int i = 0; i < width * height; i++)
			if (rgba[i] == 0xffffffff)
				rgba[i] = 0x00FFFFFF;

		img = Image.createRGBImage(rgba, width, height, true);
		return img;
	}
	
	public static Image addBorderGlow(Image input, int color, int border)
	{
		int w = input.getWidth() + (border * 2);
		int h = input.getHeight() + (border * 2);
		
		int outdata[] = new int[w * h];
		int indata[] = new int[input.getWidth() * input.getHeight()];
		input.getRGB(indata, 0, input.getWidth(), 0, 0, input.getWidth(), input.getHeight());
		
		int alpha[] = new int[border];
		int step = 255 / (border + 1);		
		for (int i = 0; i < border; i++)
			alpha[i] = ((step * i) << 24) | 0x00FFFFFF;

		for (int x = 0; x < w; x++)
		{
			for (int y = 0; y < h; y++)
			{
				if (x < border || x > w - (border + 1) || y < border || y > h - (border + 1))
				{
					int level = 0;
					int dx, dy;
					if (x < border || y < border)
					{
						if (x < border && y < border)
						{
							dx = border - x;
							dy = border - y;
							level = border - (int)Math.sqrt(dx * dx + dy * dy);
							//level = Math.min(x, y);
						}
						else if (x < border && y > (h - (border + 1)))
						{
							dx = border - x;
							dy = border - (h - y);
							level = border - (int)Math.sqrt(dx * dx + dy * dy);							
						}
						else if (y < border && x > (w - (border + 1)))
						{
							dx = border - (w - x);
							dy = border - y;
							level = border - (int)Math.sqrt(dx * dx + dy * dy);							
						}
						else if (x < border)
							level = Math.min(x, h - y);
						else
							level = Math.min(w - x, y);
					}
					else if (x > (w - (border + 1)) && y > (h - (border + 1)))
					{
						dx = border - (w - x);
						dy = border - (h - y);
						level = border - (int)Math.sqrt(dx * dx + dy * dy);						
					}
					else
						level = Math.min(w - x, h - y);
					
					outdata[x + (y * w)] = alpha[Math.max(Math.min(level, border - 1), 0)] & color;
				}
				else
					outdata[x + (y * w)] = 0xE0FFFFFF & indata[(x - border) + ((y - border) * input.getWidth())]; 
			}
		}
		
		return Image.createRGBImage(outdata, w, h, true);
	}

}

⌨️ 快捷键说明

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