Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JLabel;
- import javax.swing.ImageIcon;
- import javax.imageio.*;
- import java.io.*;
- import java.awt.Toolkit;
- import java.awt.Graphics;
- import java.awt.Color;
- import java.awt.Image;
- import java.awt.Font;
- import java.awt.image.BufferedImage;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseEvent;
- import java.io.File;
- /* [MatrixDisplayWithMouse.java]
- * A small program showing how to use the MatrixDisplayWithMouse class
- * NOTE - A lot of things to fix here!
- * @author Mangat
- */
- class MatrixDisplayWithMouse extends JFrame {
- int maxX,maxY, GridToScreenRatio;
- Organism[][] matrix;
- private static int clickX, clickY;
- private boolean clicked;
- int numOfHumans, numOfPlants, numOfZombies;
- BufferedImage manSprite, womanSprite, girlSprite, boySprite, zombieSprite, plantSprite;
- int turns=0;
- MatrixDisplayWithMouse(String title, Organism[][] matrix) {
- super(title);
- this.matrix = matrix;
- maxX = Toolkit.getDefaultToolkit().getScreenSize().width;
- maxY = Toolkit.getDefaultToolkit().getScreenSize().height;
- GridToScreenRatio = maxY / (matrix.length+1); //ratio to fit in screen as square map
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
- this.add(new MatrixPanel());
- try{
- manSprite = ImageIO.read(new File("man.png"));
- womanSprite = ImageIO.read(new File("woman.png"));
- girlSprite = ImageIO.read(new File("girl.png"));
- boySprite = ImageIO.read(new File("boy.png"));
- zombieSprite = ImageIO.read(new File("zombieman.png"));
- plantSprite = ImageIO.read(new File("plant.png"));
- }catch(IOException e){
- e.printStackTrace();
- }
- this.setVisible(true);
- }
- public void refresh() {
- this.repaint();
- }
- public int getX(){
- return clickX;
- }
- public int getY(){
- return clickY;
- }
- public boolean getClicked(){
- return clicked;
- }
- public void setClicked(){
- clicked = false;
- }
- public void increaseTurns(){
- turns++;
- }
- //Inner Class
- class MatrixPanel extends JPanel {
- MatrixPanel() {
- addMouseListener(new MatrixPanelMouseListener());
- }
- public void paintComponent(Graphics g){
- super.repaint();
- numOfHumans=0;
- numOfPlants=0;
- numOfZombies=0;
- setDoubleBuffered(true);
- g.setColor(Color.BLACK);
- g.drawOval(50, 50, 50, 50);
- for(int i=0; i<matrix.length; i++){
- for(int j=0; j<matrix[0].length; j++){
- if(matrix[i][j]!=null){
- if(matrix[i][j] instanceof Human){
- numOfHumans++;
- }else if(matrix[i][j] instanceof Zombie){
- numOfZombies++;
- }else if(matrix[i][j] instanceof Plant){
- numOfPlants++;
- }
- }
- }
- }
- for(int i = 0; i<matrix.length;i=i+1) {
- for(int j = 0; j<matrix[0].length;j=j+1) {
- BufferedImage image = null;
- g.setColor(Color.LIGHT_GRAY);
- /*
- if (matrix[i][j]==1) //This block can be changed to match character-color pairs
- g.setColor(Color.RED);
- else if (matrix[i][j]==2)
- g.setColor(Color.BLUE);
- else
- g.setColor(Color.GREEN);
- */
- if((matrix[i][j] instanceof AdultHuman) && (((MovingOrganism)matrix[i][j]).getGender()==0)){
- image = manSprite;
- }else if((matrix[i][j] instanceof AdultHuman) && (((MovingOrganism)matrix[i][j]).getGender()==1)){
- image = womanSprite;
- }else if(matrix[i][j] instanceof Zombie){
- image = zombieSprite;
- }else if(matrix[i][j] instanceof Plant){
- image = plantSprite;
- }else if((matrix[i][j] instanceof BabyHuman) && (((MovingOrganism)matrix[i][j]).getGender()==0)){
- image = boySprite;
- }else if((matrix[i][j] instanceof BabyHuman) && (((MovingOrganism)matrix[i][j]).getGender()==1)){
- image = girlSprite;
- }else{
- g.setColor(Color.LIGHT_GRAY);
- }
- g.fillRect(j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio);
- if(image!=null){
- g.drawImage(image, j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio, null);
- }else{
- g.fillRect(j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio);
- }
- g.setColor(Color.BLACK);
- g.drawRect(j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio);
- }
- }
- g.setFont(new Font(g.getFont().getFontName(), Font.PLAIN, 20));
- g.drawString("Number of Humans: "+numOfHumans, matrix[0].length*GridToScreenRatio+100,100);
- g.drawString("Number of Plants: "+numOfPlants, matrix[0].length*GridToScreenRatio+100,200);
- g.drawString("Number of Zombies: "+numOfZombies, matrix[0].length*GridToScreenRatio+100,300);
- g.drawString("Turns: "+turns, matrix[0].length*GridToScreenRatio+100,400);
- }
- }
- //Mouse Listener
- class MatrixPanelMouseListener implements MouseListener{
- //Mouse Listner Stuff
- public void mousePressed(MouseEvent e) {
- //System.out.println("Mouse pressed; # of clicks: " + e.getClickCount());
- //System.out.println("x: " + e.getPoint().x + ",y: " + e.getPoint().y);
- clickX = e.getPoint().y/GridToScreenRatio; clickY = e.getPoint().x/GridToScreenRatio;
- clicked=true;
- }
- public void mouseReleased(MouseEvent e) {
- //System.out.println("Mouse released; # of clicks: " + e.getClickCount());
- }
- public void mouseEntered(MouseEvent e) {
- //System.out.println("Mouse entered");
- }
- public void mouseExited(MouseEvent e) {
- //System.out.println("Mouse exited");
- }
- public void mouseClicked(MouseEvent e) {
- //System.out.println("Mouse clicked (# of clicks: "+ e.getClickCount() + ")");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement