Note that there are some explanatory texts on larger screens.

plurals
  1. POunable to apply graphics to if statement
    text
    copied!<p>I'm writing a program that has a grid of squares that when you click on one, it changes color from black to red. I'm writing the if statement for the first square, and It's not letting me do anything with graphics. How do I 1) change the color to red, 2) draw a new square in the same place, and 3) why dosen't it like the <code>MouseHandler</code> class declaration?</p> <pre><code>import java.awt.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class toggle extends JPanel{ public void paintComponent(Graphics g){ super.paintComponents(g); g.fillRect(0,0,20,20); g.fillRect(21,0,20,20); g.fillRect(42,0,20,20); g.fillRect(63,0,20,20); g.fillRect(84,0,20,20); g.fillRect(105,0,20,20); g.fillRect(126,0,20,20); g.fillRect(147,0,20,20); g.fillRect(168,0,20,20); g.fillRect(189,0,20,20); g.fillRect(190,0,20,20); // g.fillRect(0,21,20,20); g.fillRect(21,21,20,20); g.fillRect(42,21,20,20); g.fillRect(63,21,20,20); g.fillRect(84,21,20,20); g.fillRect(105,21,20,20); g.fillRect(126,21,20,20); g.fillRect(147,21,20,20); g.fillRect(168,21,20,20); g.fillRect(189,21,20,20); g.fillRect(190,21,20,20); // g.fillRect(0,42,20,20); g.fillRect(21,42,20,20); g.fillRect(42,42,20,20); g.fillRect(63,42,20,20); g.fillRect(84,42,20,20); g.fillRect(105,42,20,20); g.fillRect(126,42,20,20); g.fillRect(147,42,20,20); g.fillRect(168,42,20,20); g.fillRect(189,42,20,20); g.fillRect(190,42,20,20); // g.fillRect(0,63,20,20); g.fillRect(21,63,20,20); g.fillRect(42,63,20,20); g.fillRect(63,63,20,20); g.fillRect(84,63,20,20); g.fillRect(105,63,20,20); g.fillRect(126,63,20,20); g.fillRect(147,63,20,20); g.fillRect(168,63,20,20); g.fillRect(189,63,20,20); g.fillRect(190,63,20,20); // g.fillRect(0,84,20,20); g.fillRect(21,84,20,20); g.fillRect(42,84,20,20); g.fillRect(63,84,20,20); g.fillRect(84,84,20,20); g.fillRect(105,84,20,20); g.fillRect(126,84,20,20); g.fillRect(147,84,20,20); g.fillRect(168,84,20,20); g.fillRect(189,84,20,20); g.fillRect(190,84,20,20); // g.fillRect(0,105,20,20); g.fillRect(21,105,20,20); g.fillRect(42,105,20,20); g.fillRect(63,105,20,20); g.fillRect(84,105,20,20); g.fillRect(105,105,20,20); g.fillRect(126,105,20,20); g.fillRect(147,105,20,20); g.fillRect(168,105,20,20); g.fillRect(189,105,20,20); g.fillRect(190,105,20,20); // g.fillRect(0,126,20,20); g.fillRect(21,126,20,20); g.fillRect(42,126,20,20); g.fillRect(63,126,20,20); g.fillRect(84,126,20,20); g.fillRect(105,126,20,20); g.fillRect(126,126,20,20); g.fillRect(147,126,20,20); g.fillRect(168,126,20,20); g.fillRect(189,126,20,20); g.fillRect(190,126,20,20); // g.fillRect(0,147,20,20); g.fillRect(21,147,20,20); g.fillRect(42,147,20,20); g.fillRect(63,147,20,20); g.fillRect(84,147,20,20); g.fillRect(105,147,20,20); g.fillRect(126,147,20,20); g.fillRect(147,147,20,20); g.fillRect(168,147,20,20); g.fillRect(189,147,20,20); g.fillRect(190,147,20,20); // g.fillRect(0,168,20,20); g.fillRect(21,168,20,20); g.fillRect(42,168,20,20); g.fillRect(63,168,20,20); g.fillRect(84,168,20,20); g.fillRect(105,168,20,20); g.fillRect(126,168,20,20); g.fillRect(147,168,20,20); g.fillRect(168,168,20,20); g.fillRect(189,168,20,20); g.fillRect(190,168,20,20); // g.fillRect(0,189,20,20); g.fillRect(21,189,20,20); g.fillRect(42,189,20,20); g.fillRect(63,189,20,20); g.fillRect(84,189,20,20); g.fillRect(105,189,20,20); g.fillRect(126,189,20,20); g.fillRect(147,189,20,20); g.fillRect(168,189,20,20); g.fillRect(189,189,20,20); g.fillRect(190,189,20,20); } public toggle(){ super(); JFrame frame=new JFrame(); frame.setTitle("toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this); frame.setSize(226, 247); frame.setVisible(true); MouseHandler hand=new MouseHandler(); addMouseListener(hand); } public class MouseHandler implements MouseListener{ public void MousePressed (MouseEvent e){ if(e.getX()&lt;20&amp;&amp;e.getX()&gt;0&amp;&amp;e.getY()&lt;20&amp;&amp;e.getY()&gt;0){ } } } public static void main (String[]args){ new toggle(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload