【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“Color_Chooser_Canvas.java”一文,供大家参考学习
package ch06;
import javax.microedition.lcdui.*;
//该类实现一个简单的颜色选择器
public class Color_Chooser_Canvas
extends Canvas {
//声明一个代表当前合成颜色的int型属性变量
int rgbColor;
//声明一个代表当前操作三原色彩条的索引int型属性变量
int ndx = 0;
//声明一个代表边框线条宽度的int型属性变量
static final int BORDER = 2;
//声明一个代表RGB三原色彩条高度的int型属性变量
static final int BAR_H = 14;
//初始化绘制上下文使用的颜色
public Color_Chooser_Canvas() {
setColor(0xffff00);
}
//用指定RGB设置当前颜色
public void setColor(int RGB) {
rgbColor = RGB;
}
//获取当前绘制上下文使用的颜色
public int getColor() {
return rgbColor;
}
//绘制画板
protected void paint(Graphics g) {
//获取屏幕宽度和高度
int w = getWidth();
int h = getHeight();
//设置合成颜色面板矩形框的宽度、高度和y坐标
int sample_w = w - 1;
int sample_h = h - (BAR_H + BORDER) * 3;
int sample_y = BORDER;
//设置RGB三原色彩条的y坐标
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);
/*
3.绘制RGB三原色彩条
*/
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);
/*
4.绘制RGB三原色的当前取值
*/
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);
/*
5.绘制白色边框
*/
if (ndx >= 0) {
int y = b_y + ndx * BAR_H;
g.drawRect(20, y, 63, 10);
}
}
//响应连续按键事件
public void keyRepeated(int key) {
keyPressed(key);
}
/*
6.响应按键事件
*/
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();
}
}
上一篇:
ColorTest.java
下一篇:
恋沫