Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.17 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.JLabel;
  4. import javax.swing.ImageIcon;
  5. import javax.imageio.*;
  6. import java.io.*;
  7. import java.awt.Toolkit;
  8. import java.awt.Graphics;
  9. import java.awt.Color;
  10. import java.awt.Image;
  11. import java.awt.Font;
  12. import java.awt.image.BufferedImage;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseEvent;
  15. import java.io.File;
  16.  
  17. /* [MatrixDisplayWithMouse.java]
  18.  * A small program showing how to use the MatrixDisplayWithMouse class
  19.  *  NOTE - A lot of things to fix here!
  20.  * @author Mangat
  21.  */
  22.  
  23.  
  24. class MatrixDisplayWithMouse extends JFrame {
  25.  
  26.   int maxX,maxY, GridToScreenRatio;
  27.   Organism[][] matrix;
  28.   private static int clickX, clickY;
  29.   private boolean clicked;
  30.   int numOfHumans, numOfPlants, numOfZombies;
  31.   BufferedImage manSprite, womanSprite, girlSprite, boySprite, zombieSprite, plantSprite;
  32.   int turns=0;
  33.  
  34.   MatrixDisplayWithMouse(String title, Organism[][] matrix) {
  35.     super(title);
  36.    
  37.     this.matrix = matrix;
  38.     maxX = Toolkit.getDefaultToolkit().getScreenSize().width;
  39.     maxY = Toolkit.getDefaultToolkit().getScreenSize().height;
  40.     GridToScreenRatio = maxY / (matrix.length+1);  //ratio to fit in screen as square map
  41.    
  42.     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.     this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
  44.    
  45.     this.add(new MatrixPanel());
  46.    
  47.     try{
  48.       manSprite = ImageIO.read(new File("man.png"));
  49.       womanSprite = ImageIO.read(new File("woman.png"));
  50.       girlSprite = ImageIO.read(new File("girl.png"));
  51.       boySprite = ImageIO.read(new File("boy.png"));
  52.       zombieSprite = ImageIO.read(new File("zombieman.png"));
  53.       plantSprite = ImageIO.read(new File("plant.png"));
  54.     }catch(IOException e){
  55.       e.printStackTrace();
  56.     }
  57.    
  58.     this.setVisible(true);
  59.   }
  60.  
  61.    public void refresh() {
  62.     this.repaint();
  63.    }
  64.    public int getX(){
  65.      return clickX;
  66.    }
  67.    public int getY(){
  68.      return clickY;
  69.    }
  70.    public boolean getClicked(){
  71.      return clicked;
  72.    }
  73.    public void setClicked(){
  74.      clicked = false;
  75.    }
  76.    public void increaseTurns(){
  77.      turns++;
  78.    }
  79.   //Inner Class
  80.   class MatrixPanel extends JPanel {
  81.     MatrixPanel() {
  82.      
  83.       addMouseListener(new MatrixPanelMouseListener());
  84.     }
  85.    
  86.     public void paintComponent(Graphics g){  
  87.      
  88.       super.repaint();
  89.      
  90.       numOfHumans=0;
  91.       numOfPlants=0;
  92.       numOfZombies=0;
  93.      
  94.       setDoubleBuffered(true);
  95.       g.setColor(Color.BLACK);
  96.       g.drawOval(50, 50, 50, 50);
  97.  
  98.       for(int i=0; i<matrix.length; i++){
  99.         for(int j=0; j<matrix[0].length; j++){
  100.           if(matrix[i][j]!=null){
  101.             if(matrix[i][j] instanceof Human){
  102.               numOfHumans++;
  103.             }else if(matrix[i][j] instanceof Zombie){
  104.               numOfZombies++;
  105.             }else if(matrix[i][j] instanceof Plant){
  106.               numOfPlants++;
  107.             }
  108.           }
  109.         }
  110.       }
  111.        for(int i = 0; i<matrix.length;i=i+1)  {
  112.         for(int j = 0; j<matrix[0].length;j=j+1)  {
  113.           BufferedImage image = null;
  114.           g.setColor(Color.LIGHT_GRAY);
  115.           /*
  116.           if (matrix[i][j]==1)    //This block can be changed to match character-color pairs
  117.             g.setColor(Color.RED);
  118.           else if (matrix[i][j]==2)
  119.             g.setColor(Color.BLUE);
  120.           else
  121.             g.setColor(Color.GREEN);
  122.           */
  123.    
  124.           if((matrix[i][j] instanceof AdultHuman) && (((MovingOrganism)matrix[i][j]).getGender()==0)){
  125.             image = manSprite;
  126.           }else if((matrix[i][j] instanceof AdultHuman) && (((MovingOrganism)matrix[i][j]).getGender()==1)){
  127.             image = womanSprite;
  128.           }else if(matrix[i][j] instanceof Zombie){
  129.             image = zombieSprite;
  130.           }else if(matrix[i][j] instanceof Plant){
  131.             image = plantSprite;
  132.           }else if((matrix[i][j] instanceof BabyHuman) && (((MovingOrganism)matrix[i][j]).getGender()==0)){
  133.             image = boySprite;
  134.           }else if((matrix[i][j] instanceof BabyHuman) && (((MovingOrganism)matrix[i][j]).getGender()==1)){
  135.             image = girlSprite;
  136.           }else{
  137.             g.setColor(Color.LIGHT_GRAY);
  138.           }
  139.          
  140.           g.fillRect(j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio);
  141.           if(image!=null){
  142.             g.drawImage(image, j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio, null);
  143.           }else{
  144.             g.fillRect(j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio);
  145.           }
  146.           g.setColor(Color.BLACK);
  147.           g.drawRect(j*GridToScreenRatio, i*GridToScreenRatio, GridToScreenRatio, GridToScreenRatio);
  148.         }
  149.       }
  150.        g.setFont(new Font(g.getFont().getFontName(), Font.PLAIN, 20));
  151.       g.drawString("Number of Humans: "+numOfHumans, matrix[0].length*GridToScreenRatio+100,100);
  152.       g.drawString("Number of Plants: "+numOfPlants, matrix[0].length*GridToScreenRatio+100,200);
  153.       g.drawString("Number of Zombies: "+numOfZombies, matrix[0].length*GridToScreenRatio+100,300);
  154.       g.drawString("Turns: "+turns, matrix[0].length*GridToScreenRatio+100,400);
  155.     }
  156.   }
  157.  
  158.  
  159.   //Mouse Listener
  160.   class MatrixPanelMouseListener implements MouseListener{
  161.      //Mouse Listner Stuff
  162.    public void mousePressed(MouseEvent e) {
  163.        //System.out.println("Mouse pressed; # of clicks: " + e.getClickCount());
  164.        //System.out.println("x: " + e.getPoint().x + ",y: " + e.getPoint().y);
  165.        clickX = e.getPoint().y/GridToScreenRatio; clickY = e.getPoint().x/GridToScreenRatio;
  166.        clicked=true;
  167.    }
  168.  
  169.     public void mouseReleased(MouseEvent e) {
  170.       //System.out.println("Mouse released; # of clicks: " + e.getClickCount());
  171.     }
  172.  
  173.     public void mouseEntered(MouseEvent e) {
  174.        //System.out.println("Mouse entered");
  175.     }
  176.  
  177.     public void mouseExited(MouseEvent e) {
  178.        //System.out.println("Mouse exited");
  179.     }
  180.  
  181.     public void mouseClicked(MouseEvent e) {
  182.        //System.out.println("Mouse clicked (# of clicks: "+ e.getClickCount() + ")");
  183.     }
  184.   }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement