Java 版 (精华区)
发信人: zms@smth.org (来福), 信区: Java
标 题: 我写的条形码控件(Powered by zms)
发信站: BBS 水木清华站 (Sat Mar 22 21:10:22 2003)
转信站: HIT!news.neu.edu.cn!maily.cic.tsinghua.edu.cn!SMTH
我写的一个显示条形码的控件
参照我找到的一个条形码的说明写的
下面是源代码,可以运行,显示的是一本书的upc
我对比了一下,有一个小地方不一样
书后的条形码很明显违反我看的这个条形码的说明
不过大部分是和我显示的一样的
如果有什么意见和建议:可以mailto:zms@263.net
package com.zms.upc;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
/**
* Powered by zms
* 请不要用于商业用途
* 请保留上面的标志
*/
public class UPC extends JLabel {
public void paint(Graphics g) {
super.paint(g);
FontMetrics metrics=g.getFontMetrics();
int ha=metrics.getAscent()-metrics.getDescent();
int hb=ha/2;
int hc=line.height;
int hd=line.width;
int he=metrics.getAscent()-metrics.getDescent();
int totalheight=ha+hb+hc+hd+he;
int wa=metrics.stringWidth(""+left+" ");
int wb=line.width*95;
int wc=line.width;
int wd=metrics.stringWidth(""+right);
int totalwidth=wa+wb+wc+wd;
Dimension dim=new Dimension(totalwidth,totalheight);
this.setMinimumSize(dim);
int x=(this.getWidth()-totalwidth)/2;
int y=(this.getHeight()-totalheight)/2;
int wid=metrics.stringWidth(isbn);
g.drawString(isbn,x+wa+(wb-wid)/2,y+ha);
drawUpc(g,x+wa,y+ha+hb,hd+he/2);
g.drawString(""+left,x,y+totalheight);
wid=metrics.stringWidth(upc.substring(0,1));
int one=line.width;
x=x+wa+one*3;
y=y+totalheight;
int the=one*7;
for(int i=0;i<6;i++) {
g.drawString(upc.substring(i,i+1),x+(the-wid)/2,y);
x+=the;
}
x+=one*5;
for(int i=6;i<12;i++) {
g.drawString(upc.substring(i,i+1),x+(the-wid)/2,y);
x+=the;
}
x+=one*3+wc;
g.drawString(""+right,x,y);
}
private static String[] CODEA=new String[]{"0001101","0011001","0010011","0
111101","0100011","0110001","0101111","0111011","0110111","0001011"};
private static String[] CODEB=new String[]{"1110010","1100110","1101100","1
000010","1011100","1001110","1010000","1000100","1001000","1110100"};
private void drawUpc(Graphics g,int x,int y,int t) {
int x1=x;
int one=line.width;
int height=line.height;
String s="101";
String s1="101";
for(int i=0;i<6;i++) {
s+=CODEA[upc.charAt(i)-'0'];
s1+="0000000";
}
s+="01010";
s1+="01010";
for(int i=6;i<12;i++) {
s+=CODEB[upc.charAt(i)-'0'];
s1+="0000000";
}
s+="101";
s1+="101";
int n=s.length();
for(int i=0;i<n;i++) {
if(s.charAt(i)=='1') {
g.fillRect(x,y,one,height);
}
x+=one;
}
x=x1;
n=s1.length();
for(int i=0;i<n;i++) {
if(s1.charAt(i)=='1') {
g.fillRect(x,y+height,one,t);
}
x+=one;
}
}
private String upc="123456987654";
public void setUpc(String s) {
if(s!=null && s.length()==12) {
boolean valid=true;
for(int i=0;i<12;i++) {
if(!Character.isDigit(s.charAt(i))) {
valid=false;
break;
}
}
if(valid) {
this.upc=s;
this.repaint();
}
}
}
public String getUpc() {
return this.upc;
}
private String isbn="ISBN 1-2034-5678-9";
public void setIsbn(String s) {
if(s!=null && s.length()==10) {
boolean valid=true;
for(int i=0;i<10;i++) {
if(!Character.isDigit(s.charAt(i))) {
valid=false;
break;
}
}
if(valid) {
this.isbn="ISBN "+s.substring(0,1)+"-"+s.substring(1,5)+"-"+s.substring(
5,9)+"-"+s.substring(9);
this.repaint();
}
}
}
public void setIsbn(String s,int a,int b,int c,int d) {
if(s!=null && s.length()==10 && a>0 && b>0 && c>0 && d>0 && (a+b+c+d)==10)
{
boolean valid=true;
for(int i=0;i<10;i++) {
if(!Character.isDigit(s.charAt(i))) {
valid=false;
break;
}
}
if(valid) {
this.isbn="ISBN "+s.substring(0,a)+"-"+s.substring(a,a+b)+"-"+s.substrin
g(a+b,a+b+c)+"-"+s.substring(a+b+c);
this.repaint();
}
}
}
public String getIsbn() {
return this.isbn;
}
private Dimension line=new Dimension(2,100);
public void setLineDimension(Dimension dim) {
if(dim.width>0 && dim.height>0) {
this.line=new Dimension(dim.width,dim.height);
this.repaint();
}
}
public Dimension getLineDimension() {
return new Dimension(line.width,line.height);
}
private char left='9';
public void setLeft(char one) {
this.left=one;
this.repaint();
}
public char getLeft() {
return left;
}
private char right='>';
public void setRight(char one) {
this.right=one;
this.repaint();
}
public char getRight() {
return right;
}
//for test use
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
JFrame frame=new JFrame("UPC Test");
frame.setSize(500,500);
frame.getContentPane().setLayout(new BorderLayout());
//<usage>
UPC upc=new UPC();
upc.setFont(new Font("Dialog",Font.PLAIN,20)); //字体
upc.setUpc("787505379930"); //upc
upc.setIsbn("7505379933",1,4,4,1); //isbn
upc.setLineDimension(new Dimension(2,120)); //最小黑线的size
upc.setLeft('9'); //左边字符
upc.setRight('>'); //右边字符
frame.getContentPane().add(upc,BorderLayout.CENTER);
//</usage>
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
Dimension one=frame.getSize();
frame.setLocation((screen.width-one.width)/2,(screen.height-one.height)/2)
;
frame.setVisible(true);
}
}
--
为了不给smth增加负担
我决定不用花里胡梢的签名档
※ 来源:·BBS 水木清华站 smth.org·[FROM: 61.149.53.227]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:5.925毫秒