首页 > 代码库 > 小票代码

小票代码

package printer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class PrinterOK {


private int fontMargin=0;
    
    private int margin =20;
    
    private int titleFontSize = 10;
    private int contentFontSize = 10;
    
    public int getContentFontSize() {
        return contentFontSize;
    }

    public void setContentFontSize(int contentFontSize) {
        this.contentFontSize = contentFontSize;
    }

    public int getTitleFontSize() {
        return titleFontSize;
    }

    public void setTitleFontSize(int titleFontSize) {
        this.titleFontSize = titleFontSize;
    }

    public int getMargin() {
        return margin;
    }

    public void setMargin(int margin) {
        this.margin = margin;
    }

    public int getFontMargin() {
        this.fontMargin += margin;
        return fontMargin;
    }

    public void setFontMargin(int fontMargin) {
        this.fontMargin = fontMargin;
    }

    
    public void outputImage() {
        BufferedImage image = new BufferedImage(300, 800,
                BufferedImage.TYPE_USHORT_GRAY);
        Graphics2D g = image.createGraphics();
         
        g.setBackground(new Color(255,255,255));  
        g.setPaint(new Color(0,0,0));  
        g.clearRect(0, 0, 300, 1000);   
        
 g.clearRect(0, 0, 220, 1000);   
        
        g.setColor(Color.BLACK); // 设置颜色
        g.setFont(new Font("宋体",Font.BOLD,15));//设置字体
        g.drawString("婷美摩力21产后恢复", 30, this.getFontMargin());
        
        g.setFont(new Font("宋体",Font.PLAIN,this.getTitleFontSize()));//设置字体
        
        
        //抬头开始
        this.margin = 15;
        g.drawString("单号:TM0001111708110952528660000", 10, this.getFontMargin());

        g.drawString("收营员:陆建", 10, this.getFontMargin());
        
        g.drawString("调理师:015", 10, this.getFontMargin());
        
        g.drawString("购买时间:2017-08-11 09::52:52", 10, this.getFontMargin());
        //抬头结束
        
        //划线开始
        this.margin = 20;
        int line = this.getFontMargin();
        g.drawLine(10, line, 280, line);
        //划线结束
        
        //内容开始
        
        //标题开始
        int titleMargin = this.getFontMargin()-10;
        
        g.drawString("服务名称", 10, titleMargin);
        g.drawString("数量", 120, titleMargin);
        g.drawString("单价", 150, titleMargin);
        g.drawString("金额", 190, titleMargin);
        //标题结束

        g.setFont(new Font("宋体",Font.PLAIN,this.getContentFontSize()));//设置字体
        //具体内容开始
        for (int i = 0; i < 10; i++) {
            int contentMargin = this.getFontMargin();
            g.drawString("臀部上提  美肤紧腹", 10, contentMargin);
            g.drawString("5", 110, contentMargin);
            g.drawString("¥4550", 140, contentMargin);
            g.drawString("¥22900", 185, contentMargin);
        }
        //具体内容结束
        //内容结束
        //划线开始
        line = this.getFontMargin()-10;
        g.drawLine(10, line, 280, line);
        //划线结束
        this.margin = 15;
        //尾部信息
        g.drawString("会员姓名:如花", 10, this.getFontMargin()-10);
        g.drawString("会员联系方式:1888888887", 10, this.getFontMargin()-10);
        g.drawString("服务项目数量", 10, this.getFontMargin()-10);
        g.drawString("应收金额:¥96800", 10, this.getFontMargin()-10);
        g.drawString("优惠金额:0", 10, this.getFontMargin()-10);
        g.drawString("支付方式:现金¥10000", 10, this.getFontMargin()-10);
        g.drawString("支付方式:银行卡¥9000", 10, this.getFontMargin()-10);
        g.drawString("支付方式:支付宝¥100", 10, this.getFontMargin()-10);
        g.drawString("支付方式:微信¥100", 10, this.getFontMargin()-10);
        g.drawString("支付方式:优惠券¥60", 10, this.getFontMargin()-10);
        g.drawString("找零¥0", 10, this.getFontMargin());
        
        this.getFontMargin();
        g.drawString("谢谢惠顾!!!", 10, this.getFontMargin());
//        image.flush();
        try {
            ImageIO.write(image, "png", new File("C:\\Users\\Administrator\\Desktop\\aaaa.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        g.dispose();
    }

    public static void main(String args[]) {
        new PrinterOK().outputImage();
//        new FrameTicket();
    }
}

 

小票代码