25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Canvas.java 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * libcaca Java bindings for libcaca
  3. * Copyright (c) 2009 Adrien Grand <jpountz@dinauz.org>
  4. *
  5. * This library is free software. It comes without any warranty, to
  6. * the extent permitted by applicable law. You can redistribute it
  7. * and/or modify it under the terms of the Do What The Fuck You Want
  8. * To Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. package org.zoy.caca;
  12. public class Canvas extends NativeObject {
  13. static {
  14. Caca.load();
  15. }
  16. private static native int getCursorX(long canvasPtr);
  17. private static native int getCursorY(long canvasPtr);
  18. private static native void setCursorXY(long canvasPtr, int x, int y);
  19. public class Cursor {
  20. protected Cursor() {}
  21. public int getX() {
  22. return getCursorX(ptr);
  23. }
  24. public int getY() {
  25. return getCursorY(ptr);
  26. }
  27. public void setXY(int x, int y) {
  28. setCursorXY(ptr, x, y);
  29. }
  30. }
  31. private static native int getHandleX(long canvasPtr);
  32. private static native int getHandleY(long canvasPtr);
  33. private static native void setHandleXY(long canvasPtr, int x, int y);
  34. public class Handle {
  35. protected Handle() {}
  36. public int getX() {
  37. return getHandleX(ptr);
  38. }
  39. public int getY() {
  40. return getHandleY(ptr);
  41. }
  42. public void setXY(int x, int y) {
  43. setHandleXY(ptr, x, y);
  44. }
  45. }
  46. // Is this canvas managed by a display?
  47. private final boolean managed;
  48. private final Cursor cursor;
  49. private final Handle handle;
  50. private static native long createCanvas(int width, int height);
  51. public Canvas(int width, int height) {
  52. this(createCanvas(width, height), false);
  53. }
  54. protected Canvas(long ptr) {
  55. this(ptr, true);
  56. }
  57. private Canvas(long ptr, boolean managed) {
  58. this.cursor = new Cursor();
  59. this.handle = new Handle();
  60. this.ptr = ptr;
  61. this.managed = managed;
  62. }
  63. public Cursor getCursor() {
  64. return cursor;
  65. }
  66. public Handle getHandle() {
  67. return handle;
  68. }
  69. private static native int getCanvasWidth(long canvasPtr);
  70. public int getWidth() {
  71. return getCanvasWidth(ptr);
  72. }
  73. private static native int getCanvasHeight(long canvasPtr);
  74. public int getHeight() {
  75. return getCanvasHeight(ptr);
  76. }
  77. private static native void setCanvasSize(long canvasPtr, int width, int height);
  78. public void setSize(int width, int height) {
  79. setCanvasSize(ptr, width, height);
  80. }
  81. private static native void clearCanvas(long canvasPtr);
  82. public void clear() {
  83. clearCanvas(ptr);
  84. }
  85. private static native int getCanvasChar(long canvasPtr, int x, int y);
  86. public int getChar(int x, int y) {
  87. return getCanvasChar(ptr, x, y);
  88. }
  89. private static native void putCanvasChar(long canvasPtr, int x, int y, int ch);
  90. public void put(int x, int y, int ch) {
  91. putCanvasChar(ptr, x, y, ch);
  92. }
  93. private static native void putCanvasString(long canvasPtr, int x, int y, String s);
  94. public void put(int x, int y, String s) {
  95. putCanvasString(ptr, x, y, s);
  96. }
  97. private static native void blitCanvas(long canvasPtr, int x, int y, long otherCanvasPtr, long maskCanvasPtr);
  98. public void blit(int x, int y, Canvas other, Canvas mask) {
  99. blitCanvas(ptr, x, y, other.ptr, mask.ptr);
  100. }
  101. private static native void setCanvasBoundaries(long canvasPtr, int x, int y, int width, int height);
  102. public void setBoundaries(int x, int y, int width, int height) {
  103. setCanvasBoundaries(ptr, x, y, width, height);
  104. }
  105. private static native void invertCanvas(long canvasPtr);
  106. public void invert() {
  107. invertCanvas(ptr);
  108. }
  109. private static native void flipCanvas(long canvasPtr);
  110. public void flip() {
  111. flipCanvas(ptr);
  112. }
  113. private static native void flopCanvas(long canvasPtr);
  114. public void flop() {
  115. flopCanvas(ptr);
  116. }
  117. private static native void rotateCanvas180(long canvasPtr);
  118. public void rotate180() {
  119. rotateCanvas180(ptr);
  120. }
  121. private static native void rotateCanvasLeft(long canvasPtr);
  122. public void rotateLeft() {
  123. rotateCanvasLeft(ptr);
  124. }
  125. private static native void rotateCanvasRight(long canvasPtr);
  126. public void rotateRight() {
  127. rotateCanvasRight(ptr);
  128. }
  129. private static native void stretchCanvasLeft(long canvasPtr);
  130. public void stretchLeft() {
  131. stretchCanvasLeft(ptr);
  132. }
  133. private static native void stretchCanvasRight(long canvasPtr);
  134. public void stretchRight() {
  135. stretchCanvasRight(ptr);
  136. }
  137. private static native int getCanvasAttribute(long canvasPtr, int x, int y);
  138. public int getAttribute(int x, int y) {
  139. return getCanvasAttribute(ptr, x, y);
  140. }
  141. private static native void setCanvasAttribute(long canvasPtr, int attr);
  142. public void setDefaultAttribute(int attribute) {
  143. setCanvasAttribute(ptr, attribute);
  144. }
  145. private static native void putCanvasAttribute(long canvasPtr, int x, int y, int attr);
  146. public void putAttribute(int x, int y, int attribute) {
  147. putCanvasAttribute(ptr, x, y, attribute);
  148. }
  149. private static native void setCanvasColorAnsi(long canvasPtr, byte colorAnsiFg, byte colorAnsiBg);
  150. public void setColor(Color.Ansi foreground, Color.Ansi background) {
  151. setCanvasColorAnsi(ptr, foreground.code, background.code);
  152. }
  153. private static native void setCanvasColorArgb(long canvasPtr, short colorArgbFg, short colorArbgBg);
  154. public void setColor(Color.Argb foreground, Color.Argb background) {
  155. setCanvasColorArgb(ptr, foreground.code, background.code);
  156. }
  157. private static native void canvasDrawLine(long canvasPtr, int x1, int y1, int x2, int y2, int ch);
  158. public void drawLine(int x1, int y1, int x2, int y2, int ch) {
  159. canvasDrawLine(ptr, x1, y1, x2, y2, ch);
  160. }
  161. private static native void canvasDrawPolyline(long canvasPtr, int[] x, int[] y, int ch);
  162. public void drawPolyline(int[] x, int[] y, int ch) {
  163. canvasDrawPolyline(ptr, x, y, ch);
  164. }
  165. private static native void canvasDrawThinLine(long canvasPtr, int x1, int y1, int x2, int y2);
  166. public void drawThinLine(int x1, int y1, int x2, int y2) {
  167. canvasDrawThinLine(ptr, x1, y1, x2, y2);
  168. }
  169. private static native void canvasDrawThinPolyline(long canvasPtr, int[] x, int[] y);
  170. public void drawThinPolyline(int[] x, int[] y, int ch) {
  171. canvasDrawThinPolyline(ptr, x, y);
  172. }
  173. private static native void canvasDrawCircle(long canvasPtr, int x, int y, int r, int ch);
  174. public void drawCircle(int x, int y, int r, int ch) {
  175. canvasDrawCircle(ptr, x, y, r, ch);
  176. }
  177. private static native void canvasDrawEllipse(long canvasPtr, int x, int y, int a, int b, int ch);
  178. public void drawEllipse(int x, int y, int a, int b, int ch) {
  179. canvasDrawEllipse(ptr, x, y, a, b, ch);
  180. }
  181. private static native void canvasDrawThinEllipse(long canvasPtr, int x, int y, int a, int b);
  182. public void drawThinEllipse(int x, int y, int a, int b) {
  183. canvasDrawThinEllipse(ptr, x, y, a, b);
  184. }
  185. private static native void canvasFillEllipse(long canvasPtr, int x, int y, int a, int b, int ch);
  186. public void fillEllipse(int x, int y, int a, int b, int ch) {
  187. canvasFillEllipse(ptr, x, y, a, b, ch);
  188. }
  189. private static native void canvasDrawBox(long canvasPtr, int x, int y, int width, int height, int ch);
  190. public void drawBox(int x, int y, int width, int height, int ch) {
  191. canvasDrawBox(ptr, x, y, width, height, ch);
  192. }
  193. private static native void canvasDrawThinBox(long canvasPtr, int x, int y, int width, int height);
  194. public void drawThinBox(int x, int y, int width, int height) {
  195. canvasDrawThinBox(ptr, x, y, width, height);
  196. }
  197. private static native void canvasDrawCp437Box(long canvasPtr, int x, int y, int width, int height);
  198. public void drawCp437Box(int x, int y, int width, int height) {
  199. canvasDrawCp437Box(ptr, x, y, width, height);
  200. }
  201. private static native void canvasFillBox(long canvasPtr, int x, int y, int width, int height, int ch);
  202. public void fillBox(int x, int y, int width, int height, int ch) {
  203. canvasFillBox(ptr, x, y, width, height, ch);
  204. }
  205. private static native void canvasDrawTriangle(long canvasPtr, int x1, int y1, int x2, int y2, int x3, int y3, int ch);
  206. public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int ch) {
  207. canvasDrawTriangle(ptr, x1, y1, x2, y2, x3, y3, ch);
  208. }
  209. private static native void canvasDrawThinTriangle(long canvasPtr, int x1, int y1, int x2, int y2, int x3, int y3);
  210. public void drawThinTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
  211. canvasDrawThinTriangle(ptr, x1, y1, x2, y2, x3, y3);
  212. }
  213. private static native void canvasFillTriangle(long canvasPtr, int x1, int y1, int x2, int y2, int x3, int y3, int ch);
  214. public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int ch) {
  215. canvasFillTriangle(ptr, x1, y1, x2, y2, x3, y3, ch);
  216. }
  217. private static native int getCanvasFrameCount(long canvasPtr);
  218. public int getFrameCount() {
  219. return getCanvasFrameCount(ptr);
  220. }
  221. private static native void setCanvasFrame(long canvasPtr, int id);
  222. public void setFrame(int id) {
  223. setCanvasFrame(ptr, id);
  224. }
  225. private static native String getCanvasFrameName(long canvasPtr);
  226. public String getFrameName() {
  227. return getCanvasFrameName(ptr);
  228. }
  229. private static native void setCanvasFrameName(long canvasPtr, String name);
  230. public void setFrameName(String name) {
  231. setCanvasFrameName(ptr, name);
  232. }
  233. private static native void createCanvasFrame(long canvasPtr, int id);
  234. public void createFrame(int id) {
  235. createCanvasFrame(ptr, id);
  236. }
  237. private static native void freeCanvasFrame(long canvasPtr, int id);
  238. public void removeFrame(int id) {
  239. freeCanvasFrame(ptr, id);
  240. }
  241. private static native void canvasRender(long canvasPtr, long fontPtr, byte[] buf,
  242. int width, int height, int pitch);
  243. public void render(Font font, byte[] buf, int width, int height, int pitch) {
  244. canvasRender(ptr, font.ptr, buf, width, height, pitch);
  245. }
  246. private static native void canvasDitherBitmap(long canvasPtr, int x, int y, int width,
  247. int height, long ditherPtr, byte[] pixels);
  248. public void ditherBitmap(int x, int y, int width, int height, Dither dither, byte[] pixels) {
  249. canvasDitherBitmap(ptr, x, y, width, height, dither.ptr, pixels);
  250. }
  251. private static native void freeCanvas(long canvasPtr);
  252. @Override
  253. public void finalize() throws Throwable {
  254. if (!managed)
  255. freeCanvas(ptr);
  256. super.finalize();
  257. }
  258. }