Movement2DWithinShapeObserver.java 9.75 KiB
package pikelake.environment;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.swing.JMapFrame;
import org.opengis.style.Style;
import fr.cemagref.observation.gui.Configurable;
import fr.cemagref.observation.gui.Drawable;
import fr.cemagref.observation.kernel.ObservablesHandler;
import fr.cemagref.observation.kernel.ObserverListener;
import fr.cemagref.ohoui.annotations.Description;
import fr.cemagref.ohoui.annotations.NoRecursive;
import fr.cemagref.ohoui.filters.NoTransientField;
import fr.cemagref.ohoui.swing.OhOUI;
import fr.cemagref.ohoui.swing.OhOUIDialog;
import fr.cemagref.simaqualife.kernel.AquaNism;
import fr.cemagref.simaqualife.kernel.AquaNismsGroup;
import fr.cemagref.simaqualife.pilot.Pilot;
@SuppressWarnings({ "serial" })
@NoRecursive
public class Movement2DWithinShapeObserver extends ObserverListener implements Configurable, Drawable {
//    private transient FileChooserField modulesChooser;
    private transient JComponent display;
   // private String generalPathS;
    private transient GeneralPath generalPath;
    private transient List<AquaNismsGroup<? extends AquaNism<? extends Point2D,?>,?>> groups;
    private String title;
    // TODO supress transient when list will be supported by AutOUI
    @Description (name="Groups colors",tooltip="Colors af aquanisms groups")
    private transient List<Color> groupsColors;
    @Description (name="Background color",tooltip="Background color")
    private Color bgColor;
    @Description (name="Pikes color",tooltip="Pikes color")
    private Color pikeColor;
    @Description (name="River color",tooltip="River color")
    private Color shapeColor;
    private int margin;
    private int sizeOfDisplayedIndividuals;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
// use for determine if generalPath must be rescaled private transient int displayWidthBak,displayHeightBak; // used for coordinates rescaling private transient double repositioningTranslateX,repositioningTranslateY; private transient double shapeOriginalWidth, shapeOriginalHeight; private transient double shapeAspectRatio; private transient double rescaleFactorWithOriginal; public Movement2DWithinShapeObserver(int margin) { this(margin,Color.BLACK,Color.BLUE); } public Movement2DWithinShapeObserver() { this(Color.BLACK,Color.BLUE); } public Movement2DWithinShapeObserver(Color bgColor,Color shapeColor) { this(0,bgColor,shapeColor); } public Movement2DWithinShapeObserver(int margin,Color bgColor,Color shapeColor) { this.margin = margin; this.bgColor = bgColor; this.shapeColor = shapeColor; } public String getTitle() { return title; } public JComponent getDisplay() { return display; } public void valueChanged(ObserverListener clObservable, Object instance, int t) { display.repaint(); } @SuppressWarnings({ "unchecked" }) public void init() { Pilot pilot = new Pilot(); display = new DisplayComponent(); display.setVisible(false); display.setDoubleBuffered(true); displayWidthBak = 0; displayHeightBak = 0; /////////////////////////// init shape //////////////////////////////////////////////////////////// File file = new File("/Users/guillaume.garbay/workspace/PikeLake/data/input/shape/hsi_BROPRINTEMPS513_5.shp"); FileDataStore store; try { store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); MapContent map = new MapContent(); map.setTitle("Bariousses"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(featureSource, (org.geotools.styling.Style) style); map.addLayer(layer); JMapFrame.showMap(map); this.generalPath = new GeneralPath((Shape) map); //this.generalPath = new GeneralPath(((AsShapeConvertible) pilot.getAquaticWorld().getEnvironment()).getShape());
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // original dimension storage this.shapeOriginalWidth = this.generalPath.getBounds().getWidth(); this.shapeOriginalHeight = this.generalPath.getBounds().getHeight(); this.shapeAspectRatio = shapeOriginalWidth / shapeOriginalHeight; // repositionning the shape to (0,0) this.repositioningTranslateX = - this.generalPath.getBounds().getX(); this.repositioningTranslateY = - this.generalPath.getBounds().getY(); this.generalPath.transform(AffineTransform.getTranslateInstance(this.margin+repositioningTranslateX,this.margin+repositioningTranslateY)); // vertical flip this.generalPath.transform(AffineTransform.getScaleInstance(1,-1)); this.generalPath.transform(AffineTransform.getTranslateInstance(0,shapeOriginalHeight)); groups = new ArrayList<AquaNismsGroup<? extends AquaNism<? extends Point2D,?>,?>>((Collection<? extends AquaNismsGroup<? extends AquaNism<? extends Point2D, ?>, ?>>)pilot.getAquaticWorld().getAquaNismsGroupsList()); if (groups!=null) { display.setVisible(true); } groupsColors = new ArrayList<Color>(); groupsColors.add(Color.RED); groupsColors.add(Color.GREEN); groupsColors.add(Color.YELLOW); groupsColors.add(Color.CYAN); groupsColors.add(Color.GRAY); display.repaint(); } public void disable() { display.setVisible(false); } @Override public void addObservable(ObservablesHandler classObservable) { // nothing to do } @Override public void valueChanged(ObservablesHandler clObservable, Object instance, long t) { display.repaint(); } public static interface AsShapeConvertible { public Shape getShape(); } public void configure() { OhOUIDialog dialog = OhOUI.getDialog(null,this,new NoTransientField()); dialog.setSize(new Dimension(480, 200)); dialog.setVisible(true); display.repaint(); } public void addObservable(ObserverListener classObservable) { // nothing to do } public void close() { // nothing to do } private class DisplayComponent extends JComponent { @Override protected synchronized void paintComponent(Graphics g) { super.paintComponents(g);
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
Graphics2D g2d = (Graphics2D)g; // determine if generalPath must be rescaled if ( (this.getWidth() != displayWidthBak) || (this.getHeight() != displayHeightBak) ) { // backup for comparaison in the next loop displayWidthBak = this.getWidth(); displayHeightBak = this.getHeight(); // compute the rescale factor with keeping the aspect ratio double rescaleFactor; // TODO calcul faux quand margin > 0 if ( ((double)displayWidthBak/displayHeightBak) < shapeAspectRatio ) { rescaleFactor = displayWidthBak / (generalPath.getBounds().getWidth()); rescaleFactorWithOriginal = displayWidthBak / shapeOriginalWidth; } else { rescaleFactor = displayHeightBak / (generalPath.getBounds().getHeight()); rescaleFactorWithOriginal = displayHeightBak / shapeOriginalHeight; } // rescale the generalPath generalPath.transform( AffineTransform.getScaleInstance(rescaleFactor,rescaleFactor) ); } // Draw Background g.setColor(bgColor); g.fillRect(0, 0, getWidth(), getHeight()); // Draw the generalPath g.setColor(shapeColor); g2d.fill(generalPath); //g.setColor(aquanismsColor); Iterator<Color> colorsIterator = groupsColors.iterator(); // Draw aquanisms for (AquaNismsGroup<? extends AquaNism<? extends Point2D,?>,?> group : groups) { if (colorsIterator.hasNext()) g.setColor(colorsIterator.next()); for (AquaNism<? extends Point2D,?> aquanism : group.getAquaNismsList()) { Rectangle rect=new Rectangle((int)((aquanism.getPosition().getX()+repositioningTranslateX) * rescaleFactorWithOriginal), (int)(generalPath.getBounds().getHeight() /*flip vertically*/ - (aquanism.getPosition().getY()+repositioningTranslateY) * rescaleFactorWithOriginal), sizeOfDisplayedIndividuals,sizeOfDisplayedIndividuals); // draw the individuals after repositioning and rescaling the position g2d.fill(rect); } } } } }