LinkColorManager.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration;
  2. import java.awt.Color;
  3. import java.util.HashMap;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility;
  7. /**
  8. * Container for the different Link colors
  9. *
  10. * @author Andreas T. Meyer-Berg
  11. */
  12. public class LinkColorManager {
  13. /**
  14. * Map which stores the Colors and circle positions for all links
  15. */
  16. private HashMap<Link, Pair<Integer,Color>> linkColors = null;
  17. /**
  18. * Integer which contains the next color index, which should be used
  19. */
  20. private int i = 0;
  21. /**
  22. * Initialize the color manager
  23. */
  24. public LinkColorManager() {
  25. linkColors = new HashMap<Link, Pair<Integer,Color>>();
  26. }
  27. /**
  28. * Returns the color and index of the given Link
  29. * @param link link which should be looked up
  30. * @return Index and Color
  31. */
  32. public Pair<Integer,Color> getColorOfLink(Link link){
  33. return linkColors.get(link);
  34. }
  35. /**
  36. * Removes the given Link and its color
  37. * @param link link to be removed
  38. */
  39. public void removeLink(Link link){
  40. linkColors.remove(link);
  41. }
  42. /**
  43. * Adds a link with the given Color
  44. * @param link link to be added
  45. * @param pair Index and Color of the link
  46. */
  47. public void addLinkColor(Link link, Pair<Integer, Color> pair) {
  48. linkColors.put(link, pair);
  49. }
  50. /**
  51. * Returns the color of the next Link
  52. * @return next color
  53. */
  54. public Color getNextLinkColor(){
  55. /**
  56. * Distinct Color as String containing the hex values (128 static ones should be enough)
  57. */
  58. String colorHex = Utility.indexcolors[i++%Utility.indexcolors.length];
  59. /**
  60. * Color of the given Hex String (Base16)
  61. */
  62. Color color = new Color(
  63. Integer.valueOf(colorHex.substring(1,3),16),
  64. Integer.valueOf(colorHex.substring(3,5),16),
  65. Integer.valueOf(colorHex.substring(5,7),16));
  66. return color;
  67. }
  68. }