943,602 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1555
  • Java RSS
Sep 21st, 2009
0

applet

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1. public boolean mouseDown(Event evt, int x, int y)
  2. {
  3. // initialization
  4. Graphics g = getGraphics();
  5.  
  6. // checks number of clicks
  7. if (m_nNumMouseClicks < m_nMAXLOCS)
  8. {
  9. // checks for double click action
  10. if ((lastX==x) && (lastY==y) && ((evt.when-timeMouseDown) < dClkRes))
  11. {
  12. System.out.println("double click: CLEAR SCREEN!!!");
  13.  
  14. // set color a white
  15. g.setColor(Color.WHITE);
  16.  
  17. // print the lines in white to hide the black lines
  18. for (int i = 0; i < m_nNumMouseClicks - 1; i++)
  19. {
  20. for (int j = i + 1; j < m_nNumMouseClicks; j++)
  21. {
  22. g.drawLine(m_dimLocs[i].width,
  23. m_dimLocs[i].height,
  24. m_dimLocs[j].width,
  25. m_dimLocs[j].height);
  26. }
  27. }
  28.  
  29. // white squre
  30. //g.fillRect(0, 0, 400, 400);
  31. repaint();
  32.  
  33. return false;
  34. }
  35.  
  36. // single click action
  37. else
  38. {
  39. System.out.println("simple click");
  40. timeMouseDown = evt.when;
  41. lastX=x;
  42. lastY=y;
  43.  
  44. // record the click location (if there's enough room)
  45. m_dimLocs[m_nNumMouseClicks] = new Dimension(x, y);
  46. m_nNumMouseClicks++;
  47. // now force the window to repaint
  48. repaint();
  49. }
  50. }
  51. return true;
  52. }

I appreciate your help, cheers,
Attached Files
File Type: txt NewApplet5.txt (4.1 KB, 36 views)
Similar Threads
Reputation Points: 21
Solved Threads: 0
Junior Poster in Training
gampalu is offline Offline
78 posts
since Mar 2006
Sep 23rd, 2009
0

Re: applet

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:

Java Syntax (Toggle Plain Text)
  1. public boolean mouseDown(Event evt, int x, int y)
  2. {
  3. // initialization
  4. Graphics g = getGraphics();
  5.  
  6. // checks number of clicks
  7. if (m_nNumMouseClicks < m_nMAXLOCS)
  8. {
  9. // checks for double click action
  10. if ((lastX==x) && (lastY==y) && ((evt.when-timeMouseDown) < dClkRes))
  11. {
  12. System.out.println("double click: CLEAR SCREEN!!!");
  13.  
  14. // set color to RED
  15. actual = Color.ORANGE;
  16. g.setColor(actual);
  17.  
  18. // print the lines in white to hide the black lines
  19. for (int i = m_nNumMouseClicks - 2; i < m_nNumMouseClicks - 1; i++)
  20. {
  21. for (int j = i + 1; j < m_nNumMouseClicks; j++)
  22. {
  23. g.drawLine(m_dimLocs[i].width,
  24. m_dimLocs[i].height,
  25. m_dimLocs[j].width,
  26. m_dimLocs[j].height);
  27. }
  28. }
  29.  
  30. repaint();
  31.  
  32. return false;
  33. }
  34.  
  35. // single click action
  36. else
  37. {
  38. System.out.println("simple click");
  39. timeMouseDown = evt.when;
  40. lastX=x;
  41. lastY=y;
  42.  
  43. // record the click location (if there's enough room)
  44. m_dimLocs[m_nNumMouseClicks] = new Dimension(x, y);
  45. m_nNumMouseClicks++;
  46.  
  47. // set color to black to draw black lines
  48. actual = Color.BLACK;
  49.  
  50.  
  51. // now force the window to repaint
  52. repaint();
  53. }
  54. }
  55. return true;
  56. }


Cheers,
Reputation Points: 21
Solved Threads: 0
Junior Poster in Training
gampalu is offline Offline
78 posts
since Mar 2006
Sep 23rd, 2009
0

Re: applet

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,
Attached Files
File Type: java NewApplet5.java (4.3 KB, 29 views)
Reputation Points: 21
Solved Threads: 0
Junior Poster in Training
gampalu is offline Offline
78 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Windows command from within my Java program.
Next Thread in Java Forum Timeline: Java Code Errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC