fork download
  1.  
  2. public class Main {
  3. public static void main(String[] args) {
  4. VirtualScreen screen = new VirtualScreen(40, 10);
  5.  
  6. screen.box('+', 3, 0, 20, 9);
  7.  
  8. screen.line('#', 5, 2, 3, 9);
  9. screen.line('#', 7, 2, 10, 9);
  10.  
  11. screen.line('#', 9, 1, 9, 3);
  12. screen.line('#', 11, 1, 11, 3);
  13.  
  14. screen.line('#', 14, 2, 19, 2);
  15. screen.line('#', 18, 2, 19, 9);
  16. screen.line('#', 19, 8, 17, 8);
  17.  
  18. screen.line('#', 16, 1, 13, 9);
  19.  
  20. screen.circle('*', 28, 5, 4);
  21. screen.circle('*', 28, 5, 2);
  22.  
  23. screen.print();
  24. }
  25. }
  26.  
  27. class VirtualScreen {
  28. protected int width;
  29. protected int height;
  30. protected String[] lines;
  31.  
  32. VirtualScreen() {
  33. this(80, 25);
  34. }
  35. VirtualScreen(int width, int height) {
  36. this(' ', width, height);
  37. }
  38. VirtualScreen(char ch, int width, int height) {
  39. reset(ch, width, height);
  40. }
  41.  
  42. public void reset(char ch, int width, int height) {
  43. this.width = width;
  44. this.height = height;
  45. this.lines = new String[height];
  46. for (int i = 0; i < height; ++i) {
  47. lines[i] = "";
  48. for (int j = 0; j < width; ++j) {
  49. lines[i] = lines[i] + ch;
  50. }
  51. }
  52. }
  53.  
  54. public void print() {
  55. for (int y = 0; y < height; ++y) {
  56. System.out.println(lines[y]);
  57. }
  58. }
  59.  
  60. public void setPixel(char ch, int x, int y) {
  61. if (x < 0 || width <= x)
  62. return;
  63. if (y < 0 || height <= y)
  64. return;
  65. String str = lines[y];
  66. lines[y] = str.substring(0, x) + ch + str.substring(x + 1);
  67. }
  68.  
  69. public void fillBox(char ch, int left, int top, int right, int bottom) {
  70. for (int y = top; y <= bottom; ++y) {
  71. for (int x = left; x <= right; ++x) {
  72. setPixel(ch, x, y);
  73. }
  74. }
  75. }
  76.  
  77. public void line(char ch, int x0, int y0, int x1, int y1) {
  78. int dx = x1 - x0, dy = y1 - y0;
  79. if (Math.abs(dx) <= Math.abs(dy)) {
  80. if (dy < 0) {
  81. for (int y = y1; y < y0; ++y) {
  82. int x = x1 + (y - y1) * dx / dy;
  83. setPixel(ch, x, y);
  84. }
  85. } else if (dy > 0) {
  86. for (int y = y0; y < y1; ++y) {
  87. int x = x0 + (y - y0) * dx / dy;
  88. setPixel(ch, x, y);
  89. }
  90. }
  91. } else {
  92. if (dx < 0) {
  93. for (int x = x1; x < x0; ++x) {
  94. int y = y1 + (x - x1) * dy / dx;
  95. setPixel(ch, x, y);
  96. }
  97. } else if (dx > 0) {
  98. for (int x = x0; x < x1; ++x) {
  99. int y = y0 + (x - x0) * dy / dx;
  100. setPixel(ch, x, y);
  101. }
  102. }
  103. }
  104. }
  105.  
  106. public void box(char ch, int x0, int y0, int x1, int y1) {
  107. line(ch, x0, y0, x1, y0);
  108. line(ch, x1, y0, x1, y1);
  109. line(ch, x1, y1, x0, y1);
  110. line(ch, x0, y1, x0, y0);
  111. }
  112.  
  113. public void circle(char ch, int x, int y, double r) {
  114. if (r == 0) {
  115. return;
  116. }
  117. for(double t = 0; t < 2 * Math.PI; t += 1 / (2 * Math.PI * r)) {
  118. double px = x + r * Math.cos(t);
  119. double py = y + r * Math.sin(t);
  120. setPixel(ch, round(px), round(py));
  121. }
  122. }
  123.  
  124. protected int round(double d) {
  125. return (int)Math.round(d);
  126. }
  127. }
  128.  
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
   ++++++++++++++++++                   
   +     # #    #   +     *****         
   + # # # #  ##### +    **   **        
   + # #        # # +   ** *** **       
   + # #       #  # +   * ** ** *       
   + #  #      #  # +   * *   * *       
   +#   #      #  # +   * ** ** *       
   +#    #    #   # +   ** *** **       
   +#    #    #  ## +    **   **        
   +++++++++++++++++      *****