`I am just trying to get the paintComponent method from the LocateMap class to run. currently it never runs.
I am trying to draw a set number of images within a jpanel based on the dimensions given from the user (the dimensions are always symmetrical). I initially tried accomplishing this from within the window class but the paintComponent method never runs even when I run it manually. I am now trying to do this from a separate LocateMap class which I still cannot get the paintComponent method to run even when I manually run repaint();. I instead tried to create a class completely separated from the Window class to see if I could get it running to a much more specific and smaller scale. The test class runs the paintComponent method automatically without me even using repaint(); I have no idea why it isn’t working but the test showed me something is stopping the paintComponent method from running.
public class LocateMap extends JPanel {
public JPanel box = new JPanel();
int dimensions;
int tileNum[][];
int individualMapDimension;
ArrayList<String> tileNums;
ArrayList<String> collisionStatus;
Tile[] tile;
String filePath = "C:\Users\Bmcar\Desktop\Coding\TossedSouls\Maps\25x25_zeros.txt";
public LocateMap(int dimensions, int tileNum[][], int individualMapDimension, ArrayList<String> tileNums,
ArrayList<String> collisionStatus, Tile[] tile) {
this.dimensions = dimensions;
this.tileNum = tileNum;
this.individualMapDimension = individualMapDimension;
this.tileNums = tileNums;
this.collisionStatus = collisionStatus;
this.tile = tile;
drawMap();
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
handleClick(e);
}
});
}
public void drawMap() {
box.setLayout(new GridLayout());
box.setLocation(1200, 150);
box.setSize(600, 600);
box.setBackground(Color.white);
getAssetImage();
loadMap();
}
public void getAssetImage() {
for (int i = 0; i < tileNums.size(); i++) {
String tempFilePath;
boolean collision;
tempFilePath = tileNums.get(i);
if (collisionStatus.get(i).equals(true)) {
collision = true;
} else {
collision = false;
}
setup(i, tempFilePath, collision);
}
}
public void setup(int index, String imageName, boolean collision) {
UtilityTool uTool = new UtilityTool();
try {
tile[index] = new Tile();
BufferedImage img = ImageIO.read(new File(imageName));
if (img == null) {
throw new IOException("Image could not be read: " + imageName);
}
tile[index].image = uTool.scaleImage(img, individualMapDimension, individualMapDimension);
tile[index].collision = collision;
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadMap() {
try {
InputStream is = new FileInputStream(new File(filePath));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int col = 0;
int row = 0;
while (col < dimensions && row < dimensions) {
String line = br.readLine();
while (col < dimensions) {
String numbers[] = line.split(" ");
int num = Integer.parseInt(numbers[col]);
tileNum[col][row] = num;
col++;
if (col == dimensions) {
col = 0;
row++;
if (row == dimensions) {
break;
}
}
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
System.out.println("g initialized");
super.paintComponent(g);
System.out.println("g initialized");
draw(g);
}
public void draw(Graphics g) {
int mapCol = 0;
int mapRow = 0;
while (mapCol < dimensions && mapCol < dimensions) {
int currentTileNum = tileNum[mapCol][mapRow];
int mapX = mapCol * individualMapDimension + 1;
int mapY = mapRow * individualMapDimension + 1;
int maxMapX = 1800;
int maxMapY = 750;
if (mapX + individualMapDimension > 0 &&
mapX < maxMapX && mapY +
individualMapDimension > 0
&& mapY < maxMapY) {
g.drawImage(tile[currentTileNum].image, mapX, mapY, null);
System.out.println("drawing");
}
mapCol++;
if (mapCol == dimensions) {
mapCol = 0;
mapRow++;
if (mapRow == dimensions) {
break;
}
}
}
}
private void handleClick(MouseEvent e) {
}
}
`
Bmcar 4scopes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.