// ===========When a mouse is pressed======= public void mousePressed(MouseEvent e) { clickedX = e.getX(); clickedY = e.getY(); xPixelRelative = clickedX - xStartHoriz; yPixelRelative = clickedY - margin; float resultClicked; // Determinate the biggest and the smallest x value double smallestXvalue = abscissae[0]; double biggestXvalue = abscissae[0]; // Converting pixel values into expression values for (double xValue : abscissae) { if (xValue > biggestXvalue) biggestXvalue = xValue; if (xValue < smallestXvalue) smallestXvalue = xValue; } double xValueRange = biggestXvalue - smallestXvalue; double xPixelRange = 268; // =xAxisLength; double xPixelPercent = (xPixelRelative - smallestXvalue) / xPixelRange; float xValueClicked = (float) (xValueRange * xPixelPercent); try { // Determine the corresponding y based on the given function and the x resulting from // pixel-to-value conversion resultClicked = ComplexExpressionCalculator.evaluateExpression(function, String.valueOf(xValueClicked)); } catch (IllegalArgumentException iae) { JOptionPane.showMessageDialog( this, "" + iae.getMessage() + "", "Error", JOptionPane.ERROR_MESSAGE); return; } // Limit decimal points displayableX = String.format("%.02f", xValueClicked); displayableY = String.format("%.02f", resultClicked); if (e.getSource() == window) { if (clickedX >= 2 && xPixelRelative <= 270 && yPixelRelative >= 28 && yPixelRelative <= 340) { System.out.println( "The mouse clicked at pixel x=" + xPixelRelative + " based on xy reference. Originally it's at pixel x= " + e.getX()); System.out.println( "The mouse clicked at pixel y=" + yPixelRelative + " based on xy reference. Originally it's at pixel y= " + e.getY()); coordinatesLabel.setText("For x=" + displayableX + ", y=" + displayableY + " on the curve"); popUpPanel.add(coordinatesLabel, "North"); popUpWindow.getContentPane().add(popUpPanel, "Center"); popUpWindow.setSize(300, 70); popUpWindow.setLocation(100 + clickedX, 200 + yPixelRelative); popUpWindow.setTitle("Figure Coordinates"); popUpWindow.setVisible(true); } else System.out.println("Clicked outside the prescribed range."); } }