Package de.axxeed.animosy.gui

Source Code of de.axxeed.animosy.gui.Board

package de.axxeed.animosy.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.JCheckBoxMenuItem;

import org.apache.log4j.Logger;

import de.axxeed.animosy.model.Detective;
import de.axxeed.animosy.model.Fugitive;
import de.axxeed.animosy.model.Game;
import de.axxeed.animosy.model.Manager;
import de.axxeed.animosy.tools.FileHandler;

public class Board {

  /**
   *
   */
  private static Logger log = Logger.getLogger(Board.class);
  private static BufferedImage mapImageStatic = null;
  private BufferedImage mapImage = null;
  private boolean hasChanged = false;
  private boolean showMrX = false;
    static private Point pos[] = null;

    private int pulseState = 0;
    private boolean pulseOut = true;
    private static final int MAX_PULSE = 10;

  private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  private GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();

  public Board() {
    if(mapImageStatic==null) mapImageStatic = loadMapImage();
    readMapPositions();
    hasChanged = true;
    mapImage = getImage();
  }

  public BufferedImage getImage() {
    if(!hasChanged) {
      return mapImage;
    }
    else {
      // log.debug("Redrawing map...");
//      long ts = System.currentTimeMillis();

      int transparency = mapImageStatic.getColorModel().getTransparency();
      BufferedImage copy = gc.createCompatibleImage(mapImageStatic.getWidth(), mapImageStatic.getHeight(), transparency);
      Graphics2D g2d = copy.createGraphics();
      g2d.drawImage(mapImageStatic,0,0,null);

        g2d.setStroke(new BasicStroke(3f));
        Game game = Manager.getGame();
       
        Fugitive fugitive = game.getBoard().getMrX();
//        // if(fugitive!=null && showMrX) {
        if(fugitive!=null && ( Manager.getOptions().isCheat() || game.isCheckPoint() ) && game.getState() != Game.NO_GAME) {
          if(fugitive.getPrevPos().length>game.getCurrentMove()) {
              int pp = fugitive.getPosition().getPosition();
              g2d.setColor(new Color(255,255,255));
              g2d.fillRect(pos[pp].x, pos[pp].y-12, 30, 13);
              g2d.setColor(Color.BLACK);
              g2d.drawOval(pos[pp].x, pos[pp].y, 30, 30);
              g2d.drawString("X-"+pp, pos[pp].x, pos[pp].y);
          }
          else {
            log.debug("MrX hasn't moved yet!");
          }
        }
        else {
        // log.debug("don't draw MrX...");
        }
        Detective[] detectives = game.getBoard().getDetectives();
        if(detectives != null && game.getState() != Game.NO_GAME) {
          int cd = game.getActiveDetectiveIndex();
            for(int i=0;i<detectives.length;i++) {
                int pp = detectives[i].getPosition().getPosition();
                if(i==cd) {
                  //g2d.setColor(new Color(255,255,255));
                  //g2d.fillRect(pos[pp].x, pos[pp].y-12, 30, 13);
                  g2d.setColor(Detective.getColor(i));
                  //g2d.drawString(""+(i+1)+":"+pp, pos[pp].x, pos[pp].y);
                  g2d.drawOval(pos[pp].x-pulseState, pos[pp].y-pulseState, 30+pulseState*2, 30+pulseState*2);
                }
                else if(game.getState()==Game.WIN_DET && pp==game.getBoard().getMrX().getPosition().getPosition()) {
                  if(pulseOut) {
//                    g2d.setColor(Detective.getColor(i));
//                    g2d.drawOval(pos[pp].x, pos[pp].y, 30, 30);
                    pulseOut = false;
                  }
                  else {
                      g2d.setStroke(new BasicStroke(10f));
                    g2d.setColor(Detective.getColor(i));
                    g2d.drawOval(pos[pp].x, pos[pp].y, 30, 30);
                    pulseOut = true;
                  g2d.setStroke(new BasicStroke(3f));
                  }
                }
                else {
                  //g2d.setColor(new Color(255,255,255));
                  //g2d.fillRect(pos[pp].x, pos[pp].y-12, 30, 13);
                  g2d.setColor(Detective.getColor(i));
                  //g2d.drawString(""+(i+1)+":"+pp, pos[pp].x, pos[pp].y);
                  g2d.drawOval(pos[pp].x, pos[pp].y, 30, 30);
                }
            }
        }
        else {
        log.debug("no detectives to draw...");
        }
       
        // MrX Tracker
      if(((JCheckBoxMenuItem) PanelRepository.get(PanelRepository.TRACKER_ITEM)).isSelected()) {
        if(game.getTracker()!=null) {
              g2d.setStroke(new BasicStroke(2f));
          for(int n : game.getTracker().getPotentialNodes()) {
                g2d.setColor(Color.WHITE);
                g2d.drawOval(pos[n].x, pos[n].y, 30, 30);
          }
        }
        else {
          log.debug("No tracker found...");
        }
      }
     

     
      g2d.dispose();
      mapImage = copy;
      hasChanged = false;
      // log.debug("image      : "+(System.currentTimeMillis()-ts)+"ms");

    }
    return mapImage;
  }
 
