Java调用热敏票据打印机打印小票

Java调用热敏票据打印机打印小票packagecom.yc.printer;/***源辰信息*商品信息*@authornavy*2017年5月23日*/publicclassGoodsInfo{privateStringgnam

package com.yc.printer;

/**

 * 源辰信息

 * 商品信息 

 * @author navy

 * 2017年5月23日

 */

public class GoodsInfo {


private String gname; //商品名称


private String price; //商品单价


private String num; //商品数量


private String total; //小计





@Override


public String toString() {


return “GoodsInfo [gname=” + gname + “, price=” + price + “, num=” + num + “, total=” + total + “]”;


}


public GoodsInfo(String gname, String price, String num, String total) {


super();


this.gname = gname;


this.price = price;


this.num = num;


this.total = total;


}


public String getGname() {


return gname;


}


public void setGname(String gname) {


this.gname = gname;


}


public String getPrice() {


return price;


}


public void setPrice(String price) {


this.price = price;


}


public String getNum() {


return num;


}


public void setNum(String num) {


this.num = num;


}


public String getTotal() {


return total;


}


public void setTotal(String total) {


this.total = total;


}


@Override


public int hashCode() {


final int prime = 31;


int result = 1;


result = prime * result + ((gname == null) ? 0 : gname.hashCode());


result = prime * result + ((num == null) ? 0 : num.hashCode());


result = prime * result + ((price == null) ? 0 : price.hashCode());


result = prime * result + ((total == null) ? 0 : total.hashCode());


return result;


}


@Override


public boolean equals(Object obj) {


if (this == obj)


return true;


if (obj == null)


return false;


if (getClass() != obj.getClass())


return false;


GoodsInfo other = (GoodsInfo) obj;


if (gname == null) {


if (other.gname != null)


return false;


} else if (!gname.equals(other.gname))


return false;


if (num == null) {


if (other.num != null)


return false;


} else if (!num.equals(other.num))


return false;


if (price == null) {


if (other.price != null)


return false;


} else if (!price.equals(other.price))


return false;


if (total == null) {


if (other.total != null)


return false;


} else if (!total.equals(other.total))


return false;


return true;


}

}

package com.yc.printer;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

/**

 * 源辰信息

 * 销售清单对象

 * @author navy

 * 2017年5月23日

 */

public class SalesTicket  implements Printable{


private List<GoodsInfo> goods; //商品列表


private String operatorName=”源辰信息”; //操作员


private String orderId; //订单编号


private String totalGoodsNum; //商品总数


private String totalPrice; //总金额


private String actualCollection; //实收款


private String giveChange; //找零


private String cardNumber; //会员编号


private String integral; //积分





private SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss EEE”);


@Override


public String toString() {


return “SalesTicket [goods=” + goods + “, operatorName=” + operatorName + “, orderId=” + orderId


+ “, totalGoodsNum=” + totalGoodsNum + “, totalPrice=” + totalPrice + “, actualCollection=”


+ actualCollection + “, giveChange=” + giveChange + “, cardNumber=” + cardNumber + “, integral=”


+ integral + “]”;


}


public SalesTicket(List<GoodsInfo> goods, String operatorName, String orderId, String totalGoodsNum,


String totalPrice, String actualCollection, String giveChange, String cardNumber, String integral) {


super();


this.goods = goods;


this.operatorName = operatorName;


this.orderId = orderId;


this.totalGoodsNum = totalGoodsNum;


this.totalPrice = totalPrice;


this.actualCollection = actualCollection;


this.giveChange = giveChange;


this.cardNumber = cardNumber;


this.integral = integral;


}


public SalesTicket(List<GoodsInfo> goods, String operatorName, String orderId, String totalGoodsNum,


String totalPrice, String actualCollection, String giveChange) {


super();


this.goods = goods;


this.operatorName = operatorName;


this.orderId = orderId;


this.totalGoodsNum = totalGoodsNum;


this.totalPrice = totalPrice;


this.actualCollection = actualCollection;


this.giveChange = giveChange;


}


/**


* 打印方法


* graphics – 用来绘制页面的上下文,即打印的图形


* pageFormat – 将绘制页面的大小和方向,即设置打印格式,如页面大小一点为计量单位(以1/72 英寸为单位,1英寸为25.4毫米。A4纸大致为595 × 842点)


*
小票纸宽度一般为58mm,大概为165点


* pageIndex – 要绘制的页面从 0 开始的索引 ,即页号


*/


@Override


public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {


//此 Graphics2D 类扩展 Graphics 类,以提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。


//它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。 


Graphics2D g2 = (Graphics2D) graphics;  


g2.setColor(Color.black);//设置打印颜色为黑色  


//打印起点坐标  


double x= pageFormat.getImageableX();  //返回与此 PageFormat相关的 Paper对象的可成像区域左上方点的 x坐标。  


double y= pageFormat.getImageableY();  //返回与此 PageFormat相关的 Paper对象的可成像区域左上方点的 y坐标。


//Font.PLAIN: 普通样式常量  
Font.ITALIC 斜体样式常量
Font.BOLD 粗体样式常量。


Font font = new Font(“宋体”,Font.BOLD,10); //根据指定名称、样式和磅值大小,创建一个新 Font。





g2.setFont(font);//设置标题打印字体     





float heigth = font.getSize2D();//获取字体的高度  





//设置小票的标题标题  


g2.drawString(“源辰信息科技有限公司”,(float)x+25,(float)y+heigth); 





float line = 2*heigth; //下一行开始打印的高度


g2.setFont(new Font(“宋体”, Font.PLAIN,8));//设置正文字体  


heigth = font.getSize2D();// 字体高度  





line+=2;


//设置操作员 


g2.drawString(“操作员:”+operatorName,(float)x+20,(float)y+line); 


line+=heigth;





//设置订单号  


g2.drawString(“订单号:”+orderId, (float)x+20,(float)y+line);  


line+=heigth+2; 





//设置标题  


g2.drawString(“名称”,(float)x+20, (float)y+line);  


g2.drawString(“单价”,(float)x+60, (float)y+line);  


g2.drawString(“数量”,(float)x+90, (float)y+line);  


g2.drawString(“小计”,(float)x+120, (float)y+line);  


line+=heigth;


/*


* 虚线绘制设置    setStroke(Stroke):为 Graphics2D 上下文设置 Stroke


* 由 BasicStroke定义的呈现属性描述了用画笔沿 Shape 的轮廓绘制的某个标记的形状,以及应用在 Shape 路径线段的末端和连接处的装饰。


* 这些呈现属性包括: 


* width:画笔的宽度,是垂直于画笔轨迹的测量值。  此宽度必须大于或等于 0.0f,0.0f为最细线条。  


* end caps:在未封闭子路径和虚线线段的末端应用的一些装饰。如果子路径没有 CLOSE 段,则在同一点上开始和结束的子路径仍被认为是未封闭的。


*
关于 CLOSE 段的更多信息,请参阅 SEG_CLOSE。三个不同的装饰是:


*
CAP_BUTT:无装饰地结束未封闭的子路径和虚线线段。


*
CAP_ROUND:使用半径等于画笔宽度一半的圆形装饰结束未封闭的子路径和虚线线段。


*
CAP_SQUARE:使用正方形结束未封闭的子路径和虚线线段,正方形越过线段端点,并延长等于线条宽度一半的距离。 


* line joins:在两个路径线段的交汇处,以及使用 SEG_CLOSE 封闭的子路径端点的交汇处应用的装饰。


*
三个不同的装饰是:


*
JOIN_BEVEL:通过直线连接宽体轮廓的外角,将路径线段连接在一起。 


*
JOIN_MITER:扩展路径线段的外边缘,直到它们连接在一起。 


*
JOIN_ROUND:通过舍去半径为线长的一半的圆角,将路径线段连接在一起。 


* miter limit:对剪裁具有 JOIN_MITER 装饰的线接合点的限制。当斜接长度与笔划宽度的比大于 miterlimit 值时,需要剪裁线接合点。


*
斜接长度是斜接的对角线长度,即交汇处的内棱角和外棱角之间的距离。两条线段形成的角度越小,斜接长度就越长,交汇处的角度就越尖锐。


*
默认 miterlimit 值为 10.0f,它会使所有小于 11 度的角都被剪裁。剪裁斜接会使线接合点的装饰变成斜角。 必须大于或等于 1.0f。


* dash attributes:关于如何通过在不透明和透明部分之间交替形成一个虚线模式的定义。 表示虚线模式的数组  


* dash phase – 开始虚线模式的偏移量  


*/


//虚线设置


g2.setStroke(new BasicStroke(1f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,4.0f,new float[]{4.0f},0.0f));  


//在此图形上下文的坐标系中使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。 即绘制虚线


g2.drawLine((int)x,(int)(y+line),(int)x+158,(int)(y+line));  


line+=heigth; 


//设置商品清单


if(goods!=null && goods.size()>0){


for(GoodsInfo gdf:goods){


g2.drawString(gdf.getGname(),(float)x+15, (float)y+line);  


g2.drawString(gdf.getPrice(),(float)x+60, (float)y+line);  


g2.drawString(gdf.getNum(),(float)x+95,(float)y+line);  


g2.drawString(gdf.getTotal(),(float)x+120,(float)y+line);  


line += heigth; 


}


}


g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));  


line += heigth+2;  


g2.drawString(“商品总数:”+totalGoodsNum+ “件”,(float)x+15,(float)y+line);  


g2.drawString(“合计:”+totalPrice+” 元”, (float)x+80, (float)y+line);  


line += heigth;  


g2.drawString(“实收:”+actualCollection+”元”,(float)x+15,(float)y+line);  


g2.drawString(“找零:”+giveChange+”元”,(float)x+80,(float)y+line);  


line += heigth;





if(cardNumber!=null && !””.equals(cardNumber)){


g2.drawString(“当前会员:”+cardNumber,(float)x+15,(float)y+line);


line += heigth;


g2.drawString(“积分:”+integral,(float)x+15,(float)y+line);  


}


g2.drawString(“时间:”+sdf.format(new Date()),(float)x+15,(float)y+line);  


line += heigth; 


g2.drawString(“钱票请当面点清,离开柜台恕不负责”,(float)x+15,(float)y+line);  


switch (pageIndex) {  


case 0:  


return PAGE_EXISTS;  //0 


default:  


return NO_SUCH_PAGE;   //1


}  


}

}

package com.yc.printer;

import java.awt.print.Book;

import java.awt.print.PageFormat;

import java.awt.print.Paper;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

/**

 * 源辰信息

 * 打印对象 -> 实现Printable接口即可调用打印机打印

 * @author navy

 * 2017年5月23日

 */

