-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
124 lines (113 loc) · 3.21 KB
/
Map.java
File metadata and controls
124 lines (113 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.awt.event.KeyEvent;
public class Map extends JPanel{
int height = 0;
int width = 0;
int mapLegend[][];
int pixSizeWidth;
int pixSizeHeight;
int startX = -1;
int startY = -1;
BufferedImage mapImage;
public Map(String filename, int width, int height){
this.height = height*2;
this.width = width*2;
int row = 0;
this.setPreferredSize(new Dimension(width, height));
try{
BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
String line = br.readLine();
String[] pixDim = line.split(" ");
mapLegend = new int[Integer.parseInt(pixDim[1])][Integer.parseInt(pixDim[0])];
pixSizeWidth = this.width/Integer.parseInt(pixDim[0]);
pixSizeHeight = this.height/Integer.parseInt(pixDim[1]);
while((line = br.readLine()) != null) {
String[] textures = line.split(" ");
for (int col = 0; col < textures.length; col++) {
mapLegend[row][col] = Integer.parseInt(textures[col]);
}
row++;
}
createImage();
}catch(Exception e) {
e.printStackTrace();
}
}
public void createImage(){
mapImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
BufferedImage img = null;
Graphics2D g2d = (Graphics2D) mapImage.createGraphics();
//super.repaint();
for(int i = 0 ; i < mapLegend.length ; i++){
for(int j = 0 ; j < mapLegend[i].length ; j++){
try{
switch(mapLegend[i][j]){
case 1:
img = ImageIO.read(new File("piks/start.png"));
if(startX == -1) startX = j*pixSizeHeight;
if(startY == -1) startY = i*pixSizeWidth;
break;
case 2:
img = ImageIO.read(new File("piks/path.png"));
break;
case 3:
img = ImageIO.read(new File("piks/wall.png"));
break;
case 4:
img = ImageIO.read(new File("piks/brick.png"));
}
}catch (Exception e){
e.printStackTrace();
}
img = resize(img, pixSizeWidth, pixSizeHeight);
g2d.drawImage(img, j*pixSizeWidth, i*pixSizeHeight, null);
}
}
}
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(mapImage, 0,0, null);
}
public BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
public boolean checkCollision(int x, int y, int dir){
switch(dir){
case KeyEvent.VK_S:
x = x/ pixSizeWidth;
y = (y+25)/pixSizeHeight;
break;
case KeyEvent.VK_W:
x = x/pixSizeWidth;
y = y/pixSizeHeight;
break;
case KeyEvent.VK_D:
x = (x+35)/pixSizeWidth;
y = y/pixSizeHeight;
break;
case KeyEvent.VK_A:
x = x/pixSizeWidth;
y = y/pixSizeHeight;
break;
}
if(mapLegend[y][x] == 3) return false;
return true;
}
public boolean checkWin(int x, int y, int dir){
x = x/pixSizeWidth;
y = y/pixSizeHeight;
if(mapLegend[y][x] == 4) return true;
return false;
}
}