Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package domain;
  2.  
  3. import java.awt.Graphics;
  4. import java.util.ArrayList;
  5.  
  6. public class Tekening implements Drawable{
  7. private static final int MIN_X = 0;
  8. private static final int MAX_X = 399;
  9. private static final int MIN_Y = 0;
  10. private static final int MAX_Y = 399;
  11. private String naam;
  12. protected ArrayList<Vorm> vormen;
  13.  
  14. public Tekening(String naam) {
  15. setNaam(naam);
  16. vormen = new ArrayList<>();
  17. }
  18.  
  19. private void setNaam(String naam) {
  20. if (naam == null || naam.trim().isEmpty()) throw new IllegalArgumentException();
  21. else this.naam = naam;
  22. }
  23.  
  24. public String getNaam() {
  25. return naam;
  26. }
  27.  
  28. public static int getMinX() {
  29. return MIN_X;
  30. }
  31.  
  32. public static int getMaxX() {
  33. return MAX_X;
  34. }
  35.  
  36. public static int getMinY() {
  37. return MIN_Y;
  38. }
  39.  
  40. public static int getMaxY() {
  41. return MAX_Y;
  42. }
  43.  
  44. public void voegToe(Vorm vorm) {
  45. if (vormPast(vorm) == true) {
  46. this.vormen.add(vorm);
  47. }
  48. }
  49.  
  50.  
  51. private boolean vormPast(Vorm vorm) {
  52. if (vorm.getOmhullende().getLinkerBovenhoek().getX() < MIN_X) {
  53. throw new DomainException("De toegevoegde vorm past niet in de tekening langs de linkererkant.");
  54. }
  55. if (vorm.getOmhullende().getLinkerBovenhoek().getX() + vorm.getOmhullende().getBreedte() > MAX_X) {
  56. throw new DomainException("De toegevoegde vorm past niet in de tekening langs de rechterkant.");
  57. }
  58. if (vorm.getOmhullende().getLinkerBovenhoek().getY() < MIN_Y) {
  59. throw new DomainException("De toegevoegde vorm past niet in de tekening langs de bovenkant.");
  60. }
  61. if (vorm.getOmhullende().getLinkerBovenhoek().getY() + vorm.getOmhullende().getHoogte() > MAX_Y) {
  62. throw new DomainException("De toegevoegde vorm past niet in de tekening langs de onderkant.");
  63. }
  64. return true;
  65. }
  66.  
  67. public Vorm getVorm(int index) {
  68. return vormen.get(index-1);
  69. }
  70.  
  71. public int getAantalVormen() {
  72. return vormen.size();
  73. }
  74.  
  75. public int aantalOnzichtbaar() {
  76. int teller = 0;
  77. for (int i = 0; i < this.vormen.size(); i++) {
  78. if (!this.vormen.get(i).isZichtbaar()) {
  79. teller++;
  80. }
  81. }
  82. return teller;
  83. }
  84.  
  85. public int aantalZichtbaar() {
  86. int teller = 0;
  87. for (Vorm v : vormen) {
  88. if (v.isZichtbaar()) {
  89. teller++;
  90. }
  91. }
  92. return teller;
  93. }
  94.  
  95. public void verwijder(Vorm vorm) {
  96. vormen.remove(vorm);
  97. }
  98.  
  99. public boolean bevat(Vorm vorm) {
  100. return vormen.contains(vorm);
  101. }
  102.  
  103. public boolean equals(Object obj) {
  104. if (obj instanceof Tekening) {
  105. Tekening t = (Tekening) obj;
  106. return t.vormen.containsAll(this.vormen) && t.vormen.size() == this.vormen.size();
  107. }
  108. return false;
  109. }
  110.  
  111. public String toString() {
  112. String result = "Tekening met naam " + this.naam + " bestaat uit " + vormen.size() + " vormen: \n";
  113. for (Vorm v : vormen) {
  114. result+= v.toString() + '\n';
  115. }
  116. return result;
  117. }
  118.  
  119. @Override
  120. public void teken(Graphics g) {
  121. for (Vorm v: this.vormen) {
  122. Drawable d = (Drawable) v;
  123. d.teken(g);
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement