JAVA - Updating A JPanel Every Mouseclick

121 Views Asked by At

JPanel Not Updating as Expected

Every time the image on my application is clicked I want the panel to the (bottom) right of the interface to refresh with the RGB value for that specific locaton.

  • I know it is getting the values as as you can see in the console it has printed the values and locations of both pixel 1 and 2.

When the mouse is clicked it does this:

    image_Display.addMouseListener(new MouseListener() {
        
        @Override
        public void mouseClicked(MouseEvent e) {
               

                x1 = x2;
                y1 = y2;
                colour1 = colour2;
                
                x2 = e.getX();
                y2 = e.getY();                
                colour2 = originalpixelValue(x2, y2);
                
                //RGBValue pixelValue = originalpixelValue(x2, y2);
                System.out.println(colour2.toString());
                
                String pixel1 = x2 + ", " + y2;
                //String pixel2 = previousX + ", " + previousY;

                System.out.println("Pixel 1: " + pixel1);
                System.out.println("\n");
                Toolbar.repaint();

                repaint();
        }            
    }

Paint method:

@Override
public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(3));
        
        Random r = new Random();
        g2.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
       
        g2.drawOval(x1, y1, 10, 10);
        g2.drawOval(x2, y2, 10, 10);
}

UI

(R,G,B) and x,y pixel locations

Outcome Expected: Everything is working as it should, I am just struggling to print the RGB values to the user interface. I have tried using Repaint() and Revalidate().

Any help would be greatly appreciated

1

There are 1 best solutions below

1
On

Whatever class your imageDisplay is will have to keep track of all selected points and the colors you want to use for them (perhaps in a Map of some kind). Then in your paintComponent() method you first call super.paintComponent() and then call drawOval() for each of clicked points.