【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了PaperPanel.java提供大家参考,希望对大家有所帮助!
/*
* PagePanel.java
*
* Created on 2008年7月29日, 下午2:38
*/
package com.mwq.print.mwing;
import com.mwq.print.tabbedPane.BackPanel;
import com.mwq.print.tabbedPane.BasePanel;
import com.mwq.print.tabbedPane.PagePanel;
import com.mwq.print.tabbedPane.WaterPanel;
import com.mwq.print.tool.InstancePool;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
*
* @author Administrator
*/
public class PaperPanel extends JPanel implements Printable {
private static int Page = 1;
private static int[] PaperWidths = {420, 297, 210, 148, 364, 257, 182, 0};
private static int[] PaperHeights = {594, 420, 297, 210, 514, 364, 257, 0};
public static int Width, Height;
public static int UpSpace, DownSpace, LeftSpace, RightSpace;
public static int UpBorder, DownBorder, LeftBorder, RightBorder;
static {
for (int i = 0; i < PaperWidths.length - 1; i++) {
PaperWidths[i] = (int) (PaperWidths[i] * 2.8);
PaperHeights[i] = (int) (PaperHeights[i] * 2.8);
}
}
public static void setPage(int aPage) {
Page = aPage;
}
public static void setPaperSize(int width, int height) {
int index = PaperWidths.length - 1;
PaperWidths[index] = (int) (width * 2.8);
PaperHeights[index] = (int) (height * 2.8);
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
printAll(graphics);
return Printable.PAGE_EXISTS;
}
/** Creates new form PagePanel */
public PaperPanel() {
initComponents();
//设置页面
baseSetup();
pageSetup();
watermarkSetup();
backgroundSetup();
}
//背景
private static ImageIcon background;
public void initBackground() {
if (InstancePool.getBackPanel().isAddBackground()) {
BackPanel backPanel = InstancePool.getBackPanel();
String photoPath = backPanel.getPhotoPath();
if (photoPath.length() > 0) {
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new File(photoPath));
} catch (IOException ex) {
Logger.getLogger(PaperPanel.class.getName()).log(Level.SEVERE, null, ex);
}
int w = Width - LeftSpace - RightSpace;
int h = Height - UpSpace - DownSpace;
if (backPanel.isDefault()) {
w = Math.min(w, bufferedImage.getWidth());
h = Math.min(h, bufferedImage.getHeight());
background = new ImageIcon(bufferedImage.getSubimage(0, 0, w, h));
} else {
background = new ImageIcon(bufferedImage.getScaledInstance(w, h, BufferedImage.SCALE_DEFAULT));
}
}
}
}
public void backgroundSetup() {
if (InstancePool.getBackPanel().isAddBackground() && background != null) {
BackgroundLabel backgroundLabel = new BackgroundLabel(background);
backgroundLabel.setBounds(LeftSpace + 1, UpSpace + 1,
background.getIconWidth(), background.getIconHeight());
add(backgroundLabel);
}
}
//水印
private void watermarkSetup() {
WaterPanel waterPanel = InstancePool.getWaterPanel();
if (waterPanel.isYesRadioButtonSelected()) {// 判断是否添加水印
int areaWidth = Width - LeftSpace - LeftBorder - RightBorder - RightSpace;
int areaHeight = Height - UpSpace - UpBorder - DownBorder - DownSpace;
WatermarkLabel watermarkLabel = new WatermarkLabel(areaWidth);
int watermarkWidth = watermarkLabel.getPreferredSize().width;
int watermarkHeight = watermarkLabel.getPreferredSize().height;
int x = LeftSpace + LeftBorder;
int y = UpSpace + UpBorder;
Enumeration areaEnumeration = waterPanel.getAreaEnumeration();
while (areaEnumeration.hasMoreElements()) {// 判断水印添加的位置
JRadioButton radioButton = (JRadioButton) areaEnumeration.nextElement();
if (radioButton.isSelected()) {
int i = Integer.valueOf(radioButton.getName());
switch (i) {// 定义水印的起始绘制位置
case 1:// 靠左上角显示
break;
case 2:// 靠上方居中显示
x += (areaWidth - watermarkWidth) / 2;
break;
case 3:// 靠右上角显示
x += areaWidth - watermarkWidth;
break;
case 4:// 靠左侧居中显示
y += (areaHeight - watermarkHeight) / 2;
break;
case 5:// 居中显示
x += (