Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package feAttempt;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import javax.swing.*;
- //Simple display for showing and interacting with objects in a BoundedEnv.
- //All objects in the BoundedEnv must implement the Displayable interface.
- //You will probably not need to look at this code.
- public class GridDisplay extends JComponent implements KeyListener, MouseListener
- {
- private Grid<Sprite> spriteGrid;
- private Grid<Color> colorGrid;
- private JFrame frame;
- private int lastKeyPressed;
- private Location lastLocationClicked;
- private Color lineColor;
- //create display with each cell of given dimensions
- public GridDisplay(int numRows, int numCols)
- {
- this.spriteGrid = new BoundedGrid<Sprite>(numRows, numCols);
- this.colorGrid = new BoundedGrid<Color>(numRows, numCols);
- lastKeyPressed = -1;
- lastLocationClicked = null;
- lineColor = Color.WHITE;
- for (int row = 0; row < numRows; row++)
- {
- for (int col = 0; col < numCols; col++)
- colorGrid.put(new Location(row, col), Color.GREEN);
- }
- frame = new JFrame();
- frame.setTitle("GridDisplay");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.addKeyListener(this);
- int cellWidth = 500 / numCols;
- int cellHeight = 500 / numRows;
- int cellSize = Math.min(cellWidth, cellHeight);
- setPreferredSize(new Dimension(numCols * cellSize, numRows * cellSize));
- addMouseListener(this);
- frame.getContentPane().add(this);
- frame.pack();
- frame.setVisible(true);
- }
- public void paintComponent(Graphics g)
- {
- int numRows = spriteGrid.getNumRows();
- int numCols = spriteGrid.getNumCols();
- int cellSize = getCellSize();
- for (int row = 0; row < numRows; row++)
- {
- for (int col = 0; col < numCols; col++)
- {
- Location loc = new Location(row, col);
- g.setColor(colorGrid.get(loc));
- int x = col * cellSize;
- int y = row * cellSize;
- g.fillRect(x, y, (int)cellSize, (int)cellSize);
- Sprite sprite = spriteGrid.get(loc);
- if (sprite != null)
- {
- String fileName = sprite.getImageFileName();
- if (fileName == null)
- {
- System.out.println("Sprite " + sprite + " has no image file name");
- System.exit(0);
- }
- URL url = getClass().getResource(fileName);
- if (url == null)
- {
- System.out.println("File not found: " + fileName);
- System.exit(0);
- }
- ImageIcon icon = new ImageIcon(url);
- g.drawImage(icon.getImage(), x, y, cellSize, cellSize, null);
- }
- if (lineColor != null)
- {
- g.setColor(lineColor);
- g.drawRect(x, y, cellSize, cellSize);
- }
- }
- }
- }
- public void keyPressed(KeyEvent e)
- {
- lastKeyPressed = e.getKeyCode();
- }
- public void keyReleased(KeyEvent e)
- {
- //ignore
- }
- public void keyTyped(KeyEvent e)
- {
- //ignore
- }
- public void mousePressed(MouseEvent e)
- {
- int cellSize = getCellSize();
- int row = e.getY() / cellSize;
- if (row < 0 || row >= spriteGrid.getNumRows())
- return;
- int col = e.getX() / cellSize;
- if (col < 0 || col >= spriteGrid.getNumCols())
- return;
- lastLocationClicked = new Location(row, col);
- }
- public void mouseReleased(MouseEvent e)
- {
- //ignore
- }
- public void mouseClicked(MouseEvent e)
- {
- //ignore
- }
- public void mouseEntered(MouseEvent e)
- {
- //ignore
- }
- public void mouseExited(MouseEvent e)
- {
- //ignore
- }
- public void pause(int milliseconds)
- {
- repaint();
- try
- {
- Thread.sleep(milliseconds);
- }
- catch(Exception e)
- {
- //ignore
- }
- }
- public Grid<Sprite> getSpriteGrid()
- {
- return spriteGrid;
- }
- public Grid<Color> getColorGrid()
- {
- return colorGrid;
- }
- public void showMessage(String message)
- {
- JOptionPane.showMessageDialog(this, message);
- }
- public void setTitle(String title)
- {
- frame.setTitle(title);
- }
- //returns -1 if no key pressed since last call.
- //otherwise returns the code for the last key pressed.
- public int checkLastKeyPressed()
- {
- int key = lastKeyPressed;
- lastKeyPressed = -1;
- return key;
- }
- //returns null if no location clicked since last call.
- public Location checkLastLocationClicked()
- {
- Location loc = lastLocationClicked;
- lastLocationClicked = null;
- return loc;
- }
- public void setLineColor(Color color)
- {
- lineColor = color;
- repaint();
- }
- private int getCellSize()
- {
- int cellWidth = getWidth() / spriteGrid.getNumCols();
- int cellHeight = getHeight() / spriteGrid.getNumRows();
- return Math.min(cellWidth, cellHeight);
- }
- }
Add Comment
Please, Sign In to add comment