Hi,

I am trying to implement an applet.
The idea is to create the applet, then connect places where I single-click with the mouse with black lines until we give a double-click.
With a double-click I should clear the applet.

To clear the applet I am trying to set the color to white and re-draw all the lines so that I have a complete white screen.

The complete code is in attach.
Here is the specific code for the single/double clicks event:

public boolean mouseDown(Event evt, int x, int y)
    {
        // initialization
        Graphics g = getGraphics();

        // checks number of clicks
        if (m_nNumMouseClicks < m_nMAXLOCS)
        {
            // checks for double click action
            if ((lastX==x) && (lastY==y) && ((evt.when-timeMouseDown) < dClkRes))
            {
                System.out.println("double click: CLEAR SCREEN!!!");
                
                // set color a white
                g.setColor(Color.WHITE);

                // print the lines in white to hide the black lines
                for (int i = 0; i < m_nNumMouseClicks - 1; i++)
                {
                    for (int j = i + 1; j < m_nNumMouseClicks; j++)
                    {
                        g.drawLine(m_dimLocs[i].width,
                        m_dimLocs[i].height,
                        m_dimLocs[j].width,
                        m_dimLocs[j].height);
                    }
                }

                // white squre
                //g.fillRect(0, 0, 400, 400);
                repaint();

                return false;
            }

            // single click action
            else
            {
                System.out.println("simple click");
                timeMouseDown = evt.when;
                lastX=x;
                lastY=y;

                // record the click location (if there's enough room)
                m_dimLocs[m_nNumMouseClicks] = new Dimension(x, y);
                m_nNumMouseClicks++;
                // now force the window to repaint
                repaint();   
            }
        }
        return true;
    }

I appreciate your help, cheers,

Hi again,

I have managed to solve the issue by creating a new field to save the actual color as you can see in the code:

public boolean mouseDown(Event evt, int x, int y)
    {
        // initialization
        Graphics g = getGraphics();

        // checks number of clicks
        if (m_nNumMouseClicks < m_nMAXLOCS)
        {
            // checks for double click action
            if ((lastX==x) && (lastY==y) && ((evt.when-timeMouseDown) < dClkRes))
            {
                System.out.println("double click: CLEAR SCREEN!!!");
                
                // set color to RED
                actual = Color.ORANGE;
                g.setColor(actual);

                // print the lines in white to hide the black lines
                for (int i = m_nNumMouseClicks - 2; i < m_nNumMouseClicks - 1; i++)
                {
                    for (int j = i + 1; j < m_nNumMouseClicks; j++)
                    {
                        g.drawLine(m_dimLocs[i].width,
                        m_dimLocs[i].height,
                        m_dimLocs[j].width,
                        m_dimLocs[j].height);
                    }
                }

                repaint();

                return false;
            }

            // single click action
            else
            {
                System.out.println("simple click");
                timeMouseDown = evt.when;
                lastX=x;
                lastY=y;

                // record the click location (if there's enough room)
                m_dimLocs[m_nNumMouseClicks] = new Dimension(x, y);
                m_nNumMouseClicks++;

                // set color to black to draw black lines
                actual = Color.BLACK;


                // now force the window to repaint
                repaint();   
            }
        }
        return true;
    }

Cheers,

Hello,

The idea of this code is to save the clicked-mouse points and creating a line between the last 2 clicked points. Whenever a double-click the screen should be cleared.
This is all working as it should be, however, when running the program in NetBeans I am receiving this message in the debugger console:

run:
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: -2
        at sess24.NewApplet5.paint(NewApplet5.java:77)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
        at java.awt.Component.dispatchEventImpl(Component.java:4577)
        at java.awt.Container.dispatchEventImpl(Container.java:2081)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Could yo please give me a hand understanding what's wrong?
I am also attaching the code.

Cheers,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.