Java Swing – ASSORTED APPLICATIONS
TOPIC 01 – MINIMAP

 

 

LESSON NOTE

 

 

Lessons in this unit require that you study the code to fully understand how things work. While some explanations may be provided, they are more examples than true lessons.

 

MINIMAP

 

This lesson will show you how to create a gridded Minimap JPanel that has a grid and a controllable arrow. 

 

DEMO AND BRIEF EXPLANATION

 

                               

 

CODE

 

public class DisplayApp extends JFrame implements KeyListener

{

      public MiniMapPanel mmp;

     

      public DisplayApp()

      {

     

       Container cp = this.getContentPane();

       cp.setLayout(new FlowLayout());

     

     

       mmp = new MiniMapPanel(20,20,201,201,0,5,180);

       mmp.setBorder(BorderFactory.createLineBorder(Color.black));

       cp.add(mmp);

 

       this.setTitle("Game");

       this.pack();

       this.setVisible(true);  

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     

       this.addKeyListener(this);

      }

     

      public static void main(String[] args)

      {

            DisplayApp da = new DisplayApp();

      }

 

      public void keyPressed(KeyEvent e)

      {

            if (e.getKeyCode() == 38) //up

            {

                  mmp.moveForward();

                  System.out.println("38");

            }

            else if (e.getKeyCode() == 40) //down

            {

                  mmp.moveBack();

                  System.out.println("40");

            }

            else if (e.getKeyCode() == 39)  //right

            {

                  mmp.turnRight();

                  System.out.println("39");

            }

            else if (e.getKeyCode() == 37)

            {

                  mmp.turnLeft();

                  System.out.println("37");

            }

      }

 

      public void keyReleased(KeyEvent e)

      {

           

      }

 

      public void keyTyped(KeyEvent e)

      {

           

      }

}

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();

      }

     

      //=======================================================

      //=======================================================

      //=======================================================

}