added loading of map background from file

This commit is contained in:
musilto9 2020-03-13 01:15:00 +01:00
parent e8c909de67
commit 0c16b87dd1
7 changed files with 47 additions and 18 deletions

View File

@ -26,6 +26,6 @@ public class Cell {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
g.setColor(color); g.setColor(color);
g.fillOval(x*size, y*size, size, size); // g.fillOval(x*size, y*size, size, size);
} }
} }

View File

@ -47,7 +47,8 @@ public class EventMoveToCell extends Cell {
@Override @Override
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.drawImage(window.getMap().getImageCell().getImagesOfTiles().get(277), getX() * WindowOfFreeMove.CELL_SIZE,
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
g.drawString(Integer.toString(getId()), getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE); g.drawString(Integer.toString(getId()), getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE);
} }
} }

View File

@ -1,4 +1,5 @@
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
@ -10,18 +11,28 @@ import java.util.Scanner;
public class ImageCell { public class ImageCell {
private static BufferedImage image; private static BufferedImage image;
private static LinkedList<BufferedImage> imagesOfTiles; private static LinkedList<BufferedImage> imagesOfTiles;
private Scanner scanner;
private int[] mapBackground;
public void readMapFile(String name) { public void readMapFile(String name) {
try { try {
mapBackground = new int[WindowOfFreeMove.CANVAS_WIDTH * WindowOfFreeMove.CANVAS_HEIGHT];
int count = 0;
File mapFile = new File("src/main/resources/" + name + ".txt"); File mapFile = new File("src/main/resources/" + name + ".txt");
Scanner scanner = new Scanner(mapFile); scanner = new Scanner(mapFile);
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String data = scanner.nextLine(); String data = scanner.nextLine();
if (data.contains("<tileset>")) { if (data.contains("<tileset>")) {
String[] mapInfo = data.split(" "); String[] mapInfo = data.split(" ");
ImageTileset(mapInfo); ImageTileset(mapInfo);
} }
else {
String[] mapColumn = data.split(" ");
for (int i = 0; i < mapColumn.length; i++) {
mapBackground[count] = Integer.parseInt(mapColumn[i]);
count++;
}
}
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.println("An error occurred."); System.out.println("An error occurred.");
@ -29,10 +40,6 @@ public class ImageCell {
} }
} }
public void readMapFileDirect(String[] mapInfo) {
ImageTileset(mapInfo);
}
private void ImageTileset(String[] mapInfo) { private void ImageTileset(String[] mapInfo) {
try { try {
image = ImageIO.read(getClass().getResource(mapInfo[1])); image = ImageIO.read(getClass().getResource(mapInfo[1]));
@ -63,14 +70,14 @@ public class ImageCell {
for (int i = 0; i < tiles; i++) { for (int i = 0; i < tiles; i++) {
imagesOfTiles.add(image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width)); imagesOfTiles.add(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 LinkedList<BufferedImage> getImagesOfTiles() { public LinkedList<BufferedImage> getImagesOfTiles() {
return imagesOfTiles; return imagesOfTiles;
} }
public void paintBackground(Graphics g, int x, int y, int count) {
g.drawImage(imagesOfTiles.get(mapBackground[count]), y * WindowOfFreeMove.CELL_SIZE,
x * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
}
} }

View File

@ -10,7 +10,7 @@ public class InteractionCell extends Cell {
private ImageCell imageCell; private ImageCell imageCell;
InteractionCell(int x, int y, String message, Dialog dialog, ImageCell imageCell) { InteractionCell(int x, int y, String message, Dialog dialog, ImageCell imageCell) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.WHITE); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
this.message = message; this.message = message;
this.dialog = dialog; this.dialog = dialog;
this.imageCell = imageCell; this.imageCell = imageCell;

View File

@ -124,6 +124,10 @@ public class Map {
mapArray[x][y] = TypeOfCell.SPAWNPOINT; mapArray[x][y] = TypeOfCell.SPAWNPOINT;
} }
public ImageCell getImageCell() { return imageCell; }
public TypeOfCell[][] getMapArray() { return mapArray; }
public void createMap() { public void createMap() {
for (int i = 0; i < mapArray.length; i++) { for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < mapArray.length; j++) { for (int j = 0; j < mapArray.length; j++) {
@ -228,7 +232,4 @@ public class Map {
return cell; return cell;
return eventCells.getFirst(); return eventCells.getFirst();
} }
public ImageCell getImageCell() { return imageCell; }
} }

View File

@ -156,6 +156,9 @@ public class WindowOfFreeMove extends JFrame {
m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this)); m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this));
m1.getImageCell().readMapFile("m1-tileset"); m1.getImageCell().readMapFile("m1-tileset");
m2.getImageCell().readMapFile("m1-tileset");
m3.getImageCell().readMapFile("m1-tileset");
m4.getImageCell().readMapFile("m1-tileset");
setMap(m1); setMap(m1);
map.createMap(); map.createMap();
@ -204,6 +207,13 @@ public class WindowOfFreeMove extends JFrame {
Graphics2D g2D = (Graphics2D) g; Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
int count = 0;
for (int i = 0; i < CANVAS_WIDTH; i++) {
for (int j = 0; j < CANVAS_HEIGHT; j++) {
map.getImageCell().paintBackground(g2D, i, j, count);
count++;
}
}
for (Cell cell : map.getMoveToCells()) { for (Cell cell : map.getMoveToCells()) {
cell.paint(g2D); cell.paint(g2D);
} }

View File

@ -1,2 +1,12 @@
<tileset> A2_Ground.png 512 384 32 <tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32 <tileset> D_OutDoor.png 512 512 32
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 82 94 0 0 82 94 0 0
0 0 83 95 0 0 83 95 0 0
0 0 0 0 0 0 0 0 0 0
0 0 153 0 0 0 0 153 0 0
0 0 0 153 153 153 153 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