WritableImage.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package me.hammerle.supersnuvi.javafx;
  2. import javafx.scene.image.PixelWriter;
  3. public class WritableImage extends Image
  4. {
  5. private final javafx.scene.image.WritableImage fxImage;
  6. private final PixelWriter pixelWriter;
  7. private final int width;
  8. private final int height;
  9. public WritableImage(int width, int height)
  10. {
  11. fxImage = new javafx.scene.image.WritableImage(width, height);
  12. pixelWriter = fxImage.getPixelWriter();
  13. this.width = width;
  14. this.height = height;
  15. }
  16. private int toARGB(int r, int g, int b, int alpha)
  17. {
  18. return (b & 0xFF) | ((g & 0xFF) << 8) | ((r & 0xFF) << 16) | ((alpha & 0xFF) << 24);
  19. }
  20. public void setPixel(int x, int y, int r, int g, int b, int alpha)
  21. {
  22. pixelWriter.setArgb(0, 0, toARGB(r, g, b, alpha));
  23. }
  24. public void clear(int r, int g, int b, int alpha)
  25. {
  26. int black = toARGB(r, g, b, alpha);
  27. for(int x = 0; x < width; x++)
  28. {
  29. for(int y = 0; y < height; y++)
  30. {
  31. pixelWriter.setArgb(x, y, black);
  32. }
  33. }
  34. }
  35. @Override
  36. protected javafx.scene.image.Image getImage()
  37. {
  38. return fxImage;
  39. }
  40. @Override
  41. public double getWidth()
  42. {
  43. return width;
  44. }
  45. @Override
  46. public double getHeight()
  47. {
  48. return height;
  49. }
  50. }