Soutenez-nous

ang Intertial ScrollBar avec Acceleration

Éditeur : Vincent Seres   +
Mise à jour le : 26/04/2012  ·   Licence : Gratuit pour usage non commercial  ·   Téléchargé 134 fois   ·   3 Ko Date de sortie : 24/04/2012

CommentairesLa discussion sur le forum
Présentation
Peut remplacer aisément un JScrollBar classique :

/*
*Exemple d'utilisation
*/
scrollPane.setVerticalScrollBar(new inertialScrollBar(2)); //2 représente le "poid virtuel" pour le calcul d'inertie et scrollPane est un objet de type JScrollPane

Téléchargement
Compatibilité
Linux Mac Windows  
  1. package impsa.engine2D.component;
  2.  
  3. import javax.swing.JScrollBar;
  4.  
  5.  
  6.  
  7.  
  8.  
  9. public class inertialScrollBar extends JScrollBar implements Runnable
  10. {
  11. /**
  12. *
  13. */
  14. private static final long serialVersionUID = 1L;
  15.  
  16. private static final int PAS_MIN = 6; //Pixel Increment
  17. private static final int PAS_MAX = 1000; //Pixel Increment
  18.  
  19. private Thread inertialThread = null; //Inertial Thread
  20. private boolean isInertial = false;
  21. private int sampling = 10; //Sampling in ms
  22.  
  23. private int previouPosition = 0;
  24. private double previouSpeed = 0;
  25. private long previousTime = 0;
  26.  
  27. private double kineticEnergy = 0; //KineticEnergy
  28. private double weight = 0; //Virtual knob weight
  29.  
  30.  
  31. private double pas = 1; //pas
  32. private double accelerationFactor = 1.0987; //Acceleration Factor
  33. private double deccelerationFactor = 1.1; //Acceleration Factor
  34. private long accelerationHandleTime = 300; //Acceleration handle ms
  35.  
  36.  
  37. public inertialScrollBar(double weight)
  38. {
  39. this.weight = weight;
  40.  
  41. // this.setUnitIncrement(16);
  42. this.setBlockIncrement(16);
  43.  
  44. startIntertie();
  45. }
  46.  
  47. public void stopIntertie()
  48. {
  49. isInertial = false;
  50. inertialThread.interrupt();
  51.  
  52. try {
  53. inertialThread.join();
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. public void startIntertie()
  59. {
  60. inertialThread = new Thread(this);
  61. inertialThread.setPriority(Thread.MIN_PRIORITY);
  62. isInertial = true;
  63. inertialThread.start();
  64. }
  65.  
  66. private double calculKinetic(double speed)
  67. {
  68. return (0.5 * weight * (speed*speed));
  69. }
  70.  
  71. private double calculSpeed(double kineticEnergy)
  72. {
  73. return Math.sqrt(kineticEnergy/(0.5*weight));
  74. }
  75.  
  76.  
  77. @Override
  78. public void run()
  79. {
  80. int sense = 1;
  81.  
  82. while (isInertial)
  83. {
  84. if (! this.getValueIsAdjusting())
  85. {
  86. if (this.getValue() != previouPosition) //Position changed (movement detected)
  87. {
  88. previouSpeed = (this.getValue() - previouPosition); //Get speed (difference between old and new position9
  89.  
  90. //If the sense has changed
  91. if (Math.signum(previouSpeed) != Math.signum(sense))
  92. {
  93. previouSpeed = Math.signum(previouSpeed);
  94. pas = PAS_MIN;
  95. }
  96.  
  97. //Get Sense
  98. if (previouSpeed > 0) sense = 1;
  99. else sense = -1;
  100.  
  101. //Acceleration Limit (maximum limit)
  102. if (pas <= PAS_MAX)
  103. pas = pas * accelerationFactor; //Acceleration for each new movement
  104. else
  105. pas = PAS_MAX;
  106.  
  107. previousTime = System.currentTimeMillis(); //Get actual time
  108. }
  109. else if (System.currentTimeMillis() - previousTime >= accelerationHandleTime ) //If no movement detected during "accelerationHandleTime"
  110. {
  111. //Deceleration Limit (minimum limit)
  112. if (pas >= PAS_MIN)
  113. pas = pas / deccelerationFactor; //Acceleration for each new movement
  114. else
  115. pas = PAS_MIN; //Reset Acceleration to minimum
  116. }
  117.  
  118. this.setUnitIncrement((int) (pas)); //Set the increment speed
  119.  
  120. kineticEnergy = calculKinetic(previouSpeed); //Calculate Kinetic energy
  121. kineticEnergy = kineticEnergy - 0.2*(previouSpeed*previouSpeed); //Decrease Kinetic Energy
  122. if (kineticEnergy < 0.1)
  123. kineticEnergy = 0;
  124. previouSpeed = calculSpeed(kineticEnergy); //Calculate new speed
  125.  
  126. this.setValue((int) (this.getValue() + previouSpeed*sense)); //Set new knob value
  127. }
  128.  
  129. previouPosition = this.getValue(); //Get actual knob position
  130.  
  131. try {
  132. Thread.sleep(sampling);
  133. }
  134. System.out.println(e.getMessage());
  135. }
  136. }
  137. }
  138.  
  139. public void restart()
  140. {
  141. previouPosition = 0;
  142. previouSpeed = 0;
  143. previousTime = 0;
  144.  
  145. kineticEnergy = 0;
  146. this.calculSpeed(kineticEnergy);
  147. }
  148.  
  149.  
  150. public void setWeight(double weight)
  151. {
  152. this.weight = weight;
  153. }
  154. }
  155.  
Developpez.com décline toute responsabilité quant à l'utilisation des différents éléments téléchargés.
 
 
 
 
Partenaires

PlanetHoster
Ikoula