  public int getPos(int x, int y) {
    for(int i=0;i<pos.length;i++) {
      Point p = pos[i];
      if(x<p.x+40 && x>p.x && y<p.y+40 && y>p.y)
        return i;
    }
    return -1;
  }
 
  public boolean doShowMrX() {
    return showMrX;
  }

  public void setShowMrX(boolean showMrX) {
    this.showMrX = showMrX;
  }

  private BufferedImage loadMapImage() {
    URL mapURL = ClassLoader.getSystemResource("de/axxeed/animosy/map.jpg");
    if (mapURL==null) throw new RuntimeException("Error in loading board image - mapURL is null!");
    // String l_filename = IMAGE_DIR + File.separator + filename;
    log.info("Loading board image <"+mapURL.getFile()+">");
    try {
      BufferedImage bi = ImageIO.read(mapURL);
      return bi;
//      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//      GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
//      int transparency = bi.getColorModel().getTransparency();
//      BufferedImage copy = gc.createCompatibleImage(bi.getWidth(), bi.getHeight(), transparency);
//      Graphics2D g2d = copy.createGraphics();
//      g2d.drawImage(bi,0,0,null);
//      g2d.dispose();
//      return copy;
    }
    catch (IOException e) {
      log.warn("Error loading image file <"+mapURL.getFile()+">", e);
    }
    return null;
  }
 
 
    /**This method reads the text file which contains the map*/
    private void readMapPositions() {
        // file with graphics information
        try {
        FileHandler map = new FileHandler("de/axxeed/animosy/scotmapg.txt");
        log.info("Loading board info <"+map.getInfo()+">");
       
      String buffer=map.readLine();
      StringTokenizer token;
      token=new StringTokenizer(buffer);
      int numPos=Integer.parseInt(token.nextToken());
      pos=new Point[numPos];
      for(int i=0;i<numPos;i++)
      {
          pos[i]=new Point(0,0);
        buffer=map.readLine();
        // System.out.println(i+": "+buffer);
        token=new StringTokenizer(buffer);
        int posNo=Integer.parseInt(token.nextToken());
        int posX=Integer.parseInt(token.nextToken());
        int posY=Integer.parseInt(token.nextToken());
        if(i!=posNo) {
          System.out.println("WARNING! Pos "+posNo+" not equal to counter "+i);
        }
        else {
          pos[posNo] = new Point(posX-15, posY-15);
        }
      }
      }
        catch(Exception e) {
      e.printStackTrace();
          System.out.println("Error in file reading. Exitting!!! "+e);
      System.exit(1);
      }

    }
   
    public void setChanged() {
      hasChanged = true;
    }
   
    public void pulse() {
        if(pulseOut) {
          pulseState+=1;
          if(pulseState>MAX_PULSE) {
            pulseState = MAX_PULSE;
            pulseOut = false;
          }
        }
        else {
          pulseState-=1;
          if(pulseState<0) {
            pulseState = 0;
            pulseOut = true;
          }
        }
    }

}
TOP

Related Classes of de.axxeed.animosy.gui.Board

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.