RovkirHexus

GridDisplay

Jun 8th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. package feAttempt;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.net.*;
  6. import javax.swing.*;
  7.  
  8. //Simple display for showing and interacting with objects in a BoundedEnv.
  9. //All objects in the BoundedEnv must implement the Displayable interface.
  10. //You will probably not need to look at this code.
  11. public class GridDisplay extends JComponent implements KeyListener, MouseListener
  12. {
  13.   private Grid<Sprite> spriteGrid;
  14.   private Grid<Color> colorGrid;
  15.   private JFrame frame;
  16.   private int lastKeyPressed;
  17.   private Location lastLocationClicked;
  18.   private Color lineColor;
  19.  
  20.   //create display with each cell of given dimensions
  21.   public GridDisplay(int numRows, int numCols)
  22.   {
  23.     this.spriteGrid = new BoundedGrid<Sprite>(numRows, numCols);
  24.     this.colorGrid = new BoundedGrid<Color>(numRows, numCols);
  25.     lastKeyPressed = -1;
  26.     lastLocationClicked = null;
  27.     lineColor = Color.WHITE;
  28.    
  29.     for (int row = 0; row < numRows; row++)
  30.     {
  31.       for (int col = 0; col < numCols; col++)
  32.         colorGrid.put(new Location(row, col), Color.GREEN);
  33.     }
  34.    
  35.     frame = new JFrame();
  36.     frame.setTitle("GridDisplay");
  37.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.     frame.addKeyListener(this);
  39.    
  40.     int cellWidth = 500 / numCols;
  41.     int cellHeight = 500 / numRows;
  42.     int cellSize = Math.min(cellWidth, cellHeight);
  43.     setPreferredSize(new Dimension(numCols * cellSize, numRows * cellSize));
  44.     addMouseListener(this);
  45.     frame.getContentPane().add(this);
  46.    
  47.     frame.pack();
  48.     frame.setVisible(true);
  49.   }
  50.  
  51.   public void paintComponent(Graphics g)
  52.   {
  53.     int numRows = spriteGrid.getNumRows();
  54.     int numCols = spriteGrid.getNumCols();
  55.     int cellSize = getCellSize();
  56.    
  57.     for (int row = 0; row < numRows; row++)
  58.     {
  59.       for (int col = 0; col < numCols; col++)
  60.       {
  61.         Location loc = new Location(row, col);
  62.         g.setColor(colorGrid.get(loc));
  63.         int x = col * cellSize;
  64.         int y = row * cellSize;
  65.         g.fillRect(x, y, (int)cellSize, (int)cellSize);
  66.        
  67.         Sprite sprite = spriteGrid.get(loc);
  68.         if (sprite != null)
  69.         {
  70.           String fileName = sprite.getImageFileName();
  71.           if (fileName == null)
  72.           {
  73.             System.out.println("Sprite " + sprite + " has no image file name");
  74.             System.exit(0);
  75.           }
  76.           URL url = getClass().getResource(fileName);
  77.           if (url == null)
  78.           {
  79.             System.out.println("File not found:  " + fileName);
  80.             System.exit(0);
  81.           }
  82.           ImageIcon icon = new ImageIcon(url);
  83.           g.drawImage(icon.getImage(), x, y, cellSize, cellSize, null);
  84.         }
  85.        
  86.         if (lineColor != null)
  87.         {
  88.           g.setColor(lineColor);
  89.           g.drawRect(x, y, cellSize, cellSize);
  90.         }
  91.       }    
  92.     }
  93.   }
  94.  
  95.   public void keyPressed(KeyEvent e)
  96.   {
  97.     lastKeyPressed = e.getKeyCode();
  98.   }
  99.  
  100.   public void keyReleased(KeyEvent e)
  101.   {
  102.     //ignore
  103.   }
  104.  
  105.   public void keyTyped(KeyEvent e)
  106.   {
  107.     //ignore
  108.   }
  109.  
  110.   public void mousePressed(MouseEvent e)
  111.   {
  112.     int cellSize = getCellSize();
  113.     int row = e.getY() / cellSize;
  114.     if (row < 0 || row >= spriteGrid.getNumRows())
  115.       return;
  116.     int col = e.getX() / cellSize;
  117.     if (col < 0 || col >= spriteGrid.getNumCols())
  118.       return;
  119.     lastLocationClicked = new Location(row, col);
  120.   }
  121.  
  122.   public void mouseReleased(MouseEvent e)
  123.   {
  124.     //ignore
  125.   }
  126.  
  127.   public void mouseClicked(MouseEvent e)
  128.   {
  129.     //ignore
  130.   }
  131.  
  132.   public void mouseEntered(MouseEvent e)
  133.   {
  134.     //ignore
  135.   }
  136.  
  137.   public void mouseExited(MouseEvent e)
  138.   {
  139.     //ignore
  140.   }
  141.  
  142.   public void pause(int milliseconds)
  143.   {
  144.     repaint();
  145.     try
  146.     {
  147.       Thread.sleep(milliseconds);
  148.     }
  149.     catch(Exception e)
  150.     {
  151.       //ignore
  152.     }
  153.   }
  154.  
  155.   public Grid<Sprite> getSpriteGrid()
  156.   {
  157.     return spriteGrid;
  158.   }
  159.  
  160.   public Grid<Color> getColorGrid()
  161.   {
  162.     return colorGrid;
  163.   }
  164.  
  165.   public void showMessage(String message)
  166.   {
  167.     JOptionPane.showMessageDialog(this, message);
  168.   }
  169.  
  170.   public void setTitle(String title)
  171.   {
  172.     frame.setTitle(title);
  173.   }
  174.  
  175.   //returns -1 if no key pressed since last call.
  176.   //otherwise returns the code for the last key pressed.
  177.   public int checkLastKeyPressed()
  178.   {
  179.     int key = lastKeyPressed;
  180.     lastKeyPressed = -1;
  181.     return key;
  182.   }
  183.  
  184.   //returns null if no location clicked since last call.
  185.   public Location checkLastLocationClicked()
  186.   {
  187.     Location loc = lastLocationClicked;
  188.     lastLocationClicked = null;
  189.     return loc;
  190.   }
  191.  
  192.   public void setLineColor(Color color)
  193.   {
  194.     lineColor = color;
  195.     repaint();
  196.   }
  197.  
  198.   private int getCellSize()
  199.   {
  200.     int cellWidth = getWidth() / spriteGrid.getNumCols();
  201.     int cellHeight = getHeight() / spriteGrid.getNumRows();
  202.     return Math.min(cellWidth, cellHeight);
  203.   }
  204. }
Add Comment
Please, Sign In to add comment