MouseListener Help Java

后端 未结 4 955
有刺的猬
有刺的猬 2020-11-29 13:39

I am trying to write a program in Java Swing that outputs a 10 x 10 grid of geometric rectangles filled with randoms colors. However, when the user clicks on one of the rect

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 14:12

    Assuming you have a 2d array of colors, you can simply use the x and y the mouselistener gives you when you click to calculate the indices of that rectangle. Just divide the x and y by the size of the rectangle using integer division. After changing the color use repaint() to show it.

提交回复
热议问题