/*
* Copyright 2011, iSoftStone Co., Ltd. All right reserved.
*
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF iSoftStone CO., LTD.
* THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
* TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
* WITHOUT THE PRIOR WRITTEN PERMISSION OF NEWTOUCH CO., LTD.
*/
/**
*
*/
package com.iss.printer.action;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.iss.printer.config.SystemProperties;
import com.iss.printer.module.PrintPreviewDialog;
/**
* @author junni
*
*/
public class PrintWindow extends JFrame
implements ActionListener, Printable
{
private JButton printTextButton = new JButton("Print Text");
private JButton previewButton = new JButton("Print Preview");
private JButton printText2Button = new JButton("Print Text2");
private JButton printFileButton = new JButton("Print File");
private JButton printFrameButton = new JButton("Print Frame");
private JButton exitButton = new JButton("Exit");
private JLabel tipLabel = new JLabel("");
private JTextArea area = new JTextArea();
private JScrollPane scroll = new JScrollPane(area);
private JPanel buttonPanel = new JPanel();
private int PAGES = 0;
private String printStr;
public PrintWindow()
{
this.setTitle("Print Test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds((int)((SystemProperties.SCREEN_WIDTH - 800) / 2), (int)((SystemProperties.SCREEN_HEIGHT - 600) / 2), 800, 600);
initLayout();
}
private void initLayout()
{
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(scroll, BorderLayout.CENTER);
printTextButton.setMnemonic('P');
printTextButton.addActionListener(this);
buttonPanel.add(printTextButton);
previewButton.setMnemonic('v');
previewButton.addActionListener(this);
buttonPanel.add(previewButton);
printText2Button.setMnemonic('e');
printText2Button.addActionListener(this);
buttonPanel.add(printText2Button);
printFileButton.setMnemonic('i');
printFileButton.addActionListener(this);
buttonPanel.add(printFileButton);
printFrameButton.setMnemonic('F');
printFrameButton.addActionListener(this);
buttonPanel.add(printFrameButton);
exitButton.setMnemonic('x');
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent evt)
{
Object src = evt.getSource();
if (src == printTextButton)
printTextAction();
else if (src == previewButton)
previewAction();
else if (src == printText2Button)
printText2Action();
else if (src == printFileButton)
printFileAction();
else if (src == printFrameButton)
printFrameAction();
else if (src == exitButton)
exitApp();
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
if (page >= PAGES)
return Printable.NO_SUCH_PAGE;
g2.translate(pf.getImageableX(), pf.getImageableY());
drawCurrentPageText(g2, pf, page);
return Printable.PAGE_EXISTS;
}
private void drawCurrentPageText(Graphics2D g2, PageFormat pf, int page)
{
Font f = area.getFont();
String s = getDrawText(printStr)[page];
String drawText;
float ascent = 16;
int k, i = f.getSize(), lines = 0;
while(s.length() > 0 && lines < 54)
{
k = s.indexOf('\n');
if (k != -1)
{
lines += 1;
drawText = s.substring(0, k);
g2.drawString(drawText, 0, ascent);
if (s.substring(k + 1).length() > 0)
{
s = s.substring(k + 1);
ascent += i;
}
}
else
{
lines += 1;
drawText = s;
g2.drawString(drawText, 0, ascent);
s = "";
}
}
}
public String[] getDrawText(String s)
{
String[] drawText = new String[PAGES];
for (int i = 0; i < PAGES; i++)
drawText[i] = "";
int k, suffix = 0, lines = 0;
while(s.length() > 0)
{
if(lines < 54)
{
k = s.indexOf('\n');
if (k != -1)
{
lines += 1;
drawText[suffix] = drawText[suffix] + s.substring(0, k + 1);
if (s.substring(k + 1).length() > 0)
s = s.substring(k + 1);
}
else
{
lines += 1;
drawText[suffix] = drawText[suffix] + s;
s = "";
}
}
else
{
lines = 0;
suffix++;
}
}
return drawText;
}
public int getPagesCount(String curStr)
{
int page = 0;
int position, count = 0;
String str = curStr;
while(str.length() > 0)
{
position = str.indexOf('\n');
count += 1;
if (position != -1)
str = str.substring(position + 1);
else
str = "";
}
if (count > 0)
page = count / 54 + 1;
return page;
}
private void printTextAction()
{
printStr = area.getText().trim();
if (printStr != null && printStr.length() > 0)
{
PAGES = getPagesCount(printStr);
PrinterJob myPrtJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = myPrtJob.defaultPage();
myPrtJob.setPrintable(this, pageFormat);
if (myPrtJob.printDialog())
{
try
{
myPrtJob.print();
}
catch(PrinterException pe)
{
pe.printStackTrace();
}
}
}
else
{
JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty"
, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java实现打印机通信源码(awt)
共34个文件
class:18个
java:6个
properties:2个
5星 · 超过95%的资源 需积分: 13 28 下载量 101 浏览量
2012-02-20
16:25:01
上传
评论 3
收藏 456KB RAR 举报
温馨提示
java实现与打印机通信具体包括内容有: 1.实现打印输入文本预览打印显示 2.实现选择文件显示 3.配置打印服务器
资源推荐
资源详情
资源评论
收起资源包目录
EPrinterS.rar (34个子文件)
EPrinterS
.project 1KB
.mymetadata 300B
src
config.properties 77B
com
iss
printer
module
PrintPreviewDialog.java 4KB
action
PrintWindow.java 12KB
config
SystemProperties.java 2KB
PropertieFileReader.java 1KB
test
PrintTest1.java 5KB
PrintTest.java 5KB
WebRoot
WEB-INF
classes
config.properties 77B
com
iss
upload
client
UploadThread.class 4KB
FileProperties.class 1KB
FilePanel.class 2KB
UploadPanel$1.class 894B
UploadApplet.class 2KB
UploadFrame.class 2KB
ChoseFileFilter.class 3KB
UploadPanel.class 9KB
server
UploadServlet.class 2KB
UploadServer.class 2KB
UploadServerThread.class 3KB
printer
module
PrintPreviewDialog$PreviewCanvas.class 3KB
PrintPreviewDialog.class 3KB
action
PrintWindow.class 10KB
config
PropertieFileReader.class 2KB
SystemProperties.class 2KB
test
PrintTest.class 4KB
PrintTest1.class 3KB
lib
jaws.jar 1.67MB
web.xml 382B
upload
index.jsp 829B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.jdt.core.prefs 330B
.classpath 429B
共 34 条
- 1
资源评论
- alx_zhang2014-05-29还是可以,但是中文乱码
- riitfkghd1122012-10-26一般般,不是很清楚
- 小锋0072018-06-25资源可以用,感谢你的分享!
小木人印象
- 粉丝: 1
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功