I have a program that draws to the a panel and also prints. When drawing to the Canvas the renderer produces the correct output. However when drawing to the print method the font is overridden with a default font. Please see below two images displaying the out put.
The code to the specific areas are in my Visualise2D.class The Renderers for the Canvas and the Print are the same they both call the Visualise2D.class.
Some Code from Visualise2D
public void paintSurfaceTimes(Graphics2D gTimes, SurfaceConnector s, Dummy d, Color c, Rectangle2D bounds,double scale, double enhanceTie, double enhance ){
gTimes.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics fm = gTimes.getFontMetrics();
double hEdge = (UnitConvert.pixelsToMeters(LiteTieTRIAL.averageSize));
double x1 =((d.getEasting() - bounds.getX()));
double y1 = (bounds.getHeight() + bounds.getY() - d.getNorthing());
if (d instanceof Hole) {
hEdge = (UnitConvert.pixelsToMeters(((Hole) d).getDiameter()* enhance)/2);
}
else
hEdge = (UnitConvert.pixelsToMeters(LiteTieTRIAL.averageSize * enhance)/2);
x1 =((d.getEasting() - bounds.getX()));
y1 = (bounds.getHeight() + bounds.getY() - d.getNorthing());
gTimes.setColor(c);
gTimes.setFont(DEFAULT_FONT);
fm = gTimes.getFontMetrics();
gTimes.drawString((dec0P.format(s.getTime())),
(int)((x1+hEdge*Math.cos(180))*scale - fm.getStringBounds(dec0P.format(s.getTime()), gTimes).getWidth()),
(int)((y1 - hEdge*Math.sin(0))*scale+ fm.getStringBounds(dec0P.format(s.getTime()), gTimes).getHeight()));
}
And some Code from paintComponent(Graphics g) in my getCanvas() method
//Draws SurfaceTimes
if(surfaceTimesOnOffButton.isSelected()) {
try {
getSurfaceTimes();
} catch (NegativeNumberException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Pattern tempPat: world.getPatternList().values()){
for(SurfaceConnector sc: tempPat.getSurfaceList().values()){
// boolean isSelected = sc == selected || (selected instanceof Collection<?> && ((Collection<?>)selected).contains(sc));
if(sc.getTo() instanceof Dummy ){
renderer.paintSurfaceTimes(g2, sc, sc.getTo(), colourTimes, bounds, Zoom.getScalingFactor()* UnitConvert.metersToPixels(1), enhanceTie, enhance);
}
}
}
}
The only differences between the canvas drawing code and the printable is: getCanvas() code below...
Visualise2D renderer = new Visualise2D();
@Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
LiteTiePrinter.class
public class LiteTiePrinter implements Printable {
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
// We have only one page, and 'page'
// is zero-based
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
// Now we perform our rendering
Visualise2D renderer = new Visualise2D();
Any help is appreciated as to why the output on fonts is different. Thank you in advance.