Commit 9e71d07e authored by Lambert Patrick's avatar Lambert Patrick
Browse files

new BN observer

bug fixed in mousedMoved
parent 25611443
package environment;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
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.filters.NoTransientField;
import fr.cemagref.ohoui.swing.OhOUI;
import fr.cemagref.ohoui.swing.OhOUIDialog;
import fr.cemagref.simaqualife.kernel.util.TransientParameters;
import fr.cemagref.simaqualife.kernel.util.TransientParameters.InitTransientParameters;
import fr.cemagref.simaqualife.pilot.Pilot;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
......@@ -26,14 +13,28 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
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.filters.NoTransientField;
import fr.cemagref.ohoui.swing.OhOUI;
import fr.cemagref.ohoui.swing.OhOUIDialog;
import fr.cemagref.simaqualife.kernel.util.TransientParameters;
import fr.cemagref.simaqualife.pilot.Pilot;
import species.DiadromousFishGroup;
@SuppressWarnings("serial")
public class BasinNetworkObserverNEA extends ObserverListener implements Configurable,
Drawable, MouseMotionListener {
public class BasinNetworkObserverNEA extends ObserverListener implements Configurable, Drawable, MouseMotionListener {
private String title;
private double threshold = 13000000.;
......@@ -54,28 +55,35 @@ Drawable, MouseMotionListener {
protected transient Pilot pilot;
// use to draw objects ( basins andt continent)
private transient double minX, minY, rangeX, rangeY;
// use to draw objects ( basins and continent)
private transient double minX, minY, maxX, maxY, rangeX, rangeY;
// layer to be displayed
private transient JComponent display;
private transient JLabel label;
// basin under the mouse
private transient Basin basinUnderMouse;
@Override
public void addObservable(ObservablesHandler arg0) {
// nothing to do
}
@Override
public void close() {
// nothing to do
}
@Override
public void init() {
// nothing to do
}
@TransientParameters.InitTransientParameters
public void init(Pilot pilot) {
this.pilot = pilot;
......@@ -106,39 +114,49 @@ Drawable, MouseMotionListener {
time = new Time();
}
@Override
public void valueChanged(ObservablesHandler arg0, Object arg1, long arg2) {
display.repaint();
//update the label
String txt = Long.valueOf((time.getYear(pilot))).toString() + (" ")
+ time.getSeason(pilot).toString();
// update the label
String txt = Long.valueOf((time.getYear(pilot))).toString() + (" ") + time.getSeason(pilot).toString();
label.setText(txt);
if (basinUnderMouse != null) {
txt += (basinUnderMouse.getName() + " ");
for (DiadromousFishGroup group : basinUnderMouse.getGroups()) {
txt += group.getName() + "=" + basinUnderMouse.getEffective(group) + " in "
+ basinUnderMouse.getSuperFishNumber(group) + " SI ";
}
}
label.setText(txt);
display.repaint();
}
@Override
public void mouseDragged(MouseEvent arg0) {
// nothing to do
}
@Override
public void mouseMoved(MouseEvent e) {
int x = (e.getY());
int y = (e.getX());
String txt = (Long.valueOf(time.getYear(pilot))).toString() + (" ")
+ time.getSeason(pilot).toString() + " - " ;
int x = (e.getX());
int y = (e.getY());
String txt = (Long.valueOf(time.getYear(pilot))).toString() + (" ") + time.getSeason(pilot).toString() + " - ";
// identify the basin under the mouse position and enrich the label
Basin basin;
// Basin basin;
for (Shape shape : shapeBasinMap.keySet()) {
if (shape.contains(x, y)) {
basin = shapeBasinMap.get(shape);
txt += (basin.getName() + " ");
basinUnderMouse = shapeBasinMap.get(shape);
for (DiadromousFishGroup group : basin.getGroups()) {
txt += group.getName() + "=" + basin.getEffective(group) + " in "
+ basin.getSuperFishNumber(group) + " SI ";
txt += (basinUnderMouse.getName() + " ");
for (DiadromousFishGroup group : basinUnderMouse.getGroups()) {
txt += group.getName() + "=" + basinUnderMouse.getEffective(group) + " in "
+ basinUnderMouse.getSuperFishNumber(group) + " SI ";
}
break;
}
......@@ -146,40 +164,29 @@ Drawable, MouseMotionListener {
label.setText(txt);
}
@Override
public JComponent getDisplay() {
//ASK why here and not in inittransient parameter
// compute min and max of x and y
// ASK why here and not in inittransient parameter
// compute min and max of x and y
/*
* double maxX, maxY;
maxX = maxY = Double.NEGATIVE_INFINITY;
minX = minY = Double.POSITIVE_INFINITY;
bn = (BasinNetwork) pilot.getAquaticWorld().getEnvironment();
for (Basin basin : bn.getBasins()) {
// take the opposite of y coordinates because positive values in
// a panel are to the south
minX = Math.min(minX, basin.getShape().getBounds2D().getMinX());
maxX = Math.max(maxX, basin.getShape().getBounds2D().getMaxX());
minY = Math.min(minY, basin.getShape().getBounds2D().getMinY());
maxY = Math.max(maxY, basin.getShape().getBounds2D().getMaxY());
// compute the range with a small margin
rangeX = maxX - minX; // need to calculate the margin
minX -= 0.02 * rangeX;
maxX += 0.02 * rangeX;
rangeX = maxX - minX;
rangeY = maxY - minY;
minY -= 0.02 * rangeY;
maxY += 0.02 * rangeY;
rangeY = maxY - minY;
}*/
* double maxX, maxY;
*
* maxX = maxY = Double.NEGATIVE_INFINITY; minX = minY = Double.POSITIVE_INFINITY;
*
* bn = (BasinNetwork) pilot.getAquaticWorld().getEnvironment(); for (Basin basin : bn.getBasins()) { // take
* the opposite of y coordinates because positive values in // a panel are to the south minX = Math.min(minX,
* basin.getShape().getBounds2D().getMinX()); maxX = Math.max(maxX, basin.getShape().getBounds2D().getMaxX());
* minY = Math.min(minY, basin.getShape().getBounds2D().getMinY()); maxY = Math.max(maxY,
* basin.getShape().getBounds2D().getMaxY());
*
* // compute the range with a small margin rangeX = maxX - minX; // need to calculate the margin minX -= 0.02 *
* rangeX; maxX += 0.02 * rangeX; rangeX = maxX - minX;
*
* rangeY = maxY - minY; minY -= 0.02 * rangeY; maxY += 0.02 * rangeY; rangeY = maxY - minY; }
*/
if (mapContinent == null) {
double maxX, maxY;
maxX = maxY = Double.NEGATIVE_INFINITY;
minX = minY = Double.POSITIVE_INFINITY;
......@@ -193,28 +200,32 @@ Drawable, MouseMotionListener {
rangeX = maxX - minX;
rangeY = maxY - minY;
}
//System.out.println("ranges of network for display: "+rangeX+" "+rangeY);
// System.out.println("ranges of network for display: "+rangeX+"
// "+rangeY);
return display;
}
@Override
public String getTitle() {
return title;
}
@Override
public void configure() {
OhOUIDialog dialog = OhOUI
.getDialog(null, this, new NoTransientField());
OhOUIDialog dialog = OhOUI.getDialog(null, this, new NoTransientField());
dialog.setSize(new Dimension(500, 500));
dialog.setVisible(true);
display.repaint();
}
public void disable() {
display.setVisible(false);
}
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver())).toXML(new BasinNetworkObserverNEA()));
}
......@@ -224,13 +235,15 @@ Drawable, MouseMotionListener {
@Override
protected synchronized void paintComponent(Graphics g) {
// affine transformation to resize the drawing according to the window size
// affine transformation to resize the drawing according to the
// window size
double W = this.getWidth();
double H = this.getHeight();
double scaling = Math.min(W / rangeX, H / rangeY);
AffineTransform af = new AffineTransform(scaling, 0., 0.,
-scaling, - minX * scaling, H + minY * scaling);
//System.out.println(af.toString());
// AffineTransform af = new AffineTransform(scaling, 0., 0., -scaling, -minX * scaling, H + minY * scaling);
AffineTransform af = new AffineTransform(scaling, 0., 0., -scaling, -minX * scaling, maxY * scaling);
// System.out.println(af.toString());
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2)); // define the line
......@@ -241,30 +254,31 @@ Drawable, MouseMotionListener {
this.paintComponents(g);
g.setColor(Color.BLUE);
int side =(int) ((W<H? W:H) * 1.05);
int side = (int) ((W < H ? W : H) * 1.00);
g.fillRect(0, 0, side, side);
this.paintComponents(g);
// draw the continent
g.setColor(Color.GRAY);
for (Path2D.Double path : bn.getMapContinent().values() ) {
Path2D.Double displayContinent = (Path2D.Double) path.createTransformedShape(af);
//g2d.draw(displayContinent);
for (Path2D.Double path : bn.getMapContinent().values()) {
Path2D.Double displayContinent = (Path2D.Double) path.createTransformedShape(af);
// g2d.draw(displayContinent);
g2d.fill(displayContinent);
}
// draw the legend
colorScaleEnum.getScale().drawLegend(g2d, threshold);
// draw the basins and fill the map for mouse detection
// draw the basins and fill the map for mouse detection
shapeBasinMap.clear();// to be used by the mouseMoved()
double abundance;
for (Basin basin : bn.getBasins()) {
// draw each basin
Path2D.Double displayShape
= (Path2D.Double) basin.getShape().createTransformedShape(af);
//System.out.println( basin.getName() +" "+ displayShape.getBounds2D().toString());
shapeBasinMap.put(displayShape, basin); // to be used by the mouseMoved()
Path2D.Double displayShape = (Path2D.Double) basin.getShape().createTransformedShape(af);
// to be used by the mouseMoved()
shapeBasinMap.put(displayShape, basin);
// draw the shape with a color according to its type
if (basin instanceof RiverBasin) {
......@@ -276,17 +290,21 @@ Drawable, MouseMotionListener {
}
g2d.draw(displayShape);
// fill the shape with a color according to its fish abundance (FROM ALL THE FISH GROUP)
// fill the shape with a color according to its fish abundance
// (FROM ALL THE FISH GROUP)
abundance = 0;
for (DiadromousFishGroup group : basin.getGroups()) {
abundance += basin.getEffective(group);
}
//System.out.println(basin.getName()+ "-->"+abundance);
// System.out.println(basin.getName()+ "-->"+abundance);
if (abundance == 0.) {
g.setColor(Color.WHITE);
} else {
g.setColor(colorScaleEnum.getScale().getColor(Math.min(1., abundance / threshold)));
//g.setColor(new Color(0.8f * (1f - (float) Math.min(1., abundance / threshold)), 0.8f * (1 - (float) Math.min(1., abundance / threshold)), 0.8f * (1 - (float) Math.min(1., abundance / threshold))));
// g.setColor(new Color(0.8f * (1f - (float) Math.min(1.,
// abundance / threshold)), 0.8f * (1 - (float) Math.min(1.,
// abundance / threshold)), 0.8f * (1 - (float) Math.min(1.,
// abundance / threshold))));
}
g2d.fill(displayShape);
}
......@@ -295,15 +313,15 @@ Drawable, MouseMotionListener {
public enum ColorScaleEnum {
BluesScale(new BluesScale()),
BicolorScale(new BicolorScale()),
GraysScale(new GraysScale());
BluesScale(new BluesScale()), BicolorScale(new BicolorScale()), GraysScale(new GraysScale());
private ColorScale scale;
ColorScaleEnum(ColorScale colorScale) {
scale = colorScale;
}
public ColorScale getScale() {
return scale;
}
......@@ -313,10 +331,13 @@ Drawable, MouseMotionListener {
public Color getColor(double value);
public Color getSeaBasinColor();
public Color getRiverBasinColor();
public void drawLegend(Graphics2D g2d, double threshold);
}
......@@ -327,6 +348,7 @@ Drawable, MouseMotionListener {
return new Color(0.4f + 0.45f * (1f - (float) value), 0.4f + 0.45f * (1f - (float) value), 1f);
}
@Override
public void drawLegend(Graphics2D g2d, double threshold) {
int nbLegend = 11;
......@@ -334,17 +356,19 @@ Drawable, MouseMotionListener {
float col = (float) i / (float) (nbLegend - 1);
g2d.setColor(getColor(col));
g2d.fillRect(10, 30 + 10 * (nbLegend - 1 - i), 20, 10);
double limit = Math.round((double) threshold * (double) i / (double) (nbLegend - 1) * 10.) / 10.;
double limit = Math.round(threshold * i / (nbLegend - 1) * 10.) / 10.;
g2d.setColor(Color.BLACK);
g2d.drawString(String.valueOf(limit), 32, 20 + 10 * (nbLegend + 1 - i));
}
}
@Override
public Color getSeaBasinColor() {
return new Color(0, 102, 255);
}
@Override
public Color getRiverBasinColor() {
return new Color(0, 204, 255);
......@@ -356,9 +380,11 @@ Drawable, MouseMotionListener {
@Override
public Color getColor(double value) {
return new Color(0.4f + 0.45f * (1f - (float) value), 0.4f + 0.45f * (1f - (float) value), 0.4f + 0.45f * (1f - (float) value));
return new Color(0.4f + 0.45f * (1f - (float) value), 0.4f + 0.45f * (1f - (float) value),
0.4f + 0.45f * (1f - (float) value));
}
@Override
public void drawLegend(Graphics2D g2d, double threshold) {
int nbLegend = 11;
......@@ -366,17 +392,19 @@ Drawable, MouseMotionListener {
float col = (float) i / (float) (nbLegend - 1);
g2d.setColor(getColor(col));
g2d.fillRect(10, 30 + 10 * (nbLegend - 1 - i), 20, 10);
double limit = Math.round((double) threshold * (double) i / (double) (nbLegend - 1) * 10.) / 10.;
double limit = Math.round(threshold * i / (nbLegend - 1) * 10.) / 10.;
g2d.setColor(Color.BLACK);
g2d.drawString(String.valueOf(limit), 32, 20 + 10 * (nbLegend + 1 - i));
}
}
@Override
public Color getSeaBasinColor() {
return Color.BLACK;
}
@Override
public Color getRiverBasinColor() {
return Color.BLACK;
......@@ -391,15 +419,18 @@ Drawable, MouseMotionListener {
return value > 0 ? new Color(0.6f, 0.6f, 0.6f) : Color.WHITE;
}
@Override
public void drawLegend(Graphics2D g2d, double threshold) {
}
@Override
public Color getSeaBasinColor() {
return Color.BLACK;
}
@Override
public Color getRiverBasinColor() {
return Color.BLACK;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment