baby steps towards loading map bg from file
This commit is contained in:
parent
c8f8d7dde3
commit
0641c81429
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
//
|
//
|
||||||
|
|||||||
BIN
src/main/resources/A2_Ground.png
Normal file
BIN
src/main/resources/A2_Ground.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
1
src/main/resources/map-test.txt
Normal file
1
src/main/resources/map-test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
A2_Ground.png 512 384 32
|
||||||
Loading…
x
Reference in New Issue
Block a user