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

108 lines
3.1 KiB
Java

package ch.psi.mxsc;
import ch.psi.pshell.swing.SwingUtils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
/**
*
*/
public class SampleGraphics {
final Sample sample;
final PuckGraphics puckGraphics;
public SampleGraphics(Sample sample, PuckGraphics graphics){
this.sample = sample;
this.puckGraphics = graphics;
}
Color getColor() {
Color ret = Color.LIGHT_GRAY;
if (sample.isLoaded() ) {
ret = Color.BLUE;
//} else if (sample.wasLoaded()) {
// ret = Color.GREEN;
} else if (sample.isPresent()) {
ret = Color.CYAN.darker().darker();
}
if (sample.isSelected()) {
ret = ret.brighter();
}
return ret;
}
int getNormalDrawSize() {
return puckGraphics.getDrawSize(sample.getSize().getWidth() );
}
int getDrawSize() {
int ret = getNormalDrawSize();
if (sample.isSelected()) {
ret += 2;
}
return ret;
}
Point getDrawPosition() {
return puckGraphics.getDrawPosition(puckGraphics.getSamplePosition(sample));
}
Color getLabelColor(boolean drawBackground) {
return drawBackground ? puckGraphics.isHighlithted() ? Color.DARK_GRAY : new Color(92, 92, 92) : new Color (0,96,0);
}
Font getLabelFont() {
////return new Font("Times New Roman", Font.PLAIN, 8);
//return new Font("Courier New", Font.PLAIN, 8);
return new Font("Tahoma", Font.PLAIN, 8);
}
Point getLabelPosition(String text, Graphics g) {
Point center = getDrawPosition();
Dimension textSize = SwingUtils.getTextSize(text, g.getFontMetrics());
return new Point(center.x - textSize.width / 2 + 1, center.y + (g.getFontMetrics().getAscent()/2));
}
Color getBorderColor(boolean drawBackground) {
if (drawBackground){
if (!sample.isEnabled()) {
return Color.GRAY;
} else if (sample.isSelected()) {
return new Color(32,32,32);
}
return Color.GRAY;
}
return sample.isSelected() ? new Color(0,32,0) : new Color(0,128,0);
}
void draw (Graphics2D g, boolean drawBackground){
Point position = getDrawPosition();
int size = getDrawSize();
if (drawBackground){
g.setColor(getColor());
g.fillOval(position.x - size / 2, position.y - size / 2, size, size);
}
g.setColor(getBorderColor(drawBackground));
g.drawOval(position.x - size / 2, position.y - size / 2, size, size);
if (getNormalDrawSize()>10){
String text = String.valueOf(sample.index + 1);
g.setColor(getLabelColor(drawBackground));
g.setFont(getLabelFont());
Point labelPosition = getLabelPosition(text, g);
g.drawString(text, labelPosition.x, labelPosition.y);
}
g.setPaintMode();
}
}