CertainTrustCanvas.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * CertainTrust SDK
  3. *
  4. * Implements the computational trust model "CertainTrust"
  5. * in Java.
  6. * See <http://www.tk.informatik.tu-darmstadt.de/de/research/smart-security-and-trust/> for further details.
  7. *
  8. *
  9. * Telecooperation Department, Technische Universit�t Darmstadt
  10. * <http://www.tk.informatik.tu-darmstadt.de/>
  11. *
  12. * Prof. Dr. Max Mühlhäuser <max@informatik.tu-darmstadt.de>
  13. * Florian Volk <florian.volk@cased.de>
  14. *
  15. *
  16. * @author David Kalnischkies
  17. * @version 1.0
  18. */
  19. /* This Source Code Form is subject to the terms of the Mozilla Public
  20. * License, v. 2.0. If a copy of the MPL was not distributed with this
  21. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  22. package CertainTrust;
  23. import java.awt.Canvas;
  24. import java.awt.Color;
  25. import java.awt.Dimension;
  26. import java.awt.Graphics;
  27. import java.awt.Image;
  28. import java.awt.event.MouseAdapter;
  29. import java.awt.event.MouseEvent;
  30. import java.awt.event.MouseMotionAdapter;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import java.util.Observable;
  34. import java.util.Observer;
  35. /**
  36. * The canvas is a lovely image trying to visualize
  37. * certainty and trust in an easy understandable way.
  38. *
  39. * @author David Kalnischkies <kalnischkies@gmail.com>
  40. */
  41. public class CertainTrustCanvas extends Canvas implements Observer {
  42. private static final long serialVersionUID = -8020598465155085223L;
  43. private static final int CROSSHAIRLENGTH = 5;
  44. private CertainTrust cl;
  45. private Map<String,String> config;
  46. private Image buffer;
  47. private double old_f = -1;
  48. /**
  49. * Constructs a new Canvas to display the certainlogic element
  50. * @param certainlogic element to display
  51. */
  52. public CertainTrustCanvas(CertainTrust certainlogic) {
  53. setup(certainlogic, null);
  54. }
  55. /**
  56. * Constructs a new Canvas to display the certainlogic element
  57. *
  58. * Supported config options are:
  59. * - readonly.mouse: disables all mouse actions if set to "true"
  60. * - canvas.height and canvas.width to define the dimensions
  61. *
  62. * @param certainlogic element to display
  63. * @param config to use for this HTI
  64. */
  65. public CertainTrustCanvas(CertainTrust certainlogic, Map<String,String> config) {
  66. setup(certainlogic, config);
  67. }
  68. private void setup(CertainTrust certainlogic, Map<String,String> config) {
  69. if (config != null)
  70. this.config = config;
  71. else
  72. this.config = new HashMap<String,String>();
  73. // set the requested dimensions
  74. if (this.config.containsKey("canvas.width") == false)
  75. this.config.put("canvas.width", "120");
  76. if (this.config.containsKey("canvas.height") == false)
  77. this.config.put("canvas.height", "100");
  78. final int canvasWidth = Integer.parseInt(this.config.get("canvas.width"));
  79. final int canvasHeight = Integer.parseInt(this.config.get("canvas.height"));
  80. setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  81. cl = certainlogic;
  82. cl.addObserver(this);
  83. // register for mouse events if required
  84. if (this.config.containsKey("readonly") == false) {
  85. if (this.config.containsKey("readonly.mouse") == false)
  86. this.config.put("readonly.mouse", "false");
  87. } else {
  88. String readOnly = this.config.get("readonly");
  89. if (this.config.containsKey("readonly.mouse") == false)
  90. this.config.put("readonly.mouse", readOnly);
  91. }
  92. if (this.config.get("readonly.mouse").compareToIgnoreCase("false") == 0) {
  93. addMouseListener(new MouseAdapter () {
  94. @Override
  95. public void mousePressed(MouseEvent e) {
  96. setTCByMouseEvent(e, canvasWidth, canvasHeight);
  97. }});
  98. addMouseMotionListener(new MouseMotionAdapter () {
  99. @Override
  100. public void mouseDragged(MouseEvent e) {
  101. setTCByMouseEvent(e, canvasWidth, canvasHeight);
  102. }});
  103. } else if (this.config.get("readonly.mouse").compareToIgnoreCase("true") != 0)
  104. throw new IllegalArgumentException("Mouse has no valid readonly setting");
  105. // call paint() indirectly the first time
  106. update(null, null);
  107. }
  108. private void setTCByMouseEvent(MouseEvent e, int canvasWidth, int canvasHeight) {
  109. // mouse position to x and y coordinate
  110. double pointer_x = Math.max(Math.min(canvasWidth, e.getX()), 0);
  111. double pointer_y = Math.max(Math.min(canvasHeight, canvasHeight - e.getY()), 0);
  112. // ensure that the value is in the interval [0;1]
  113. double t = Math.max(Math.min(pointer_x / canvasWidth, 1.0), 0.0);
  114. double c = Math.max(Math.min(pointer_y / canvasHeight, 1.0), 0.0);
  115. //------------------modified by Debashis------------//
  116. if(c == 1){
  117. c = 0.999;
  118. }
  119. cl.setTC(t, c);
  120. }
  121. @Override
  122. public void update(Observable ignored1, Object ignored2) {
  123. repaint();
  124. }
  125. @Override
  126. public void paint(Graphics g) {
  127. final int canvasWidth = Integer.parseInt(this.config.get("canvas.width"));
  128. final int canvasHeight = Integer.parseInt(this.config.get("canvas.height"));
  129. if (null == buffer) {
  130. buffer = createImage(canvasWidth, canvasHeight);
  131. }
  132. // do we need to update the colorgradient?
  133. double initf = cl.getF();
  134. if (old_f != initf) {
  135. Graphics bg = buffer.getGraphics();
  136. for (int y = 0; y < canvasHeight; ++y){
  137. double certainty = 1 - ((double)y / canvasHeight);
  138. double resultp2 = ((1 - certainty) * initf);
  139. for (int x = 0;x < canvasWidth; ++x){
  140. double trust = (double)x / canvasWidth;
  141. double result = (trust * certainty) + resultp2;
  142. if (result < 0.5) {
  143. bg.setColor(new Color(255, (int) Math.min(255, (255 * 2 * result)), 0));
  144. } else {
  145. bg.setColor(new Color((int) Math.min(255, (2 - (2 * result)) * 255), 255, 0));
  146. }
  147. bg.drawLine(x, y, x, y);
  148. }
  149. }
  150. old_f = initf;
  151. }
  152. g.drawImage(buffer, 0, 0, this);
  153. // draw the crosshair
  154. g.setColor(Color.BLACK);
  155. int pointer_x = (int) Math.round(cl.getT() * canvasWidth);
  156. int pointer_y = (int) Math.round((1 - cl.getC()) * canvasHeight);
  157. g.drawLine(pointer_x - CROSSHAIRLENGTH, pointer_y, pointer_x + CROSSHAIRLENGTH, pointer_y);
  158. g.drawLine(pointer_x, pointer_y - CROSSHAIRLENGTH, pointer_x, pointer_y + CROSSHAIRLENGTH);
  159. }
  160. }