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