?? minicolorchooser.java
字號(hào):
package ch09.section07;
import javax.microedition.lcdui.*;
//該類(lèi)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的顏色選擇器
public class MiniColorChooser
extends Canvas {
//當(dāng)前顏色
int rgbColor;
//當(dāng)前索引
int ndx = 0;
//為每個(gè)單獨(dú)顏色創(chuàng)建一個(gè)MiniColorChooser類(lèi)實(shí)例
public MiniColorChooser() {
setColor(0xffff00);
}
//用指定RGB設(shè)置當(dāng)前顏色
public void setColor(int RGB) {
rgbColor = RGB & 0x00ffffff;
}
//獲取當(dāng)前顏色
public int getColor() {
return rgbColor;
}
//面板寬度
static final int BORDER = 2;
//面板高度
static final int BAR_H = 14;
//使用當(dāng)前顏色繪制畫(huà)板
protected void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
int sample_w = w - 1;
int sample_h = h - (BAR_H + BORDER) * 3;
int sample_y = BORDER;
int b_y = sample_y + sample_h + BORDER * 2;
int g_y = b_y + BAR_H;
int r_y = g_y + BAR_H;
//填充背景色
g.setColor(0x000000);
g.fillRect(0, 0, w, h);
//填充顏色示例
g.setColor(rgbColor);
g.fillRect(BORDER, sample_y, sample_w, sample_h);
//繪制三色彩條
int blue = (rgbColor >> 0) & 0xff;
g.setColor(0, 0, 255);
g.fillRect(20, b_y, blue / 4, 10);
int green = (rgbColor >> 8) & 0xff;
g.setColor(0, 255, 0);
g.fillRect(20, g_y, green / 4, 10);
int red = (rgbColor >> 16) & 0xff;
g.setColor(255, 0, 0);
g.fillRect(20, r_y, red / 4, 10);
g.setColor(255, 255, 255);
g.drawString(Integer.toString(blue), 18, b_y - 3,
Graphics.RIGHT | Graphics.TOP);
g.drawString(Integer.toString(green), 18, g_y - 3,
Graphics.RIGHT | Graphics.TOP);
g.drawString(Integer.toString(red), 18, r_y - 3,
Graphics.RIGHT | Graphics.TOP);
if (ndx >= 0) {
int y = b_y + ndx * BAR_H;
g.drawRect(20, y, 63, 10);
}
}
public void keyRepeated(int key) {
keyPressed(key);
}
//點(diǎn)擊左右方向鍵改變顏色取值
protected void keyPressed(int key) {
int action = getGameAction(key);
int dir = 0;
switch (action) {
case RIGHT:
dir += 1;
break;
case LEFT:
dir -= 1;
break;
case UP:
ndx -= 1;
break;
case DOWN:
ndx += 1;
break;
default:
return;
}
if (ndx < 0) {
ndx = 0;
}
if (ndx > 2) {
ndx = 2;
}
if (ndx >= 0) {
int v = (rgbColor >> (ndx * 8)) & 0xff;
v += dir * 0x20;
if (v < 0) {
v = 0;
}
if (v > 255) {
v = 255;
}
int mask = 0xff << (ndx * 8);
rgbColor = (rgbColor & ~mask) | v << (ndx * 8);
}
repaint();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -