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,

Attachments
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sess24;

//**************************************************************
// ConnectTheDots.java: Applet
//
//**************************************************************
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class NewApplet5 extends Applet
{
    int dClkRes = 300;    // double-click speed in ms
    long timeMouseDown=0; // last mouse down time
    int lastX=0,lastY=0;  //  last x and y

    // user defined data
    // the following is a recording of all
    // previous clicked locations
    static final int m_nMAXLOCS = 100;
    Dimension m_dimLocs[];
    int m_nNumMouseClicks;

    // the current cursor location
    Dimension m_dimCursorLoc;

    public NewApplet5()
    {
        // initialize the object data members
        m_dimLocs = new Dimension[m_nMAXLOCS];
        m_nNumMouseClicks = 0;
        m_dimCursorLoc = new Dimension(0, 0);
    }

    public String getAppletInfo()
    {
        return "Name: ConnectTheDots\r\n" +
        "Author: Stephen R. Davis\r\n" +
        "Created for Learn Java Now (c)";
    }

    public void init()
    {
        resize(400, 400);
        //setBackground( Color.ORANGE );

    }

    public void destroy()
    {
    }


    public void paint(Graphics g)
    {
        // put a cross where the cursor is located
        int nX = m_dimCursorLoc.width;
        int nY = m_dimCursorLoc.height;
        g.drawLine(nX - 2, nY, nX + 2, nY);
        g.drawLine( nX, nY - 2, nX, nY + 2);

        // now draw a line from each clicked location
        // to every other clicked location
        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);
            }
        }
    }

    public void start()
    {
    }

    public void stop()
    {
    }

    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;
    }

    public boolean mouseUp(Event evt, int x, int y)
    {
        // ignore the mouseUp event
        return true;
    }

    public boolean mouseDrag(Event evt, int x, int y)
    {
        // ignore the mouseDrag event
        return true;
    }

    public boolean mouseMove(Event evt, int x, int y)
    {
        // record the mouse location and repaint it
        m_dimCursorLoc = new Dimension(x, y);
        repaint();
        return true;
    }
}

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,

Attachments
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sess24;

//**************************************************************
// ConnectTheDots.java: Applet
//
//**************************************************************
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class NewApplet5 extends Applet
{
    Color actual = Color.ORANGE;
    int dClkRes = 300;    // double-click speed in ms
    long timeMouseDown=0; // last mouse down time
    int lastX=0,lastY=0;  //  last x and y

    // user defined data
    // the following is a recording of all
    // previous clicked locations
    static final int m_nMAXLOCS = 100;
    Dimension m_dimLocs[];
    int m_nNumMouseClicks;

    // the current cursor location
    Dimension m_dimCursorLoc;

    public NewApplet5()
    {
        // initialize the object data members
        m_dimLocs = new Dimension[m_nMAXLOCS];
        m_nNumMouseClicks = 0;
        m_dimCursorLoc = new Dimension(0, 0);
    }

    public String getAppletInfo()
    {
        return "Name: ConnectTheDots\r\n" +
        "Author: Stephen R. Davis\r\n" +
        "Created for Learn Java Now (c)";
    }

    public void init()
    {
        resize(400, 400);
        setBackground( Color.ORANGE );
        actual = Color.ORANGE;

    }

    public void destroy()
    {
    }


    public void paint(Graphics g)
    {
        g.setColor(actual);
        
        // put a cross where the cursor is located
        int nX = m_dimCursorLoc.width;
        int nY = m_dimCursorLoc.height;
        g.drawLine(nX - 2, nY, nX + 2, nY);
        g.drawLine( nX, nY - 2, nX, nY + 2);

        // now draw a line from each clicked location
        // to every other clicked location
        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);
            }
        }
    }

    public void start()
    {
    }

    public void stop()
    {
    }

    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;
    }

    public boolean mouseUp(Event evt, int x, int y)
    {
        // ignore the mouseUp event
        return true;
    }

    public boolean mouseDrag(Event evt, int x, int y)
    {
        // ignore the mouseDrag event
        return true;
    }

    public boolean mouseMove(Event evt, int x, int y)
    {
        // record the mouse location and repaint it
        m_dimCursorLoc = new Dimension(x, y);
        repaint();
        return true;
    }
}
This question has already been answered. Start a new discussion instead.