0

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?

Zenyte
  • 11
  • 1
  • 2
  • maybe you are looking for the same needs as here : https://stackoverflow.com/questions/26565166/how-to-display-a-color-selector-when-clicking-a-button – Yassine CHABLI Dec 29 '17 at 09:43
  • @MOHAMMEDYASSINEChabli I'm not trying to get a color selector. Just when I click at a position to get the color from that position – Zenyte Dec 29 '17 at 09:46
  • Sorry , i think the link here may help you : https://stackoverflow.com/questions/13061122/getting-rgb-value-from-under-mouse-cursor – Yassine CHABLI Dec 29 '17 at 10:44

0 Answers0