baby steps towards loading map bg from file

This commit is contained in:
musilto9 2020-03-05 01:56:56 +01:00
parent c8f8d7dde3
commit 0641c81429
6 changed files with 79 additions and 1 deletions

View File

@ -19,5 +19,7 @@ public class Block extends Cell {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);
g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE); g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE);
g.drawImage(WindowOfFreeMove.getMap().getImagesOfTiles()[102], getX() * WindowOfFreeMove.CELL_SIZE,
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
} }
} }

View File

@ -1,5 +1,5 @@
public class Game { public class Game {
public static void main(String[] args) { public static void main(String[] args) {
new WindowOfFreeMove().start(); } new WindowOfFreeMove().start();}
} }

View File

@ -1,4 +1,11 @@
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.io.File;
import java.util.Scanner;
public class Map { public class Map {
private String name; private String name;
@ -176,4 +183,70 @@ public class Map {
return cell; return cell;
return moveToCells.getFirst(); return moveToCells.getFirst();
} }
private static Scanner scanner;
private static BufferedImage image;
private static BufferedImage[] imagesOfTiles;
public void readMapFile(String name) {
try {
File mapFile = new File("src/main/resources/" + name + ".txt");
scanner = new Scanner(mapFile);
String firstLine = scanner.nextLine();
String[] mapInfo = firstLine.split(" ");
ImageTileset(mapInfo);
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private void ImageTileset(String[] mapInfo) {
try {
image = ImageIO.read(getClass().getResource(mapInfo[0]));
} catch (IOException e) {
e.printStackTrace();
}
int width = Integer.parseInt(mapInfo[1]);
int height = Integer.parseInt(mapInfo[2]);
int tileSize = Integer.parseInt(mapInfo[3]);
int Xtiles = width / tileSize;
int Ytiles = height / tileSize;
int tiles = Xtiles * Ytiles;
System.out.println(tiles);
imagesOfTiles = new BufferedImage[tiles];
Rectangle[] rectangles = new Rectangle[tiles];
int count = 0;
for (int i = 0; i < rectangles.length/Ytiles; i++){
for (int j = 0; j < rectangles.length/Xtiles; j++) {
rectangles[count] = new Rectangle(tileSize * i, tileSize * j, tileSize, tileSize);
count++;
}
}
if (rectangles.length < tiles)
throw new IllegalArgumentException();
for (int i = 0; i < imagesOfTiles.length; i++) {
imagesOfTiles[i] = image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width);
}
// while (scanner.hasNextLine()) {
// String data = scanner.nextLine();
// System.out.println(imagesOfTiles[Integer.parseInt(data)]);
// }
}
public BufferedImage[] getImagesOfTiles() {
return imagesOfTiles;
}
} }

View File

@ -163,6 +163,8 @@ public class WindowOfFreeMove extends JFrame {
map.createMap(); map.createMap();
isReadyToPaint = true; isReadyToPaint = true;
canvas.repaint(); canvas.repaint();
map.readMapFile("map-test");
// //
// lp.remove(dialogBar); // lp.remove(dialogBar);
// //

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -0,0 +1 @@
A2_Ground.png 512 384 32