public class MiniMapPanel
extends JPanel
{
public BufferedImage arrow0;
public BufferedImage arrow90;
public BufferedImage arrow180;
public BufferedImage arrow270;
public int rows;
public int cols;
public int myW;
public int myH;
public int atRow;
public int atCol;
public int direction;
//=======================================================
//=======================================================
//=======================================================
public MiniMapPanel(int gridRows, int gridCols, int width, int height, int startingRow, int startingCol, int
startingDirection)
{
rows = gridRows;
cols = gridCols;
myW = width;
myH = height;
atRow = startingRow;
atCol = startingCol;
direction = startingDirection;
this.setPreferredSize(new Dimension(myW,myH));
loadImages();
}
//=======================================================
//=======================================================
//=======================================================
public void
loadImages()
{
try
{
arrow0 = ImageIO.read(new File("arrowRight.png"));
arrow90 = ImageIO.read(new File("arrowUp.png"));
arrow180 = ImageIO.read(new File("arrowLeft.png"));
arrow270 = ImageIO.read(new File("arrowDown.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
//=======================================================
//=======================================================
//=======================================================
public void
paint(Graphics g)
{
super.paint(g);
//draw grid
int rowHeight = (myH-1) / rows;
int colWidth = (myW-1) / cols;
int minY = 0;
int maxY = rows*rowHeight;
int minX = 0;
int maxX = cols*colWidth;
//rows
int ycoord = 0;
for (int r=0;
r<=rows; r++)
{
g.drawLine(0,ycoord,maxX,ycoord);
ycoord
= ycoord + rowHeight;
}
//columns
int xcoord = 0;
for (int c=0;
c<=cols; c++)
{
g.drawLine(xcoord,0,xcoord,maxY);
xcoord
= xcoord + colWidth;
}
//draw arrow
int arrowX = atCol *
colWidth; //+ pixels to center
int arrowY = atRow *
rowHeight; //+ pixels to center
if (direction == 0)
{
g.drawImage(arrow0,arrowX,arrowY,colWidth,rowHeight,null);
}
else if (direction == 90)
{
g.drawImage(arrow90,arrowX,arrowY,colWidth,rowHeight,null);
}
else if (direction == 180)
{
g.drawImage(arrow180,arrowX,arrowY,colWidth,rowHeight,null);
}
else if (direction == 270)
{
g.drawImage(arrow270,arrowX,arrowY,colWidth,rowHeight,null);
}
}
//=======================================================
//=======================================================
//=======================================================
public boolean
moveForward()
{
boolean legalMove = true; //for now
if (legalMove)
{
if (direction == 0 && atCol<cols-1)
{
atCol++;
repaint();
return true;
}
else if (direction == 90 && atRow > 0)
{
atRow--;
repaint();
return true;
}
else if (direction == 180 && atCol>0)
{
atCol--;
repaint();
return true;
}
else if (direction == 270 && atRow<rows-1)
{
atRow++;
repaint();
return true;
}
}
return false;
}
//=======================================================
//=======================================================
//=======================================================
public boolean moveBack()
{
boolean legalMove = true; //for now
if (legalMove)
{
if (direction == 0 && atCol>0)
{
atCol--;
repaint();
return true;
}
else if (direction == 90 && atRow<rows-1)
{
atRow++;
repaint();
return true;
}
else if (direction == 180 && atCol<cols-1)
{
atCol++;
repaint();
return true;
}
else if (direction == 270 && atRow > 0)
{
atRow--;
repaint();
return true;
}
}
return false;
}
//=======================================================
//=======================================================
//=======================================================
public void turnLeft()
{
direction = direction + 90;
if (direction >= 360)
{
direction = direction - 360;
}
repaint();
}
//=======================================================
//=======================================================
//=======================================================
public void
turnRight()
{
direction = direction - 90;
if (direction < 0)
{
direction = direction + 360;
}
repaint();
}
//=======================================================
//=======================================================
//=======================================================
}
|