📄 pixelator.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: Pixelator.java,v 1.1 2003/05/16 00:16:01 zull Exp $ */package com.sun.j2me.blueprints.slugs.client;import com.sun.j2me.blueprints.slugs.shared.*;import java.util.Hashtable;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Graphics;public class Pixelator { private static String color_key; private static int[] color_value; private static Hashtable images; static { images = new Hashtable(); } /** * Set up the color table for the pixelator. color_key is a string of * color keys, a list of characters, where each character represents * one color. Keys should be unique, if you pick 'g' for green, you * should use something else for gray. color_value is an array of * the actual RGB values for the colors. The lengths of color_key and * color_value need to be equal. color_value[0] holds the RGB value * for color_key[0], and so forth. */ public static void setColors(String color_key, int[] color_value) { if (color_key.length() != color_value.length) { throw new RuntimeException("mismatched length for color keys and values"); } // be defensive, make copies of the values // that are being passed in Pixelator.color_key = new String(color_key); Pixelator.color_value = new int[color_value.length]; for (int i = 0; i < color_value.length; i++) { Pixelator.color_value[i] = color_value[i]; } } /** * Create an image based on a description in 'pattern'. Please note that * setColors() needs to be called at least once before the first call * to this method, to intialize the color table. 'pattern' is a list * of color keys (these keys need to match the ones you set with setColors()), * 'image_width' is the width of the image in pixels. The pixelator has an * internal image cache, from which it will first try to retrieve the image. * If the image is not already defined, it will be created here, and stored * in the cache for future reference. To better visualize the image you are * creating, you might want to format your source as shown below: * <p><code> * * String pattern = "" * + "..........." * + "....XXX...." * + "..XXXXXXX.." * + ".X.X.X.X.X." * + "XX.X.X.X.XX" * + ".X.X.X.X.X." * + "..XXXXXXX.." * + "....XXX...." * + "..........."; * * Pixelator.setColors(".X", new int[] {0x000000, 0x0xffff00}); * Image img = Pixelator.createImage(pattern, 11); * * <p></code> */ public static Image createImage(String pattern, int image_width) { Image img; int x, y, n, len; if (color_key == null) { throw new RuntimeException("colors not set up"); } img = (Image) images.get(pattern); if (img != null) { return img; } len = pattern.length(); img = Image.createImage(image_width, len / image_width); Graphics g = img.getGraphics(); for (y = 0; y < len / image_width; y++) { for (x = 0; x < image_width; x++) { n = color_key.indexOf(pattern.charAt(y * image_width + x)); if (n < 0) { throw new RuntimeException("illegal color"); } g.setColor(color_value[n]); g.drawLine(x, y, x, y); } } images.put(pattern, img); return img; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -