Package de.mpi.rgblab.gui.map

Source Code of de.mpi.rgblab.gui.map.GuiMap

package de.mpi.rgblab.gui.map;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JPanel;

import de.mpi.rgblab.color.ColorModel;
import de.mpi.rgblab.color.ColorType;
import de.mpi.rgblab.data.Data;
import de.mpi.rgblab.optimizer.Optimizer;
import de.mpi.rgblab.utils.VisUtil;

public class GuiMap extends JPanel {

  // color brewer
  // 255, 255, 204; 255, 237, 160; 254, 217, 118; 254, 178, 76; 253, 141, 60;
  // 252, 78, 42; 227, 26, 28; 177, 0, 38;

  int displayDistance;
 
  Data data;
  int first = 0;
  Vector<Color> clr = new Vector<Color>();

  // Vector<Vector<Double>> pixelsMatrix = new Vector<Vector<Double>>();
  // Vector<Double> classLabels = new Vector<Double>();

  Vector<ColorModel> classColors = new Vector<ColorModel>();

  // Vector<Double> saliency = new Vector<Double>();
  // Vector<Double> visibility_score = new Vector<Double>();
  // Vector<Integer> classLabels = new Vector<Integer>();

  int threshold = 0;

  double currentVisibilityScore;
  double currentSaliency;
  double max_saliency;
  double min_saliency;
  double max_visibility_score;
  double min_visibility_score;

  // Vector<ColorModel> my_color = new Vector<ColorModel>();

  int reason = -1;
  int sector = -1;

  int last_radius;
  int last_threshold;
  Vector<ColorModel> last_classColors = new Vector<ColorModel>();

  int percent = 100;

  public void setThreshold(int t) {
    threshold = t;

  }

  public GuiMap(Data data) {
    setBackground(Color.white);
    // clr = ColorReader.getColorsAsColor(sal_vis_palette);
    // classColors = ColorReader.getColorsAsColorModel(inputColors);

    this.data = data;
  }

  // public void setSaliency(Vector<Double> saliency) {
  // this.saliency = saliency;
  // }

  public void computeVisibility(int distance) {
    int radius = VisUtil.getViewRadius(distance);
    data.computeVisibility(radius);
  }

  public void computeSaliency(int distance, int surroundAngle) {
    int radius =  VisUtil.getRadius(distance, surroundAngle);
   
    if (last_radius == radius && last_classColors.equals(classColors))
      return;

    data.computeSaliency(radius, classColors);
    // check
    last_classColors = classColors;
    last_radius = radius;
    System.out.println(classColors);

  }

  public void setColors(Vector<Color> colors) {
    classColors = arrayToColorModelVector(colors);
  }

  private Vector<ColorModel> arrayToColorModelVector(Color[] colors) {
    Vector<ColorModel> v = new Vector<ColorModel>();
    for (int i = 0; i < colors.length; i++)
      v.add(new ColorModel(colors[i].getRed(), colors[i].getGreen(),
          colors[i].getBlue(), ColorType.RGB));
    return v;
  }

  private Vector<ColorModel> arrayToColorModelVector(Vector<Color> colors) {
    Vector<ColorModel> v = new Vector<ColorModel>();
    for (int i = 0; i < colors.size(); i++)
      v.add(new ColorModel(colors.get(i).getRed(), colors.get(i)
          .getGreen(), colors.get(i).getBlue(), ColorType.RGB));
    return v;
  }

  public void setPercent(int percent) {
    this.percent = percent;
    updateUI();
  }

  /*
   * public void setPreferredSize(int n) { this.setPreferredSize(new
   * Dimension(500, 500)); updateUI(); }
   */

  /*
   * private void resizeMap(int percent) {
   * System.out.println("max screen size");
   * System.out.println(initialDimension.getHeight());
   * System.out.println(initialDimension.getWidth());
   *
   * windowSize = initialDimension.getSize(); windowSize.width = (int)
   * (windowSize.getWidth()percent/100); windowSize.height =(int)
   * (windowSize.getHeight()percent/100); //this.setSize(width, height); }
   */

  /*
   * public Dimension getMaxSize() { return initialDimension; }
   */

