/* ColorPicker v1.0 by Toh Lik Khoong Please contact me at lktoh@navsurf.com for any comments or suggestions The source code is strictly for educational purposes and cannot be modified or used without the express permission of the author Visit http://navsurf.com for the latest version and other useful applets */ import java.awt.*; import java.awt.event.*; import java.awt.image.MemoryImageSource; import java.awt.image.PixelGrabber; public class CanvasB extends Canvas{ static final int HEIGHT = 256; static final int WIDTH = 30; private int[] colormap = new int[WIDTH*HEIGHT]; private Image col_img = null; private Point cur_point = new Point(); CanvasB(){ super.setBackground(Color.white); super.resize(WIDTH + 10, HEIGHT); for (int x = 0; x < WIDTH; x++){ for (int y = 1; y <= HEIGHT; y++){ colormap[x + (HEIGHT - y)*WIDTH] = Color.HSBtoRGB((float) 0.0, (float) 0.0, ((float) y)/HEIGHT); } } col_img = createImage(new MemoryImageSource(WIDTH, HEIGHT, colormap, 0, WIDTH)); mouseY = HEIGHT/2; } public void refresh(Color col){ for (int x = 0; x < WIDTH; x++){ for (int y = 1; y <= HEIGHT/2; y++){ colormap[x + y * WIDTH] = 0xFF << 24 | (y*2*(col.getRed() - 255)/HEIGHT + 255) << 16 | (y*2*(col.getGreen() - 255)/HEIGHT + 255) << 8 | (y*2*(col.getBlue() - 255)/HEIGHT + 255); } } for (int x = 0; x < WIDTH; x++){ for (int y = HEIGHT/2; y < HEIGHT; y++){ colormap[x + y * WIDTH] = 0xFF << 24 | (col.getRed()*2*(HEIGHT-y)/HEIGHT) << 16 | (col.getGreen()*2*(HEIGHT-y)/HEIGHT) << 8 | (col.getBlue()*2*(HEIGHT-y)/HEIGHT); } } col_img = createImage(new MemoryImageSource(WIDTH, HEIGHT, colormap, 0, WIDTH)); repaint(); } int mouseY = 0; int prevY = 0; public void setCursor(int y){ prevY = mouseY; mouseY = y; } public Color getColorAt(int x, int y){ Color couleur = new Color(colormap[x + y*WIDTH]); return couleur; } public Color getColor(){ Color couleur = new Color(colormap[mouseY*WIDTH]); return couleur; } public boolean mouseDrag( Event evt, int x, int y ){ /*Color cur_color = cb.getColorAt(e.getX(), e.getY()); tf_r.setText(Integer.toString(cur_color.getRed())); tf_g.setText(Integer.toString(cur_color.getGreen())); tf_b.setText(Integer.toString(cur_color.getBlue())); tf_hc.setText(Integer.toHexString(cur_color.getRGB() & 0xFFFFFF).toUpperCase()); cb.setCursor(e.getY()); cb.repaint(); cc.setBackground(cur_color);*/ System.out.println(evt.y); return true; } public void paint(Graphics g){ int[] p_x = {0, 5, 0}; int[] p_y = {mouseY - 5, mouseY, mouseY + 5}; g.setColor(Color.black); g.fillPolygon(p_x, p_y, 3); p_x[0] = size().width - p_x[0]; p_x[1] = size().width - p_x[1]; p_x[2] = size().width - p_x[2]; g.fillPolygon(p_x, p_y, 3); g.drawImage(col_img, 5, 0, this); } public void update(Graphics g){ //g.clearRect(0, Math.max(0, prevY - 5), 5, Math.max(5, prevY + 5)); //g.clearRect(size().width - 5, Math.max(0, prevY - 5), size().width, Math.max(5, prevY + 5)); g.clearRect(0, 0, 5, size().height); g.clearRect(size().width - 5, 0, size().width, size().height); paint(g); } }