Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import org.jetbrains.annotations.NotNull;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.image.CropImageFilter;
- import java.awt.image.FilteredImageSource;
- import java.util.ArrayList;
- public class Panel extends JPanel {
- private final ArrayList<Image> tileset;
- private final Integer[] data;
- private @NotNull Image createCroppedImage(@NotNull ImageIcon image, int x, int y, int width, int height) {
- FilteredImageSource croppedImage = new FilteredImageSource(
- image.getImage().getSource(),
- new CropImageFilter(x, y, width, height)
- );
- return createImage(croppedImage);
- }
- private ArrayList<Image> loadTileset(String filepath, int tileWidth, int tileHeight) {
- if (filepath == null) {
- return null;
- }
- ArrayList<Image> images = new ArrayList<>();
- images.add(createImage(tileWidth, tileHeight));
- ImageIcon tileset = new ImageIcon(filepath);
- if (tileset.getImage() == null) {
- return null;
- }
- int tilesX = tileset.getIconWidth() / tileWidth;
- int tilesY = tileset.getIconHeight() / tileHeight;
- for (int i = 0; i < tilesX * tilesY; ++i) {
- images.add(this.createCroppedImage(tileset, tileWidth * (i % tilesX), tileHeight * (i / tilesX), tileWidth, tileHeight));
- }
- return images;
- }
- public Panel() {
- 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, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0, 0, 0, 0, 0, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 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, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 5, 6, 7, 0, 0, 12, 0, 1, 2, 2, 2, 2, 3, 0, 0, 5, 6, 7, 0, 0, 9, 10, 11, 0, 0, 12, 0, 5, 6, 6, 6, 6, 7, 0, 0, 9, 10, 11, 0, 0, 13, 14, 15, 0, 0, 12, 0, 9, 10, 10, 10, 10, 11, 0, 0, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 12, 0, 9, 10, 10, 10, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 9, 10, 10, 10, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 9, 10, 10, 10, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int tileWidth = 32;
- int tileHeight = 32;
- this.tileset = this.loadTileset("resources/tileset.png", tileWidth, tileHeight);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(this.tileset.get(19), 0, 0, 100, 100, this);
- }
- }
Add Comment
Please, Sign In to add comment