use of jmri.jmrix.rps.Region in project JMRI by JMRI.
the class RpsTrackingPanel method paint.
@Override
public void paint(Graphics g) {
// draw everything else
super.paint(g);
log.debug("paint invoked");
// Now show regions
// First, Graphics2D setup
Graphics2D g2 = (Graphics2D) g;
double xscale = this.getWidth() / (xmax - xorigin);
double yscale = this.getHeight() / (ymax - yorigin);
Stroke stroke = new BasicStroke((float) (2. / xscale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2.setStroke(stroke);
// Save the current transform
AffineTransform saveAT = g2.getTransform();
// Install the new one
currentAT = new AffineTransform();
currentAT.translate(0, this.getHeight());
currentAT.scale(xscale, -yscale);
// put origin in bottom corner
currentAT.translate(-xorigin, -yorigin);
g2.setTransform(currentAT);
if (showRegions) {
// Draw the regions
List<Region> l = Model.instance().getRegions();
for (int i = 0; i < l.size(); i++) {
g2.setPaint(regionOutlineColor);
// border (same color)
g2.draw(l.get(i).getPath());
g2.setPaint(regionFillColor);
g2.fill(l.get(i).getPath());
}
}
// Draw the measurements; changes graphics
for (int i = 0; i < measurementRepList.size(); i++) {
measurementRepList.get(i).draw(g2);
}
if (showReceivers) {
// draw receivers
for (int i = 1; i < Engine.instance().getMaxReceiverNumber() + 1; i++) {
// indexed from 1
Receiver r = Engine.instance().getReceiver(i);
Point3d p = Engine.instance().getReceiverPosition(i);
if (p != null && r != null) {
if (r.isActive()) {
g2.setPaint(Color.BLACK);
} else {
g2.setPaint(Color.GRAY);
}
Shape s = new Ellipse2D.Double(p.x - RECEIVER_SIZE / 2, p.y - RECEIVER_SIZE / 2, RECEIVER_SIZE, RECEIVER_SIZE);
g2.draw(s);
g2.fill(s);
}
}
}
// restore original transform
g2.setTransform(saveAT);
}
use of jmri.jmrix.rps.Region in project JMRI by JMRI.
the class RpsTrackingPanelTest method testShow.
@Test
public void testShow() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
new Engine() {
void reset() {
_instance = null;
}
}.reset();
Engine.instance().setMaxReceiverNumber(2);
Engine.instance().setReceiver(1, new Receiver(new Point3d(12., 12., 0.)));
Engine.instance().setReceiver(2, new Receiver(new Point3d(13., 13., 0.)));
JmriJFrame f = new JmriJFrame("Test Tracking Panel");
f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
RpsTrackingPanel p = new RpsTrackingPanel();
p.setSize(400, 400);
p.setOrigin(0, 0);
p.setCoordMax(30., 30.);
f.getContentPane().add(p);
f.pack();
// add some regions to probe corners
Region r = new Region("(4,4,0);(10,16,0);(18,10,0);(4,4,0)");
Model.instance().addRegion(r);
r = new Region("(30,15,0);(25,15,0);(25,20,0);(30,15,0)");
Model.instance().addRegion(r);
r = new Region("(15,30,0);(15,25,0);(20,25,0);(15,30,0)");
Model.instance().addRegion(r);
r = new Region("(25,25,0);(25,28,0);(30,30,1);(29,25,0);(25,25,0)");
Model.instance().addRegion(r);
// show overlap
r = new Region("(20,20,0);(22,20,0);(22,22,1);(20,22,0)");
Model.instance().addRegion(r);
r = new Region("(19,19,0);(21,19,0);(21,21,1);(19,21,0)");
Model.instance().addRegion(r);
// show panel
f.setSize(400, 400);
f.setVisible(true);
Reading loco = new Reading("21", null);
Measurement m = new Measurement(loco, 0.0, 0.0, 0.0, 0.133, 5, "source");
p.notify(m);
loco = new Reading("21", null);
m = new Measurement(loco, 5., 5., 0.0, 0.133, 5, "source");
p.notify(m);
loco = new Reading("21", null);
m = new Measurement(loco, 0., 5., 0.0, 0.133, 5, "source");
p.notify(m);
loco = new Reading("21", null);
m = new Measurement(loco, 5., 0., 0.0, 0.133, 5, "source");
p.notify(m);
// check separate locos
int NUM_LOCO = 64;
for (int i = 0; i < NUM_LOCO; i++) {
loco = new Reading("" + i, null);
m = new Measurement(loco, 6. + 1. * i, 0., 0.0, 0.133, 5, "source");
p.notify(m);
m = new Measurement(loco, 6. + 1. * i, 12., 0.0, 0.133, 5, "source");
p.notify(m);
}
Assert.assertNotNull("found frame", f);
f.dispose();
}
use of jmri.jmrix.rps.Region in project JMRI by JMRI.
the class RpsTrackingPanel method getToolTipText.
/**
* Provide tool tip text that depends on what's under the cursor.
* <p>
* Names either a measurement point or a region.
*
* @return null if no object under mouse; this suppresses ToolTip
*/
@Override
public String getToolTipText(MouseEvent e) {
// get mouse coordinates
try {
Point mouse = e.getPoint();
Point2D userPt = currentAT.inverseTransform(new Point2D.Double(mouse.x, mouse.y), null);
// find the path object containing it, if any
for (int i = measurementRepList.size() - 1; i >= 0; i--) {
MeasurementRep r = measurementRepList.get(i);
if (r.contains(userPt)) {
Measurement m = r.measurement;
return "ID " + m.getID() + " at " + m.getX() + "," + m.getY();
}
}
// find the region containing it, if any
// Go through backwards to find the top if overlaps
List<Region> l = Model.instance().getRegions();
for (int i = l.size() - 1; i >= 0; i--) {
Shape s = l.get(i).getPath();
if (s.contains(userPt)) {
return "Region: " + l.get(i).toString() + ", at " + userPt.getX() + "," + userPt.getY();
}
}
// found nothing, just display location
return "" + userPt.getX() + "," + userPt.getY();
} catch (Exception ex) {
}
// or return default
return null;
}
Aggregations