📄 barcodesimplesample.java
字号:
package barcode.awt.sample;
// BarcodeSimpleSample.java
//
// Bokai Barcode/Java v4.0 Sample Program
// Copyright (c) 2005 Bokai Corporation
// http://www.bokai.com
// To run this sample application,
// - include "." and "easybar.jar" in your CLASSPATH
// - run: java BarcodeSimpleSample
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.PrintJob;
import com.bokai.barcodes.Barcode;
import com.bokai.barcodes.BarcodeExtraTextPosition;
import com.bokai.barcodes.BarcodeOrientation;
import com.bokai.barcodes.BarcodeTextPosition;
import com.bokai.barcodes.CheckCharShowMode;
import com.bokai.drawing.SimpleColor;
import com.bokai.drawing.SimpleFont;
public class BarcodeSimpleSample extends Frame {
private static final long serialVersionUID = -7845387022053359195L;
private Barcode _barcode;
private void drawBarcode(Graphics g, int x, int y) {
int w, h;
int orient = _barcode.getOrientation();
if (orient == BarcodeOrientation.leftFacing
|| orient == BarcodeOrientation.rightFacing) {
w = 50;
h = 1;
} else {
w = 1;
h = 50;
}
try {
java.awt.Image img = _barcode.makeSimpleImage(w, h, true, 1, null);
g.drawImage(img, 50, 100, null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void paint(Graphics g) {
drawBarcode(g, 10, 10);
}
@SuppressWarnings("unused")
private void customizeBarcode() {
// here you can set other properties on the barcode object _barcode,
// which
// have no user interface elements in this sample app, e.g.,
_barcode.setOrientation(BarcodeOrientation.bottomFacing);
// also: BarcodeOrientation.topFacing, BarcodeOrientation.leftFacing,
// BarcodeOrientation.rightFacing
_barcode.setTextAlign(BarcodeTextPosition.below); // also: above,
// hidden
_barcode.setExtraTextPosition(BarcodeExtraTextPosition.above); // also:
// below
_barcode.setCheckCharShowMode(CheckCharShowMode.auto); // also: hide,
// show
_barcode.setStretchText(true); // also: false
_barcode.setShowCode39StartStop(true); // also: false
_barcode.setForeSimpleColor(new SimpleColor(0, 0, 0));
_barcode
.setSimpleFont(new SimpleFont("SansSerif", SimpleFont.PLAIN, 11));
// ...
}
public void doPrint() {
PrintJob printJob = getToolkit().getPrintJob(this, "Print Barcode",
null);
if (printJob != null) {
Graphics g = printJob.getGraphics();
if (g != null) {
drawBarcode(g, 50, 50);
g.dispose();
}
printJob.end();
}
}
public BarcodeSimpleSample(String title) throws Exception {
super(title);
try {
_barcode = new Barcode(Barcode.BCT_CODE39);
} catch (Exception e) {
e.printStackTrace();
}
_barcode.setData("12345678");
// optionally customize the barcode
// customizeBarcode();
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
doPrint();
}
});
setSize(480, 380);
}
@SuppressWarnings("deprecation")
public static void main(String args[]) throws Exception {
Frame f = new BarcodeSimpleSample(
"Bokai Barcode/Java V4.0 Simple Sample");
f.show();
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -