세로색 막대가 있으며, 7 가지 주요 색상이 모두 그라디언트로 결합되어 있습니다. 나는 그을이 같은 JPanel
에 페인트 :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);
}
I는 다음과 같습니다 같은 클래스의 클릭 이벤트를 가지고 있습니다
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
그리고 빨강, 노랑, 녹색, 청록색 등의 아무 곳이나 클릭 할 수 있으며 항상 그 RGB 색상을 다시 가져옵니다. 왜?
그래디언트 위에 페인트 할 내용이 있습니까? 그렇지 않다면'BufferedImage'에 페인트하여 이미지를 라벨에 표시하십시오. 마우스 리스너를 레이블에 추가하고 이미지에서 직접 색상을 가져옵니다. –