📄 componentprinter.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/8/17
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.tools.print;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import eti.bi.common.Locale.Resource;
public class ComponentPrinter implements Printable{
private Component component = null;
private String[] superscript;
private String title;
private String alphaminer = Resource.srcStr("AlphaMiner");
private static Font TITLE_FONT;
/** The given component should be printed. */
public ComponentPrinter(Component component,String title, String[] superscript) {
this.component = component;
this.title = title;
this.superscript = superscript;
if(this.title==null) {
this.title = "";
}
try {
TITLE_FONT = new Font(Resource.srcStr("font"), Font.PLAIN, Integer.parseInt(Resource.srcStr("fontsize")));
}
catch(Exception e) {
e.printStackTrace();
TITLE_FONT = new Font(Resource.srcStr("font"), Font.PLAIN, 10);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex >= 1)
return NO_SUCH_PAGE;
int i,y;
Rectangle2D rectAlphaminer = TITLE_FONT.getStringBounds(alphaminer, ((Graphics2D) g).getFontRenderContext());
g.setFont(TITLE_FONT);
y = (int)(pageFormat.getImageableY()+rectAlphaminer.getHeight())+1;
g.drawString(alphaminer,(int)pageFormat.getImageableX(),y);
if(superscript!=null) {
for(i=0;i<superscript.length;i++) {
rectAlphaminer = TITLE_FONT.getStringBounds(superscript[i], ((Graphics2D) g).getFontRenderContext());
y += (int)rectAlphaminer.getHeight()+1;
g.drawString(superscript[i],(int)pageFormat.getImageableX(),y);
}
}
Rectangle2D rectTitle = TITLE_FONT.getStringBounds(title, ((Graphics2D) g).getFontRenderContext());
y += (int)rectTitle.getHeight()+ 4;
g.drawString(title, (int) (pageFormat.getImageableX() + pageFormat.getImageableWidth() / 2 - rectTitle.getWidth() / 2), y);
y+=3;
Graphics2D translated = (Graphics2D) g.create((int) pageFormat.getImageableX(), y, (int) pageFormat.getImageableWidth(), (int) (pageFormat.getImageableHeight() - y));
component.print(translated);
translated.dispose();
return PAGE_EXISTS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -