📄 printcard.java~51~
字号:
repaint(); return; } narrowestDim=DEFSIZE; repaint(); } /** * Gets the dimension of the narrowest bar. * @return the dim of the bar * @see #SMALL * @see #MEDIUM * @see #LARGE * @see #setDimension */ public int getDimension() { return narrowestDim; } /** * Call this method with true (or false) as argument if you want (or not) a text label inside. * @see #isTextInside */ public void setTextInside(boolean bool) { textInside=bool; repaint(); } /** * Return true if a label will be shown inside. * @see #setTextInside */ public boolean isTextInside() { return textInside; } /** * Set the style of the code to one of the two available style. * @see #CODE3OF9 * @see #CODE3OF9CHK * @see #getStyle */ public void setStyle(int styl) { switch (styl) { case CODE3OF9: case CODE3OF9CHK: style=styl; Encode(); return; } style=CODE3OF9; Encode(); } /** * return the style of the barcode currently used. * @see #CODE3OF9 * @see #CODE3OF9CHK * @see #setStyle */ public int getStyle() { return style; } /** * set the color of the background to the specified color. * @param c the color to be used * @see #getBackgroundColor * @see #setForegroundColor * @see #getForegroundColor */ public void setBackground(Color c) { backColor=c; repaint(); } /** * return the color used for the background. * @return the color used * @see #setBackgroundColor * @see #setForegroundColor * @see #getForegroundColor */ public Color getBackground() { return backColor; } /** * set the color of the foreground to the specified color. * @param c the color to be used * @see #getBackgroundColor * @see #setBackgroundColor * @see #getForegroundColor */ public void setForeground(Color c) { foreColor=c; repaint(); } /** * return the color used for the foreground. * @return the color used * @see #setBackgroundColor * @see #getBackgroundColor * @see #setForegroundColor */ public Color getForeground() { return foreColor; } /** * Sets the font of the component. * @param fnt the font * @see #getFont */ public void setFont(Font fnt) { font=fnt; repaint(); } /** * Gets the font of the component. * @see #setFont */ public Font getFont() { return font; } /** * Set the alignment of text inside the bar code. Three option are available. * @see #BASELINE * @see #MIDDLELINE * @see #TOPLINE * @see #getTextAlign */ public void setTextAlign(int align) { switch (align) { case BASELINE: case MIDDLELINE: case TOPLINE: textAlign=align; repaint(); return; } textAlign=TOPLINE; repaint(); } /** * Set the alignment of text inside the bar code. Three option are available. * @see #BASELINE * @see #MIDDLELINE * @see #TOPLINE * @see #setTextAlign */ public int getTextAlign() { return textAlign; } /** * Paints the component. * @param g the specified Graphics window */ public synchronized void paint (Graphics g) { int width=500; int height=260; g.drawRect(0,0,498,258); Font ft=new Font("Courier",0, 14); g.setFont(ft); FontMetrics fm=g.getFontMetrics(font); int fmAscent=fm.getAscent(); labelLength= filledStringToEncode.length()*16*narrowestDim; marginWidth=(width-labelLength)/2; marginHeight=(height-labelLength)/2; if ( textInside ) { if ( textAlign==BASELINE ) labelHeight=height; if ( textAlign==MIDDLELINE ) labelHeight=height-(int)(fmAscent/2); if ( textAlign==TOPLINE ) labelHeight=height-fmAscent; } else { labelHeight=height; } g.setColor(backColor); g.fillRect(1,1,width-1,height-1); int length=encodedString.length();//149 int yp=(int)(labelHeight/4); //=57 int x0=0,y0=0,x=0,y=0; int wid=0; System.out.println("length="+length); System.out.println("yp="+yp); x0=(int) marginWidth; //130 y0=0; x=0; y=0; for ( int i=0;i<length ;i++ ) { if (i%2==0) g.setColor(foreColor); else g.setColor(backColor); if ( Character.digit(encodedString.charAt(i),10)==1 ) wid=(int)(wideToNarrowRatio*narrowestDim); else wid=(int)narrowestDim; g.fillRect(x+x0,(int)(labelHeight/4+2*fm.getHeight()+7+yp),wid,40); //130 //130 //1\u00BBò\u00D5\u00DF3 57 x+=wid; } // g.drawString( // stringName+" "+" "+stringPosition,width/4,(int)labelHeight/2); g.drawString("姓名:"+" "+stringName,113, 100); g.drawString("职位:"+" "+stringPosition,113,130); g.drawString("单位:"+" "+stringCompany,113,160); if ( textInside ) { int k=fm.stringWidth(filledStringToEncode); int h=250; System.out.println(h);//250 int mlk2=(int)(marginWidth+labelLength/2-k/2); g.setColor(backColor); g.fillRect(mlk2,h-fmAscent,k,fmAscent); //171,229,159,21 int kkkkkkk=h-fmAscent; System.out.println("mlk2="+mlk2); System.out.println("h-fmAscent="+kkkkkkk); System.out.println("k="+k); System.out.println("fmAscent="+fmAscent); g.setColor(foreColor); g.drawString(filledStringToEncode,mlk2+20,h-8);//171,250//\u00CA\u00B9\u00B4\u00F8\u00D0\u00C7\u00BA\u00C5\u00B5\u00C4×\u00D6·\u00FB\u00B4\u00AE\u00BF\u00C9\u00D2\u00D4\u00C9\u00CF\u00C9\u00FD } Image printImage; printImage = new javax.swing.ImageIcon("e:/image/sjtulogo1.gif").getImage(); int iwidth=printImage.getWidth(this);//40 int iheight=printImage.getHeight(this);//40 double pwidth=width/5;//=100 double pheight=labelHeight/3;//76.33333333333333333333333 double scalex=pwidth/iwidth;//2.5 double scaley=pheight/iheight;//1.90833333333 if(scalex>scaley) g.drawImage(printImage, 70, 10,40,40, null); //g.clearRect( 1, 1,(int)(iwidth*scaley),(int)(pheight)); else g.drawImage(printImage, 70, 10,40,40, null); ft=new Font("Courier",0, 20); g.setFont(ft); g.drawString("上海交大研究生代表大会",160,35); } private void stringValidate() throws IllegalArgumentException { int len=stringToEncode.length(); System.out.println("len="+len); for ( int i=0;i<len ;i++ ) { if (alphabet3of9.indexOf(stringToEncode.charAt(i))==-1 || stringToEncode.indexOf("*")!=-1) throw new IllegalArgumentException("only digits and upper case letter for 3of9"); } } private void Encode() { filledStringToEncode=new String(stringToEncode); if ( style==CODE3OF9CHK ) { int leng=stringToEncode.length(); int sum=0; for ( int i=0;i<leng ;i++ ) { sum+=alphabet3of9.indexOf(stringToEncode.charAt(i)); } sum=sum%43; filledStringToEncode+=alphabet3of9.charAt(sum); } filledStringToEncode="*"+filledStringToEncode+"*"; encodedString=""; int length=filledStringToEncode.length(); for ( int i=0;i<length-1 ;i++ ) { encodedString += coded3of9Char[alphabet3of9.indexOf(filledStringToEncode.charAt(i))]; encodedString += intercharacterGap; } encodedString += coded3of9Char[alphabet3of9.indexOf(filledStringToEncode.charAt(length-1))]; repaint(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -