0

I have to make a 4x4 grid in a Java Applet, when you click in a box it should create a circle. If you click in a box with a circle, it should be removed.

I'm having a problem with my Mouse Listener and scope.

How do you toggle creating or removing a circle in the box clicked upon?

public class Grid extends Applet {

boolean swap;

public void init()
{
    swap = false;
    addMouseListener(new MyMouseListener());
}

public void paint(Graphics g)
{
    super.paint(g);

    g.drawRect(100, 100, 400, 400);
    //each box
    g.drawRect(100, 100, 100, 100);
    g.drawRect(200, 100, 100, 100);
    g.drawRect(300, 100, 100, 100);
    g.drawRect(400, 100, 100, 100);
    //2y
    g.drawRect(100, 200, 100, 100);
    g.drawRect(200, 200, 100, 100);
    g.drawRect(300, 200, 100, 100);
    g.drawRect(400, 200, 100, 100);
    //3y1x
    g.drawRect(100, 300, 100, 100);
    g.drawRect(200, 300, 100, 100);
    g.drawRect(300, 300, 100, 100);
    g.drawRect(400, 300, 100, 100);
    //4y1x
    g.drawRect(100, 400, 100, 100);
    g.drawRect(200, 400, 100, 100);
    g.drawRect(300, 400, 100, 100);
    g.drawRect(400, 400, 100, 100);

}
private class MyMouseListener implements MouseListener
{

    public void mouseClicked(MouseEvent e)
    {
         int nowx = e.getX();
         int nowy = e.getY();

         nowx = nowx / 100;
         String stringx = Integer.toString(nowx);
         stringx = stringx+"00";
         int currentx = Integer.parseInt(stringx);

         nowy = nowy /100;
         String stringy = Integer.toString(nowy);
         stringy = stringy+"00";
         int currenty = Integer.parseInt(stringy);

         FillCircle(null, currentx, currenty);           
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub          
    }       
}
public void FillCircle(Graphics g,int currenty, int currentx)
{
    g.fillOval(currentx, currenty, 100, 100);
}
}
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
th30d0rab1e
  • 119
  • 1
  • 11
  • A [big grid example](http://stackoverflow.com/questions/15870608/creating-a-draw-rectangle-filled-with-black-color-function-in-java-for-a-grid/15870637#15870637), a [smaller grid example](http://stackoverflow.com/questions/15421708/how-to-draw-grid-using-swing-class-java-and-detect-mouse-position-when-click-and/15422801#15422801), [another grid](http://stackoverflow.com/questions/34036216/drawing-java-grid-using-swing/34036397#34036397) – MadProgrammer Jan 28 '16 at 03:10
  • Oh [another big grid](http://stackoverflow.com/questions/14742069/java-how-to-paint-rectangles-on-mouseclick/14742199#14742199) – MadProgrammer Jan 28 '16 at 03:14
  • I don't see much information for Action Listeners in those links. – th30d0rab1e Jan 28 '16 at 07:58
  • I did see any mention of `ActionListener` in your question. Most of the examples have some form of `MouseListener` implement to allow for selecting a cell, which means it's capable of identifying where the user clicked – MadProgrammer Jan 28 '16 at 08:01
  • Oops my bad. Could I use (Graphic g) in my MouseListener? I tried calling a method in it, but I got a bunch of errors on mouseclick. I need the values in the MouseListener to create the circle. – th30d0rab1e Jan 28 '16 at 08:05
  • Essentially, no, or more to the point, I wouldn't. Instead, your `MouseListener` should update the state of some kind of data model, which would be used by the view to render the state in some meaningful manner – MadProgrammer Jan 28 '16 at 08:08
  • 1
    Ah, [this](http://stackoverflow.com/questions/34078603/how-to-make-a-rectangle-with-mouse-listeners-and-add-them-to-jframe/34078815#34078815) was the example I've been trying to find :P – MadProgrammer Jan 28 '16 at 08:11
  • Your links are resourceful but complicated to me, I suppose that still makes me a novice. Alas, more research :D – th30d0rab1e Jan 28 '16 at 08:11
  • The last one is generally more closer to what you seem to be trying to achieve, it at least shows a single grid and how you would determine which cell was clicked :P – MadProgrammer Jan 28 '16 at 08:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101849/discussion-between-th30d0rab1e-and-madprogrammer). – th30d0rab1e Jan 28 '16 at 08:14

0 Answers0