帆软报表设计器源代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

79 lines
3.0 KiB

package com.fr.design.report.fit;
import com.fr.base.GraphHelper;
import com.fr.general.FRFont;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
public class NewFitPreviewPane extends JPanel {
private boolean fitFont = false;
private FitType fitType = FitType.DOUBLE_FIT;
private static final Color DEFAULT_PAINT_COLOR = Color.decode("#419BF9");
private static final int FIT_FONT_SIZE = 15;
private static final int NO_FIT_FONT_SIZE = 10;
private static final Dimension NO_FIT_CONTAINER_DIMENSION = new Dimension(230, 80);
public NewFitPreviewPane(){
}
public NewFitPreviewPane(FitType fitType){
this.fitType = fitType;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GRAY);
GraphHelper.drawRect(g, 1, 1, this.getWidth() - 2, this.getHeight() - 2);
g.setColor(DEFAULT_PAINT_COLOR);
FRFont textFont = FRFont.getInstance(FRFont.DEFAULT_FONTNAME, Font.PLAIN, fitFont ? FIT_FONT_SIZE : NO_FIT_FONT_SIZE);
g.setFont(textFont);
Dimension dimension = calculateCellDimension();
GraphHelper.drawLine(g, 1, dimension.height, dimension.width * 2 - 1, dimension.height);
GraphHelper.drawLine(g, dimension.width, 1, dimension.width, dimension.height * 2 - 1);
GraphHelper.drawRect(g, 1, 1, dimension.width * 2 - 2, dimension.height * 2 - 2);
double startX = calculateTextDrawStartX(dimension.width, this.getFontMetrics(textFont), "text1");
double startY = calculateTextDrawStartY(dimension.height);
GraphHelper.drawString(g, "text1", startX, startY);
GraphHelper.drawString(g, "text2", dimension.width + startX, startY);
GraphHelper.drawString(g, "text3", startX, dimension.height + startY);
GraphHelper.drawString(g, "text4", dimension.width + startX, dimension.height + startY);
}
private Dimension calculateCellDimension() {
if (fitType == FitType.DOUBLE_FIT) {
return new Dimension(this.getWidth() / 2, this.getHeight() / 2);
} else if (fitType == FitType.NOT_FIT) {
return new Dimension(NO_FIT_CONTAINER_DIMENSION.width / 2, NO_FIT_CONTAINER_DIMENSION.height / 2);
} else {
return new Dimension(this.getWidth() / 2, NO_FIT_CONTAINER_DIMENSION.height / 2);
}
}
private double calculateTextDrawStartX(int containerWidth, FontMetrics fontMetrics, String text) {
return (containerWidth - fontMetrics.stringWidth(text)) / 2.0D;
}
private double calculateTextDrawStartY(int containerHeight) {
return containerHeight / 2.0D;
}
public void refreshPreview(boolean fitFont, FitType fitType) {
this.fitFont = fitFont;
this.fitType = fitType;
repaint();
}
public void refreshPreview(boolean fitFont) {
this.fitFont = fitFont;
repaint();
}
}