Miloz46

Untitled

Feb 28th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package com.company;
  2.  
  3. import org.jetbrains.annotations.NotNull;
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.image.CropImageFilter;
  8. import java.awt.image.FilteredImageSource;
  9. import java.util.ArrayList;
  10.  
  11. public class Panel extends JPanel {
  12.  
  13. private final ArrayList<Image> tileset;
  14. private final Integer[] data;
  15.  
  16. private @NotNull Image createCroppedImage(@NotNull ImageIcon image, int x, int y, int width, int height) {
  17.  
  18. FilteredImageSource croppedImage = new FilteredImageSource(
  19. image.getImage().getSource(),
  20. new CropImageFilter(x, y, width, height)
  21. );
  22.  
  23. return createImage(croppedImage);
  24.  
  25. }
  26.  
  27. private ArrayList<Image> loadTileset(String filepath, int tileWidth, int tileHeight) {
  28.  
  29. if (filepath == null) {
  30.  
  31. return null;
  32.  
  33. }
  34.  
  35. ArrayList<Image> images = new ArrayList<>();
  36. images.add(createImage(tileWidth, tileHeight));
  37.  
  38. ImageIcon tileset = new ImageIcon(filepath);
  39. if (tileset.getImage() == null) {
  40.  
  41. return null;
  42.  
  43. }
  44.  
  45. int tilesX = tileset.getIconWidth() / tileWidth;
  46. int tilesY = tileset.getIconHeight() / tileHeight;
  47.  
  48. for (int i = 0; i < tilesX * tilesY; ++i) {
  49.  
  50. images.add(this.createCroppedImage(tileset, tileWidth * (i % tilesX), tileHeight * (i / tilesX), tileWidth, tileHeight));
  51.  
  52. }
  53.  
  54. return images;
  55.  
  56. }
  57.  
  58. public Panel() {
  59.  
  60. this.data = new Integer[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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61.  
  62. int tileWidth = 32;
  63. int tileHeight = 32;
  64.  
  65. this.tileset = this.loadTileset("resources/tileset.png", tileWidth, tileHeight);
  66.  
  67. }
  68.  
  69. @Override
  70. protected void paintComponent(Graphics g) {
  71.  
  72. super.paintComponent(g);
  73.  
  74. g.drawImage(this.tileset.get(19), 0, 0, 100, 100, this);
  75.  
  76. }
  77. }
  78.  
Add Comment
Please, Sign In to add comment