  @SuppressWarnings("unchecked")
  public void paintComponent(Graphics g) {

    // do not update
   
    //setBackground(Color.GRAY);

    super.paintComponent(g); // JPanel draws background
    if (reason == -3)
      return;
   
    // initialDimension = this.getSize();
    // resizeMap(20);
    // this.setSize(windowSize.height, windowSize.width);
    Dimension windowSize = this.getSize();
    if (first < 2) {

      data.setX_res((int) windowSize.getWidth());
      data.setY_res((int) windowSize.getHeight());
      data.printResolution();
      System.out.println(first);
      first++;
    }

    windowSize.width = (int) Math.round(windowSize.getWidth() * percent
        / 100);
    windowSize.height = (int) Math.round(windowSize.getHeight() * percent
        / 100);

    int offsetWidth = this.getWidth() - windowSize.width;
    offsetWidth /= 2;

    int offsetHeight = this.getHeight() - windowSize.height;
    offsetHeight /= 2;

    // Vector<Vector<Integer>> scaled_pixelsMatrix = new
    // Vector<Vector<Integer>>();
    // Numeric.scale_data_2_screen_res(pixelsMatrix, scaled_pixelsMatrix,
    // windowSize.width, windowSize.height);

    Iterator it = data.getData().iterator();
    // Iterator it_colors = classLabels.iterator();

    // int label_count=0;
    /*
     * while (it.hasNext()) {
     *
     * Vector< Double> nbh; Vector<Double> my_data_item = (Vector<Double>)
     * it.next();
     *
     * Iterator it2 = scaled_pixelsMatrix.iterator();
     *
     * while (it2.hasNext()) {
     *
     * Vector<Double> nbh_item = (Vector<Double>) it2.next();
     *
     * double dist=eucl_distance(my_data_item, nbh_item);
     *
     * if(dist < 10) {
     *
     * nbh.addElement(nbh_item);
     *
     * }
     *
     * }
     *
     * }
     */

    // draw the image
    // it = scaled_pixelsMatrix.iterator();
    // int count = 0;
    // some test for resizing the components
    // System.out.println(this.getParent().getComponent(0).getWidth());
    // System.out.println(this.getParent().getComponent(1).getHeight());
    // this.getParent().getComponent(1).setSize(this.getParent().getComponent(1).getWidth(),
    // 100);
    // System.out.println(this.getParent().getComponent(0).getHeight());
    // System.out.println(this.getParent().getComponent(1).getWidth());
    // System.out.println(this.getParent().getComponent(1).getHeight());
    // System.out.println(windowSize.height);
    int count = 0;
    // System.out.println("threshold: " + threshold);

    int x, y, l;
    while (it.hasNext()) {
      Vector<Integer> row = (Vector<Integer>) it.next();

      x = row.elementAt(0);
      y = row.elementAt(1);
      l = row.elementAt(2);

      y = windowSize.height - y;
      // set the color

      // new code - show the map with the input colors
      if (reason == -1) {

        ColorModel currentColorModel = classColors.get(l);
        Color currentColor = new Color((int) currentColorModel
            .getDimension_1(), (int) currentColorModel
            .getDimension_2(), (int) currentColorModel
            .getDimension_3());
        g.setColor(currentColor);

        // Dimension screenSize =
        // Toolkit.getDefaultToolkit().getScreenSize();

        if (sector == -1)
          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);
        else if (l == sector)
          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

      }
      // compute saliency
      else if (reason == 0) {
        currentSaliency = data.getSaliency().get(count);

        float current_color_double = (float) (this.clr.size()
            * (currentSaliency - data.getMinSaliency()) / (data
            .getMaxSaliency() - data.getMinSaliency()));
        int current_color = (int) (current_color_double);
        if (current_color >= clr.size())
          current_color = clr.size() - 1;

        g.setColor(clr.get(current_color));

        if (sector == -1)
          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

        else if (current_color == sector)
          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

      }
      // identify bad visibility
      else if (reason == 1) {
        // old code

        currentSaliency = data.getSaliency().get(count);

        if (currentSaliency > threshold) {
          ColorModel currentColorModel = classColors.get(l);
          Color currentColor = new Color((int) currentColorModel
              .getDimension_1(), (int) currentColorModel
              .getDimension_2(), (int) currentColorModel
              .getDimension_3());
          g.setColor(currentColor);

          // Dimension screenSize =
          // Toolkit.getDefaultToolkit().getScreenSize();

          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

        } else {
          g.setColor(Color.BLACK);

          // Dimension screenSize =
          // Toolkit.getDefaultToolkit().getScreenSize();

          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

        }
      }
      // compute visibility
      // V(p|cl) = Sum (Saliency(q)) for every q that belongs to the
      // neighbourhood
      else if (reason == 2) {
        currentVisibilityScore = data.getVisibility().get(count);

        int current_color = (int) (float) (clr.size()
            * (currentVisibilityScore - data.getMinVisibility()) / (data
            .getMaxVisibility() - data.getMinVisibility()));

        if (current_color >= clr.size())
          current_color = clr.size() - 1;

        // System.out.println(current_color + " " + x.intValue() + " "
        // + (windowSize.height - y.intValue()));

        g.setColor(clr.get(current_color));

        if (sector == -1)
          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

        else if (current_color == sector)
          g.drawRect(x + offsetWidth, y
              + offsetHeight, 1, 1);

      }
      count++;

    }
    // System.out.println("count:" + count);
  }

  public void update(int reason) {
    // currentSaliency = 0;
    if (reason == -2) {
      data.export();
      return;
    }

    // this.sector = sector;
    this.reason = reason;
    updateUI();

  }

  public Vector<Color> getSaliencyColors() {
    return clr;
  }

  public void setSalVisPalette(Vector<Color> c) {
    clr = c;
  }

  public void updateSector(int sector) {
    this.sector = sector;
    updateUI();
  }
 
  public void updateDisplayDistance(int distance) {
    this.displayDistance = distance;
    updateUI();
  }

  public void importData(String filename) {
    data.importData(filename);
  }

  // public void showSaliency(int saliencySector) {
  // sector = saliencySector;
  // reason = 0;
  // }
  //
  // public void showVisibility(int visibilitySector) {
  // sector = visibilitySector;
  // reason = 0;
  // }

  public void optimize(int distance, int surroundAngle) {
    int radiusView =  VisUtil.getViewRadius(distance);
    int radiusSurround =  VisUtil.getRadius(distance, surroundAngle);
    Optimizer opt = new Optimizer();
    classColors = opt.optimize(data, classColors, radiusSurround, radiusView);
    update(-1);
  }

  // reutrn 0 for ok
  // otherwise return the minimum numbers of colors needed
  public int checkInputDataComplience() {
    if (this.classColors.size() < data.getClassNumbers())
      return data.getClassNumbers();
    else
      return 0;
  }

}
TOP

Related Classes of de.mpi.rgblab.gui.map.GuiMap

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.