applet

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

applet

 
0
  #1
Sep 21st, 2009
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:

  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, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: applet

 
0
  #2
Sep 23rd, 2009
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:

  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,
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: applet

 
0
  #3
Sep 23rd, 2009
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, 0 views)
Reply With Quote Quick reply to this message  
Reply

Tags
applet, clear, draw, event, java

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for applet, clear, draw, event, java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC