私は縦のカラーバーを持っています、それはすべてグラデーションとして組み合わされた7つの主要な色を持っています。次に、それを次のようにペイントしJPanel
ます。
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
int w = getWidth();
int h = getHeight();
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, h);
float[] dist = {
0.02f,
0.28f,
0.42f,
0.56f,
0.70f,
0.84f,
1.0f
};
Color[] colors = {
Color.red,
Color.magenta,
Color.blue,
Color.cyan,
Color.green,
Color.yellow,
Color.red
};
LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors);
g2d.setPaint(p);
g2d.fillRect(0, 0, w, h);
}
次に、同じクラスに次のようなクリックイベントがあります。
public void mouseClick(MouseEvent evt){
BufferedImage img = (BufferedImage)this.createImage(getWidth(), getHeight());
int[] colors = new int[3];
int x = evt.getX();
int y = evt.getY();
img.getRaster().getPixel(evt.getX(), evt.getY(), colors);
ColorPickerDialog.sldColor = new Color(colors[0], colors[1], colors[2]);
getParent().invalidate();
getParent().repaint();
}
この行img.getRaster().getPixel(evt.getX(), evt.getY(), colors);
は常にRGBカラーを返します。
- 240
- 240
- 240
そして、赤、黄、緑、シアンなど、どこでもクリックでき、常にそれらのRGBカラーを取り戻します。なんで?