ColorPicker.java
/*
* Copyright © 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.paint.views.components;
import com.nokia.example.paint.Main;
import com.nokia.example.paint.helpers.ImageLoader;
import com.nokia.example.paint.helpers.Rectangle;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class ColorPicker {
private Image palette;
private int x = 4;
private int y = 27;
private Rectangle area;
private Vector listeners;
private boolean isShown = false;
public ColorPicker(int screenWidth) {
listeners = new Vector();
palette = ImageLoader.getInstance().loadImage("/color_panel.png");
if (screenWidth > 240) {
x = (screenWidth - palette.getWidth()) / 2;
}
area = new Rectangle(x + 14, y + 17, 200, 233);
}
public void render(Graphics g) {
if (isShown) {
g.drawImage(palette, x, y, Graphics.TOP | Graphics.LEFT);
}
}
public void dragged(int x, int y) {
selectColor(x, y);
}
public void pressed(int x, int y) {
selectColor(x, y);
// hide me
Main.getInstance().getDrawArea().hideColorPicker();
}
public void addListener(ColorChangeListener listener) {
listeners.addElement(listener);
}
public void show() {
isShown = true;
}
public void hide() {
isShown = false;
}
public boolean isVisible() {
return isShown;
}
private void selectColor(int x, int y) {
if (area.isPointInside(x, y)) {
int[] argb = new int[1];
palette.getRGB(argb, 0, 1, x - this.x, y - this.y, 1, 1);
int rgb = argb[0] & 0x00FFFFFF;
int l = listeners.size();
for (int i = 0; i < l; i++) {
ColorChangeListener listener = (ColorChangeListener) listeners.elementAt(i);
listener.colorChanged(rgb);
}
}
}
}