I am writing to BlueJ a program that processes clicks on different buttons and displays the results corresponding to these buttons to the console.
However, at startup, the code responds only to the last created button (origButton), and when you click on it, it performs all the branches of the condition at once (in this case, it prints four different lines at once.)
I've been fighting for a couple of days over this, I still can not understand anything.
Tell me, please, what am I doing wrong?
import objectdraw.*; class Button { private static Text name; private static FramedRect button; private static final int SPACE = 5; private static final int BLANK = 10; public Button (double left, double top, String label, DrawingCanvas canvas) { name = new Text(label, 0, 0, canvas); button = new FramedRect(left, top, name.getWidth()+2*SPACE, name.getHeight()+2*SPACE, canvas); name.moveTo(left+SPACE, top+SPACE); } public double getRight() { return button.getX() + button.getWidth() + BLANK; } public boolean contains(Location point) { return button.contains(point); } } public class ImageManipulator extends WindowController { private static final int LEFT = 20; private static final int TOP = 20; private static Button grayButton, mirrButton, blurButton, origButton; public void begin() { grayButton = new Button(LEFT, TOP, "Gray", canvas); mirrButton = new Button(grayButton.getRight(), TOP, "Mirror", canvas); blurButton = new Button(mirrButton.getRight(), TOP, "Blur", canvas); origButton = new Button(blurButton.getRight(), TOP, "Original", canvas); } public void onMouseClick(Location point) { if (grayButton.contains(point)) { System.out.println("gray"); } if (mirrButton.contains(point)) { System.out.println("mirr"); } if (blurButton.contains(point)) { System.out.println("blur"); } if (origButton.contains(point)) { System.out.println("orig"); } } }