Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. ArrayList< int[] > cirPos = new ArrayList<int[]>();
  2.  
  3. void setup()
  4. {
  5.  
  6. background(255);
  7. size(600,600);
  8. fill(245, 239, 66);
  9. try{
  10. readfile();
  11. refresh();
  12. }
  13. catch(Exception e)
  14. {}
  15.  
  16.  
  17. }
  18.  
  19. void draw()
  20. {
  21.  
  22. }
  23.  
  24.  
  25. void mousePressed()
  26. {
  27.  
  28. if (mouseButton == LEFT)
  29. {
  30. circle(mouseX , mouseY , 50);
  31.  
  32. int[] thisPos = {mouseX , mouseY , 50}; // (x,y,size)
  33. cirPos.add(thisPos);
  34. thread("writefile");
  35. }
  36.  
  37. else if (mouseButton == RIGHT)
  38. {
  39. double L = 0;
  40.  
  41. int[] mousePos = {mouseX,mouseY};
  42. for (int i = cirPos.size()-1;i>=0;i--)
  43. {
  44. L = sqrt(pow(cirPos.get(i)[0] - mousePos[0] , 2) + pow(cirPos.get(i)[1] - mousePos[1] , 2) );
  45. if (L <= cirPos.get(i)[2])
  46. {
  47. cirPos.remove(i);
  48. thread("writefile");
  49. refresh();
  50. }
  51. }
  52.  
  53. }
  54.  
  55.  
  56. }
  57.  
  58. void refresh()
  59. {
  60. background(255);
  61. for (int[] n : cirPos)
  62. {
  63. circle(n[0] , n[1] , n[2]);
  64. }
  65. }
  66.  
  67.  
  68. void writefile()
  69. {
  70. JSONArray values = new JSONArray();
  71.  
  72. for (int i = 0; i < cirPos.size(); i++) {
  73.  
  74. JSONObject eachCir = new JSONObject();
  75.  
  76. eachCir.setInt("x", cirPos.get(i)[0]);
  77. eachCir.setInt("y", cirPos.get(i)[1]);
  78. eachCir.setInt("size", cirPos.get(i)[2]);
  79.  
  80. values.setJSONObject(i, eachCir);
  81. }
  82.  
  83. saveJSONArray(values, "cirPos.json");
  84.  
  85. }
  86.  
  87. void readfile()
  88. {
  89.  
  90. JSONArray values = loadJSONArray("cirPos.json");
  91.  
  92. for (int i = 0; i < values.size(); i++)
  93. {
  94.  
  95. JSONObject eachPos = values.getJSONObject(i);
  96.  
  97. int x = eachPos.getInt("x");
  98. int y = eachPos.getInt("y");
  99. int size = eachPos.getInt("size");
  100.  
  101. int[] oPos = {x , y , size};
  102. cirPos.add(oPos);
  103.  
  104. }
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement