I'm new to java (just pointing that out). So, I'm trying to make a project like paint from windows and I'm having trouble with the color picker(color sampler) that I'm trying to make. So I want to get the color from where my mouse position is. But I dont' know where to get that color from.
This is what I have so far for this tool:
public void colorPicker(Color currentColor){
if(!colorPick){
colorPick=true;
}
if(colorPick){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
System.out.println("COLOR PICK: "+"mouseReleased X: "+e.getX()+"mouseReleased Y: "+e.getY());
cXpos=e.getX();
cYpos=e.getY();
}
});
currentColor=???
this.currentColor=currentColor;
}
Maybe this part will help:
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawIntoBufferedImage();
g.drawImage(bImage,0,0,null);
freehandLines(g);
}
public void drawIntoBufferedImage()
{
Graphics g = bImage.getGraphics();
freehandLines(g);
g.dispose();
}
public void freehandLines(Graphics g)
{
if(points != null && points.size() > 1)
{
((Graphics2D) g).setStroke(new BasicStroke(size));
g.setColor(getCurrentColor());
for(int i = 0; i < points.size()-1;i++)
{
int x1 = points.get(i).x;
int y1 = points.get(i).y;
int x2 = points.get(i+1).x;
int y2 = points.get(i+1).y;
g.drawLine(x1, y1, x2, y2);
}
}
}
I'm guessing it has something to do with the bufferedImage. Is there a function like getColor(); but from some coordinates?