Guest User

Untitled

a guest
Nov 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. class Question3
  2. {
  3. public static void main ( String args [] )
  4. {
  5. (new VectorGraphics (20, 20, 100)).run(args[0]);
  6. }
  7. }
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. import java.util.*;
  20. import java.io.*;
  21.  
  22. /**
  23. * Assignment 5 Q3 VectorGraphics 2011 version
  24. *
  25. * @version 1.1
  26. */
  27. class VectorGraphics {
  28. private static final String RENDER = "W";
  29. private static final String EXIT = "X";
  30. private static final String ADD = "A";
  31. private static final String MOVE = "M";
  32. private static final String RECTANGLE = "RECTANGLE";
  33. private static final String HLINE = "HLINE";
  34. private static final String VLINE = "VLINE";
  35. private static final String PTLINE = "PTLINE";
  36. private static final String DELETE = "D";
  37.  
  38. private int xSize, ySize;
  39. private VectorObject[] objects;
  40. private int numberOfObjects;
  41.  
  42. VectorGraphics(int x, int y, int objSize) {
  43. this.xSize = x;
  44. this.ySize = y;
  45. this.objects = new VectorObject[objSize];
  46. this.numberOfObjects = 0;
  47. }
  48.  
  49. void run(String filename) {
  50. Scanner inputStream = null;
  51.  
  52. try {
  53. inputStream = new Scanner(new FileInputStream(filename));
  54. } catch (Exception e) {
  55. System.exit(0);
  56. }
  57.  
  58. while (inputStream.hasNextLine()) {
  59. String action = inputStream.next().toUpperCase();
  60. if (action.equals(EXIT)) {
  61. return;
  62. } else if (action.equals(RENDER)) {
  63. render();
  64. } else if (action.equals(ADD)) {
  65. int id = inputStream.nextInt();
  66. int x = inputStream.nextInt();
  67. int y = inputStream.nextInt();
  68. String obj = inputStream.next().toUpperCase();
  69. if (obj.equals(RECTANGLE)) {
  70. int xLen = inputStream.nextInt();
  71. int yLen = inputStream.nextInt();
  72. add(new Rectangle(id, x, y, xLen, yLen));
  73. } else if (obj.equals(HLINE)) {
  74. int len = inputStream.nextInt();
  75. add(new HLine(id, x, y, len));
  76. } else if (obj.equals(VLINE)) {
  77. int len = inputStream.nextInt();
  78. add(new VLine(id, x, y, len));
  79. } else if (obj.equals(PTLINE)) {
  80. int bx = inputStream.nextInt();
  81. int by = inputStream.nextInt();
  82. add(new PtLine(id, x, y, bx, by));
  83. }
  84. } else if (action.equals(DELETE)) {
  85. int id = inputStream.nextInt();
  86. delete(id);
  87. } else if (action.equals(MOVE)) {
  88. int id = inputStream.nextInt();
  89. int x = inputStream.nextInt();
  90. int y = inputStream.nextInt();
  91. move(id, x, y);
  92. }
  93. }
  94.  
  95. inputStream.close();
  96. }
  97.  
  98. private void add(VectorObject anObject) {
  99. objects[numberOfObjects++] = anObject;
  100. }
  101.  
  102. private boolean delete(int aNumber) {
  103. for (int i = 0; i < numberOfObjects; i++)
  104. if (aNumber == objects[i].getId()) {
  105. for (int j = i; j < numberOfObjects - 1; j++)
  106. objects[j] = objects[j + 1];
  107. numberOfObjects--;
  108. return true;
  109. }
  110. return false;
  111. }
  112.  
  113. private void move(int aNumber, int x, int y) {
  114. for (int i = 0; i < numberOfObjects; i++)
  115. if (aNumber == objects[i].getId()) {
  116. objects[i].setNewCoords(x, y);
  117. return;
  118. }
  119. }
  120.  
  121. private void render() {
  122. char[][] matrix = new char[ySize][xSize];
  123. for (int y = 0; y < ySize; y++) {
  124. for (int x = 0; x < xSize; x++) {
  125. matrix[y][x] = &#039; &#039;;
  126. }
  127. }
  128. for (int i = 0; i < numberOfObjects; i++) {
  129. objects[i].draw(matrix);
  130. }
  131. System.out.print(&#039;+&#039;);
  132. for (int x = 0; x < xSize; x++) {
  133. System.out.print(&#039;-&#039;);
  134. }
  135. System.out.println(&#039;+&#039;);
  136. for (int y = 0; y < ySize; y++) {
  137. System.out.print(&#039;+&#039;);
  138. for (int x = 0; x < xSize; x++) {
  139. System.out.print(matrix[y][x]);
  140. }
  141. System.out.println(&#039;+&#039;);
  142. }
  143. System.out.print(&#039;+&#039;);
  144. for (int x = 0; x < xSize; x++) {
  145. System.out.print(&#039;-&#039;);
  146. }
  147. System.out.println(&#039;+&#039;);
  148. }
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. abstract class VectorObject
  158. {
  159. protected int id, x, y;
  160.  
  161. VectorObject ( int anId, int ax, int ay )
  162. {
  163. id = anId;
  164. x = ax;
  165. y = ay;
  166. }
  167.  
  168. int getId ()
  169. {
  170. return id;
  171. }
  172.  
  173. void setNewCoords ( int newx, int newy )
  174. {
  175. x = newx;
  176. y = newy;
  177. }
  178.  
  179. public abstract void draw ( char [][] matrix );
  180. }
Add Comment
Please, Sign In to add comment