public class YcPrinter{


private SalesTicket salesTicket;





public YcPrinter(SalesTicket salesTicket){


this.salesTicket=salesTicket;


}




    public void printer() {  

        try {  

            //Book 类提供文档的表示形式,该文档的页面可以使用不同的页面格式和页面 painter

            Book book = new Book(); //要打印的文档

            

            //PageFormat类描述要打印的页面大小和方向  

            PageFormat pf = new PageFormat();  //初始化一个页面打印对象

            pf.setOrientation(PageFormat.PORTRAIT); //设置页面打印方向,从上往下,从左往右

          

            //设置打印纸页面信息。通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。  

            Paper paper = new Paper(); 

            paper.setSize(158,30000);// 纸张大小  

            paper.setImageableArea(0,0,158,30000);// A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72  

            pf.setPaper(paper);  

  

            book.append(salesTicket,pf);  

           

            PrinterJob job = PrinterJob.getPrinterJob();   //获取打印服务对象  

            

            job.setPageable(book);  //设置打印类  

  

            job.print(); //开始打印 

        } catch (PrinterException e) {  

            e.printStackTrace();  

        }  

    }  

}

package com.yc.printer;

import java.util.ArrayList;

import java.util.List;

public class Test {


public static void main(String[] args) {


List<GoodsInfo> goods=new ArrayList<GoodsInfo>();


goods.add(new GoodsInfo(“J2EE”,”11800″,”1″,”11800″));


goods.add(new GoodsInfo(“大数据”,”14800″,”1″,”14800″));


goods.add(new GoodsInfo(“前端”,”11800″,”1″,”11800″));





SalesTicket stk=new SalesTicket(goods,”源辰信息”,”201705230010″,”3″,”38400″,”38400″,”0″);


YcPrinter p=new YcPrinter(stk);


p.printer();


}

}

Java调用热敏票据打印机打印小票

今天的文章Java调用热敏票据打印机打印小票分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/62279.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注