Files
MXSC/src/main/java/ch/psi/mxsc/BasePlateGraphics.java
2025-08-08 10:50:06 +02:00

147 lines
5.0 KiB
Java

package ch.psi.mxsc;
import ch.psi.pshell.app.MainFrame;
import ch.psi.pshell.imaging.Utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class BasePlateGraphics {
final BasePlate basePlate;
List<PuckGraphics> puckGraphics = new ArrayList<>();
public BasePlateGraphics(BasePlate basePlate){
this.basePlate = basePlate;
for (Puck puck : basePlate.getPucks()){
puckGraphics.add(new PuckGraphics(puck, this));
}
}
Rectangle plotRect = new Rectangle(0, 0, 0, 0);
Rectangle boundingBox;
public Rectangle getPlotRect() {
return plotRect;
}
public Rectangle getBoundingBox() {
return boundingBox;
}
Color getBorderColor(boolean drawBackground) {
//return new Color(32,32,32);
if (drawBackground){
return MainFrame.isDark() ? new Color(32,32,32) : Color.DARK_GRAY;
}
return MainFrame.isDark() ? new Color(0,32,0) : new Color(0,64,0);
}
Color getColor() {
return MainFrame.isDark() ? new Color(67,71,73) /*new Color(65,81,109)*/ : new Color(200, 204, 213);
}
boolean drawContour;
enum DrawMode{
fill,
center,
left,
right,
top,
bottom
}
Rectangle imageRoi;
public void setImageRoi( Rectangle roi){
this.imageRoi = roi;
}
Point detection;
public void setDetection( Point detection){
this.detection = detection;
}
void draw(Graphics2D g, Rectangle plotRect, boolean drawSamples, boolean drawIds, boolean drawContour, DrawMode mode, BufferedImage img) {
if (mode != DrawMode.fill){
int length = Math.min(plotRect.width, plotRect.height);
Point center= new Point(plotRect.x + plotRect.width/2, plotRect.y + plotRect.height/2); //center;
switch (mode){
case left:
center.setLocation(length/2, center.y);
break;
case right:
center.setLocation(plotRect.width - length/2, center.y);
break;
case top:
center.setLocation(center.x, length/2);
break;
case bottom:
center.setLocation(center.x, plotRect.height - length/2);
break;
}
plotRect = new Rectangle(center.x - length/2, center.y - length/2, length, length);
}
this.plotRect = plotRect;
this.drawContour = drawContour;
boundingBox = new Rectangle((int)(plotRect.x+plotRect.width*0.05), (int)(plotRect.y+plotRect.height * 0.05), (int)(plotRect.width*0.90), (int)(plotRect.height*0.90));
int center_x=0, center_y=0;
if (img!=null){
Point detection = this.detection;
if (imageRoi!=null){
img = img.getSubimage(imageRoi.x, imageRoi.y, imageRoi.width, imageRoi.height);
if (detection!=null){
//detection = new Point(detection.x-imageRoi.x, detection.y-imageRoi.y);
center_x = detection.x * boundingBox.width / img.getWidth() + boundingBox.x;
center_y = detection.y * boundingBox.height / img.getHeight() + boundingBox.y;
center_x -= imageRoi.x * boundingBox.width / img.getWidth();
center_y -= imageRoi.y * boundingBox.height / img.getHeight();
}
}
img = Utils.stretch(img, boundingBox.width, boundingBox.height);
g.setClip(new Ellipse2D.Float(boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height));
g.drawImage(img, boundingBox.x, boundingBox.y, null);
}
boolean drawBackground = (img==null);
if (drawContour){
if (drawBackground){
g.setColor(getColor());
g.fillOval(boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
}
g.setColor(getBorderColor(drawBackground));
g.drawOval(boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
}
for (PuckGraphics pg : puckGraphics) {
pg.draw(g, null, drawSamples, drawIds, drawBackground) ;
}
if (img!=null){
if (detection!=null){
int size=25;
g.setColor(new Color(86, 193, 255));
g.setStroke(new BasicStroke(2f));
g.drawLine(center_x-size, center_y, center_x+size, center_y);
g.drawLine(center_x, center_y-size, center_x, center_y+size);
//g.drawOval(center_x-size, center_y-size, 2*size, 2*size);
}
}
}
}