JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:如何修改條碼的大小 [Re:ryanpai]
nfty





發文: 21
積分: 0
於 2007-05-19 13:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ryanpai wrote:
試試看吧,使用BufferedImage的getScaledInstance


下面是我修改后的代碼,但無法印列到印表機上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class PrintingExample {
    
    public static void main(String[] args) {
        PrintPanel canvas = new PrintPanel();
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        try {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(canvas);
            if (job.printDialog()) {
                job.print(attributes);
            }
        } catch (PrinterException exception) {
            System.out.println("取消打印!!!!");
        }
        
    }
    
}
 
class PrintPanel extends JPanel implements Printable {
    int pages = 1;
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
    }
    
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
        if(page < pages){
            Graphics2D g2 = (Graphics2D)g;
            g2.translate(pf.getImageableX(),pf.getImageableY());
            drawCode(g2);
            return Printable.PAGE_EXISTS;
        }
        return Printable.NO_SUCH_PAGE;        
    }
    
    public void drawCode(Graphics2D g2){
        try {
            Barcode barcode  = BarcodeFactory.createCode39("086240205523",true);
            BufferedImage image = new BufferedImage(410, 800, BufferedImage.TYPE_BYTE_GRAY);
            g2 = (Graphics2D) image.getGraphics();
            g2.setColor(Color.WHITE);
            g2.fillRect(0,0,410,80); 
            barcode .draw(g2, 10, 5);
            
            double scaleRate = 0.5;
            
            int width = image.getWidth();
            int height = image.getHeight();
            java.awt.Image scaledImage = image.getScaledInstance((int)(width*scaleRate), (int)(height*scaleRate), java.awt.Image.SCALE_DEFAULT);
            BufferedImage bi2 = new BufferedImage((int)(width*scaleRate), (int)(height*scaleRate), BufferedImage.TYPE_BYTE_BINARY);
            g2 = (Graphics2D) bi2.getGraphics();
            g2.setColor(Color.WHITE);
            g2.fillRect(0,0, (int)(width*scaleRate), (int)(height*scaleRate));
            g2.drawImage(scaledImage,0,0,null);
 
        } catch (BarcodeException ex) {
            ex.printStackTrace();
        } catch (OutputException ex) {
            ex.printStackTrace();
        }
    }


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
4133 如何修改條碼的大小 nfty 172 2007-05-16 16:39
3928 Re:如何修改條碼的大小 nfty 471 2007-05-17 16:05
4510 Re:如何修改條碼的大小 ryanpai 2289 2007-05-18 09:57
3750 Re:如何修改條碼的大小 nfty 259 2007-05-19 09:09
3768 Re:如何修改條碼的大小 nfty 2467 2007-05-19 13:37
3724 Re:如何修改條碼的大小 Tand 68 2007-05-19 08:36
3701 Re:如何修改條碼的大小 nfty 1492 2007-05-21 13:35
3989 Re:如何修改條碼的大小 ryanpai 3841 2007-05-21 17:02
3649 Re:如何修改條碼的大小 nfty 66 2007-05-23 11:22
3660 Re:如何修改條碼的大小 Tand 62 2007-05-24 15:41
3742 Re:如何修改條碼的大小 nfty 166 2007-05-24 16:10
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8