ColorChooser.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:11k
源码类别:

J2ME

开发平台:

Java

  1. package ColorDemo;
  2. import javax.microedition.midlet.*;
  3. import javax.microedition.lcdui.*;
  4. /**
  5.  * A Color chooser.  This screen can be used to display and
  6.  * choose colors.  The current color is always available
  7.  * via the getColor and getGrayScale methods.  It can be
  8.  * set with setColor.
  9.  * A palette provides some reuse of colors, the current
  10.  * index in the palette can get set and retrieved.
  11.  * When the chooser is active the user may set the index
  12.  * in the palette, and change the red, green, and blue
  13.  * components.  
  14.  * The application using the chooser must add commands
  15.  * to the chooser as appropriate to terminate selection
  16.  * and to change to other screens.
  17.  * The chooser adapts to the available screen size
  18.  * and font sizes.
  19.  */
  20. public class ColorChooser extends Canvas {
  21.     private int width, height; // Width and height of canvas
  22.     private Font font;
  23.     private int label_w; // width of "999"
  24.     private int label_h; // height of "999"
  25.     private int gray;
  26.     private int rgbColor;
  27.     private int radix = 10; // radix to display numbers (10 or 16)
  28.     private int delta = 0x20; // default increment/decrement
  29.     private int ndx = 0; // 0 == blue, 1 == green, 2 == red
  30.     private boolean isColor;
  31.     private int numColors;
  32.     private int pndx;
  33.     private int[] palette = {0xffff00,
  34.      0x00ffff, 0xff00ff, 0xffff00,
  35.      0xc00000, 0x00c000, 0x0000c0,
  36.      0x80ffff, 0xff80ff, 0xffff80};
  37.      
  38.     
  39.     public ColorChooser(boolean isColor, int numColors) {
  40. this.isColor = isColor;
  41. this.numColors = numColors;
  42. ndx = 0;
  43. pndx = 0;
  44. setColor(palette[pndx]);
  45. width = getWidth();
  46. height = getHeight();
  47. font = Font.getDefaultFont();
  48. label_h = font.getHeight();
  49. if (label_h > (height / 6)) {
  50.     font = Font.getFont(Font.FACE_SYSTEM,
  51. Font.STYLE_PLAIN,
  52. Font.SIZE_SMALL);
  53.     label_h = font.getHeight();
  54. }
  55. label_w = font.stringWidth("999");
  56.     }
  57.     /**
  58.      * Select which entry in the Palette to use for the current color.
  59.      * @param index index into the palette; 0..10.
  60.      */
  61.     public boolean setPaletteIndex(int index) {
  62. if (index >= palette.length)
  63.     return false;
  64. pndx = index;
  65. setColor(palette[index]);
  66. return true;
  67.     }
  68.     /**
  69.      * Get the current palette index.
  70.      * @return the current index in the palette.
  71.      */
  72.     public int getPaletteIndex() {
  73. return ndx;
  74.     }
  75.     /**
  76.      * Sets the current color to the specified RGB values.
  77.      * @param red The red component of the color being set in range 0-255.
  78.      * @param green The green component of the color being set in range 0-255.
  79.      * @param blue The blue component of the color being set in range 0-255.
  80.      */
  81.     public void setColor(int red, int green, int blue) {
  82. red   = (red   < 0) ? 0 : (red   & 0xff);
  83. green = (green < 0) ? 0 : (green & 0xff);
  84. blue  = (blue  < 0) ? 0 : (blue  & 0xff);
  85. setColor((red << 16) | (green << 8) | blue);
  86.     }
  87.     /**
  88.      * Sets the current color to the specified RGB values. All subsequent
  89.      * rendering operations will use this specified color. The RGB value
  90.      * passed in is interpreted with the least significant eight bits
  91.      * giving the blue component, the next eight more significant bits
  92.      * giving the green component, and the next eight more significant
  93.      * bits giving the red component. That is to say, the color component
  94.      * is specified like 0x00RRGGBB.
  95.      * @param RGB The color being set.
  96.      */
  97.     public void setColor(int RGB) {
  98. rgbColor = RGB & 0x00ffffff;
  99. palette[pndx] = rgbColor;
  100. updateGray();
  101.     }
  102.     /*
  103.      * Compute the gray value from the RGB value.
  104.      */
  105.     private void updateGray() {
  106. /*
  107.  * REMIND: change this, if the spec allows it
  108.  *
  109.  * Gray is the value according to HSV.  I think it would
  110.  * make more sense to use NTSC gray, but that's not what
  111.  * is currently in the spec.   -- rib 20 Mar 2000
  112.  */
  113. gray = Math.max((rgbColor >> 16) & 0xff,
  114. Math.max((rgbColor >> 8) & 0xff, rgbColor & 0xff));
  115.     }
  116.     /**
  117.      * Gets the current color.
  118.      * @return an integer in form 0x00RRGGBB
  119.      * @see #setColor(int, int, int)
  120.      */
  121.     public int getColor() {
  122.         return rgbColor;
  123.     }
  124.     /** 
  125.      * Gets the red component of the current color.
  126.      * @return integer value in range 0-255
  127.      * @see #setColor(int, int, int)
  128.      */
  129.     public int getRedComponent() {
  130.         return (rgbColor >> 16) & 0xff;
  131.     }
  132.     /** 
  133.      * Gets the green component of the current color.
  134.      * @return integer value in range 0-255
  135.      * @see #setColor(int, int, int)
  136.      */
  137.     public int getGreenComponent() {
  138.         return (rgbColor >> 8) & 0xff;
  139.     }
  140.     /**
  141.      * Gets the blue component of the current color.
  142.      * @return integer value in range 0-255
  143.      * @see #setColor(int, int, int)
  144.      */
  145.     public int getBlueComponent() {
  146.         return rgbColor & 0xff;
  147.     }
  148.     /*
  149.      * Get the current grayscale value.
  150.      */
  151.     public int getGrayScale() {
  152. return gray;
  153.     }
  154.     /**
  155.      * Sets the current grayscale.
  156.      * For color the value is used to set each component.
  157.      * @param the value in range 0-255
  158.      */
  159.     public void setGrayScale(int value) {
  160.         setColor(value, value, value);
  161.     }
  162.     /**
  163.      * The canvas is being displayed.  Compute the 
  164.      * relative placement of items the depend on the screen size.
  165.      */
  166.     protected void showNotify() {
  167.     }
  168.     /*
  169.      * Paint the canvas with the current color and controls to change it.
  170.      */
  171.     protected void paint(Graphics g) {
  172. if (isColor) {
  173.     colorPaint(g);
  174. } else {
  175.     grayPaint(g);
  176. }
  177. System.out.println(g.getDisplayColor(0x00FF0000));
  178.     }
  179.     /**
  180.      * Set the radix used to display numbers.
  181.      * The default is decimal (10).
  182.      */
  183.     public void setRadix(int rad) {
  184. if (rad != 10 && rad != 16) {
  185.     throw new IllegalArgumentException();
  186. }
  187. radix = rad;
  188.     }
  189.     /**
  190.      * Get the radix used to display numbers.
  191.      */
  192.     public int getRadix() {
  193. return radix;
  194.     }
  195.     /**
  196.      * Set the delta used to increment/decrement.
  197.      * The default is 32.
  198.      */
  199.     public void setDelta(int delta) {
  200. if (delta > 0 && delta <= 128) {
  201.     this.delta = delta;
  202. }
  203.     }
  204.     /**
  205.      * Get the delta used to increment/decrement.
  206.      */
  207.     public int getDelta() {
  208. return delta;
  209.     }
  210.     /*
  211.      * Use Integer toString to convert.
  212.      * padding may be required.
  213.      */
  214.     private String format(int num) {
  215. String s = Integer.toString(num, radix);
  216. if (radix == 10 || s.length() >= 2)
  217.     return s;
  218. return "0" + 2;
  219.     }
  220.     static final int BORDER = 2;
  221.     private void colorPaint(Graphics g) {
  222. // Scale palette cells to fit 10 across
  223. int p_w = width / palette.length;
  224. int p_h = (height - (BORDER*3)) / 4;
  225. int usable_w = p_w * palette.length;
  226. int sample_w = p_w * palette.length - 1;
  227. int sample_h = p_h;
  228. int sample_x = (width - usable_w) / 2;
  229. int sample_y = 0;
  230. int p_x = sample_x;
  231. int p_y = sample_y + sample_h + 4;
  232. // Fill the background
  233. g.setColor(0xffffff);
  234. g.fillRect(0, 0, width, height);
  235. // Fill in the color sample
  236. g.setColor(rgbColor);
  237. gray = g.getGrayScale();
  238. g.fillRect(sample_x, sample_y, sample_w, sample_h);
  239. g.setColor((ndx < 0) ? 0x000000 : 0x808080);
  240. g.drawRect(sample_x, sample_y, sample_w-1, sample_h-1);
  241. // Draw the palette
  242. for (int i = 0; i < palette.length; i++) {
  243.     g.setColor(palette[i]);
  244.     int shift = ((i == pndx) ? BORDER*2 : 0);
  245.     g.fillRect(p_x + i*p_w, p_y-shift, p_w-1, p_h);
  246.     // If the palette is selected, outline it
  247.     if (i == pndx) {
  248. g.setColor((ndx < 0) ? 0x000000 : 0x808080);
  249. g.drawRect(p_x + i*p_w, p_y-shift-1, p_w-2, p_h+1);
  250.     }
  251. }
  252. int bars_x = sample_x;
  253. int bars_y = p_y + p_h + BORDER;
  254. int bar_h = label_h + BORDER;
  255. int bar_w = usable_w - label_w - BORDER;
  256. int b_x = label_w + BORDER;
  257. int b_y = bars_y + BORDER;
  258. int g_y = b_y + bar_h;
  259. int r_y = g_y + bar_h;
  260. // Draw the colorbars
  261. g.setColor(0, 0, 255);
  262. int b_w = (bar_w * getBlueComponent()) / 255;
  263. g.fillRect(b_x, b_y, b_w, bar_h - BORDER);
  264. g.setColor((ndx == 0) ? 0x000000 : 0xa0ffa0);
  265. g.drawRect(b_x, b_y, bar_w - 1, bar_h - BORDER - 1);
  266. int g_w = (bar_w * getGreenComponent()) / 255;
  267. g.setColor(0, 255, 0);
  268. g.fillRect(b_x, g_y, g_w, bar_h - BORDER);
  269. g.setColor((ndx == 1) ? 0x000000 : 0xa0ffa0);
  270. g.drawRect(b_x, g_y, bar_w - 1, bar_h - BORDER - 1);
  271. int r_w = (bar_w * getRedComponent()) / 255;
  272. g.setColor(255, 0, 0);
  273. g.fillRect(b_x, r_y, r_w, bar_h - BORDER);
  274. g.setColor((ndx == 2) ? 0x000000 : 0xffa0a0);
  275. g.drawRect(b_x, r_y, bar_w - 1, bar_h - BORDER - 1);
  276. g.setFont(font);
  277. g.setColor(0, 0, 0);
  278. g.drawString(format(getBlueComponent()), 
  279.      label_w, b_y+bar_h,  Graphics.BOTTOM|Graphics.RIGHT);
  280. g.drawString(format(getGreenComponent()),
  281.      label_w, g_y+bar_h,  Graphics.BOTTOM|Graphics.RIGHT);
  282. g.drawString(format(getRedComponent()),
  283.      label_w, r_y+bar_h,  Graphics.BOTTOM|Graphics.RIGHT);
  284.     }
  285.     private void grayPaint(Graphics g) {
  286. int sample_w = width;
  287. int sample_h = height / 2;
  288. int g_y = height / 2;
  289. int g_w = width - (label_w + BORDER);
  290. int g_h = label_h + BORDER;
  291. // Fill the background
  292. g.setGrayScale(0xff);
  293. g.fillRect(0, 0, width, height);
  294. // Fill in the gray sample
  295. g.setGrayScale(gray);
  296. g.fillRect(0, 0, sample_w, sample_h);
  297. g.setGrayScale(0);
  298. g.drawRect(0, 0, sample_w-1, sample_h-1);
  299. // Fill in the gray bar
  300. g.setGrayScale(0);
  301. g.fillRect(label_w+BORDER, g_y + BORDER, (g_w * gray) / 255, g_h);
  302. g.drawRect(label_w+BORDER, g_y + BORDER, g_w-1, g_h-1);
  303. g.drawString(format(gray), label_w,
  304.      g_y+BORDER + g_h,  Graphics.BOTTOM|Graphics.RIGHT);
  305.     }
  306.     /*
  307.      * Handle repeat as in pressed.
  308.      */
  309.     public void keyRepeated(int key) {
  310. keyPressed(key);
  311.     }
  312.     /*
  313.      * Left and Right are used to change which color bar to change
  314.      * Up and Down are used to increase/decrease the value of that bar.
  315.      */
  316.     protected void keyPressed(int key) {
  317. int action = getGameAction(key);
  318. int dir = 0;
  319. switch (action) {
  320. case RIGHT: dir += 1; break;
  321. case LEFT: dir -= 1; break;
  322. case UP: ndx -= 1; break;
  323. case DOWN: ndx += 1; break;
  324. default:
  325.     return; // nothing we recognize, exit
  326. }
  327. // Gray scale event handling is simpler than color
  328. if (isColor) {
  329.     // Limit selection to r,g,b and palette 
  330.     if (ndx < -1)
  331. ndx = -1;
  332.     if (ndx > 2)
  333. ndx = 2;
  334.     if (ndx >= 0) {
  335. int v = (rgbColor >> (ndx*8)) & 0xff;
  336. v += dir * delta;
  337. if (v < 0)
  338.     v = 0;
  339. if (v > 255)
  340.     v = 255;
  341. int mask = 0xff << (ndx*8);
  342. rgbColor = (rgbColor & ~mask) | v << (ndx*8);
  343. palette[pndx] = rgbColor;
  344.     } else {
  345. pndx += dir;
  346. if (pndx < 0)
  347.     pndx = 0;
  348. if (pndx >= palette.length)
  349.     pndx = palette.length-1;
  350. rgbColor = palette[pndx];
  351.     }
  352. } else {
  353.     /*
  354.      * Gray scale; multiple dir and add to gray
  355.      * ignore (up/down) there is only one thing to select.
  356.      */
  357.     gray += dir * delta;
  358.     if (gray < 0)
  359. gray = 0;
  360.     if (gray > 255)
  361. gray = 255;
  362. }
  363. repaint();
  364.     }
  365. }