hello everyone : I dont understand why we need to first declare the boolean as false :boolean clickControl = false;
and then true and then true again please help .... here is the code:
import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class Week1MouseEventsQ6 extends JFrame { DrawPanel canvas; public static void main(String[] args) { Week1MouseEventsQ6 w = new Week1MouseEventsQ6(); w.setVisible(true); } public Week1MouseEventsQ6() { setTitle("Week2MouseEvents: starting code"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500,220); setLocation(300,300); canvas = new DrawPanel(7, 5); add(canvas); } class DrawPanel extends JPanel implements MouseListener { int numCols; int numRows; int clickColumnIndex = -1; int clickRowIndex = -1; int buttonNumber = 0; boolean clickControl = false; public void mouseReleased(MouseEvent event) { System.out.println("Mouse Released"); System.out.println("X: " + event.getX() + "Y: " + event.getY()); } public void mousePressed(MouseEvent event) { System.out.println("Mouse Pressed"); System.out.println("X: " + event.getX() + "Y: " + event.getY()); } public void mouseClicked(MouseEvent event) { buttonNumber = event.getButton(); clickColumnIndex = (numCols) * (event.getX()) / (getWidth()); clickRowIndex = (numRows) * (event.getY()) / (getHeight()); clickControl = true; System.out.println("Mouse Clicked"); System.out.println("X: " + event.getX() + "Y: " + event.getY()); System.out.println("Row " + clickRowIndex + "Column " + clickColumnIndex); repaint(); } public void mouseEntered(MouseEvent event) { System.out.println("Mouse Entered"); System.out.println("X: " + event.getX() + "Y: " + event.getY()); } public void mouseExited(MouseEvent event) { System.out.println("Mouse Exited"); System.out.println("X: " + event.getX() + "Y: " + event.getY()); } public DrawPanel(int nc, int nr) { numCols = nc; numRows = nr; addMouseListener(this); } Rectangle getRect(int thisCol, int thisRow) { // if input is out of range, return "null" if(thisCol <0 || thisRow < 0) return null; if(thisCol>=numCols || thisRow>=numRows) return null; // otherwise, make and return the Rectangle int w = getWidth()/numCols; int h = getHeight()/numRows; int x = thisCol*w; int y = thisRow*h; Rectangle myRect = new Rectangle(x,y,w,h); return myRect; } public void paint(Graphics g) { g.setColor(Color.gray); g.fillRect(0,0,getWidth(), getHeight()); g.setColor(Color.black); Graphics2D g2 = (Graphics2D)g; // we'll use Graphics2D for it's "draw" method - // neater than the Graphics "drawRect" suppled // (which you could also use) for (int i = 0;i<numCols;i++) for(int j = 0;j<numRows;j++) g2.draw(getRect(i,j)); if (clickControl == true) { Rectangle r2 = getRect(clickColumnIndex,clickRowIndex); g.setColor(Color.red); g.fillOval(r2.x,r2.y,r2.width, r2.height); } if (buttonNumber == 3) { Rectangle r3 = getRect(clickColumnIndex,clickRowIndex); g.setColor(Color.yellow); g.fillOval(r3.x,r3.y,r3.width, r3.height); } } } }