I’ve been trying to figure out a way to draw a map onto a JPanel for the past week or so and my current solution should work, my only problem is that I can’t figure out how to tell my paint method to paint onto my JPanel which is named gamePanel. I’ve tried looking up how to specify which JPanel to paint onto but it hasn’t yielded much information.
Small Snippit:
class Maps extends JPanel
{
int testMap[][] = new int[2][2];
boolean[][] testCollisionMap= new boolean[2][2];
int tempIntOne = 0, tempIntTwo = 0;
JPanel gamePanel;
public Maps(JPanel gamePanel)
{
this.gamePanel = gamePanel;
testMap[0][0] = 1;
testMap[0][1] = 2;
testMap[1][0] = 1;
testMap[1][1] = 2;
testCollisionMap[0][0] = false;
testCollisionMap[0][1] = true;
testCollisionMap[1][0] = false;
testCollisionMap[1][1] = true;
}
public void paint(Graphics2D g)
{
for(int i = -1; i <3; i++) //For the part (i < 1) just replace the 1 with the max number it goes to.
{
if (testMap[tempIntOne][tempIntTwo] == 1)
{
g.setColor(Color.blue);
g.fillRect(50, 50, 20, 20);
g.drawRect(50, 50, 20, 20);
}
else
{
g.setColor(Color.red);
g.fillRect(100, 100, 20, 20);
g.drawRect(100, 100, 20, 20);
}
if (tempIntTwo == 1)
{
tempIntOne++;
}
else
{
tempIntTwo++;
}
}
}
}
I don’t remember too much about painting and that from the applets section in my class but I’d assume this should work if I can somehow say what to paint onto.
~Thanks in advance.