Advertisement
Guest User

PuzzleImage.java

a guest
Jun 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.scene.image.Image;
  4. import javafx.scene.image.PixelReader;
  5. import javafx.scene.image.WritableImage;
  6.  
  7. import java.util.ArrayList;
  8.  
  9. public class PuzzleImage {
  10. ArrayList<WritableImage> cutPieces = new ArrayList<>();
  11. ArrayList<Integer> rowInd = new ArrayList<>();
  12. ArrayList<Integer> colInd = new ArrayList<>();
  13. Image srcImg;
  14. boolean loaded = false;
  15. int rectNum;
  16.  
  17. public void outprintRowsAndIndices(){
  18. System.out.println("Wiersze:");
  19. for(int i = 0; i < rowInd.size(); i++){
  20. System.out.println(rowInd.get(i));
  21. }
  22. System.out.println("Kolumny:");
  23. for(int i = 0; i < colInd.size(); i++){
  24. System.out.println(colInd.get(i));
  25. }
  26. }
  27.  
  28. public void loadImage(String path){
  29. srcImg = new Image(path);
  30. loaded = true;
  31. }
  32.  
  33. public void cutImage(int rectNum) throws Exception {
  34. this.rectNum = rectNum;
  35. System.out.println(rectNum);
  36. if(loaded) {
  37. double width, height;
  38. if (rectNum == 6) {
  39. height = srcImg.getHeight() / 2;
  40. System.out.println(height);
  41. width = srcImg.getWidth() / 3;
  42. System.out.println(width);
  43. PixelReader reader = srcImg.getPixelReader();
  44. for (int i = 0; i < 2; i++) {
  45. for (int j = 0; j < rectNum/2; j++) {
  46. cutPieces.add(new WritableImage(reader, (int) ((j+1) * width - width), (int) ((i+1) * height - height), (int) width, (int) height));
  47. rowInd.add(i);
  48. colInd.add(j);
  49. }
  50. }
  51. outprintRowsAndIndices();
  52. }
  53.  
  54. if (rectNum == 8) {
  55. height = srcImg.getHeight() / 2;
  56. System.out.println(height);
  57. width = srcImg.getWidth() / 4;
  58. System.out.println(width);
  59. PixelReader reader = srcImg.getPixelReader();
  60. for (int i = 0; i < 2; i++) {
  61. for (int j = 0; j < rectNum / 2; j++) {
  62. cutPieces.add(new WritableImage(reader, (int) (j * width - width), (int) (i * height - height), (int) width, (int) height));
  63. rowInd.add(i - 1);
  64. colInd.add(j - 1);
  65. }
  66. }
  67. outprintRowsAndIndices();
  68. }
  69.  
  70. if (rectNum == 10) {
  71. height = srcImg.getHeight() / 2;
  72. System.out.println(height);
  73. width = srcImg.getWidth() / 5;
  74. System.out.println(width);
  75. PixelReader reader = srcImg.getPixelReader();
  76. for (int i = 1; i <= 2; i++) {
  77. for (int j = 1; j <= rectNum / 2; j++) {
  78. cutPieces.add(new WritableImage(reader, (int) (j * width - width), (int) (i * height - height), (int) width, (int) height));
  79. rowInd.add(i - 1);
  80. colInd.add(j - 1);
  81. }
  82. }
  83. outprintRowsAndIndices();
  84. }
  85. }else{
  86. throw new Exception("Load an image first");
  87. }
  88. }
  89.  
  90. public int row_getSize(){
  91. return rowInd.size();
  92. }
  93.  
  94. public int col_getSize(){
  95. return colInd.size();
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement