Bokai Barcode/Java AWT Sample
To run this sample application,
- Include "." and "easybar.jar" in your CLASSPATH
- javac BarcodeSimpleSample.java
- java BarcodeSimpleSample
// BarcodeSimpleSample.java
//
import java.awt.*;
import com.bokai.barcodes.*;
import com.bokai.drawing.*;
public class BarcodeSimpleSample extends Frame
{
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);
}
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);
}
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); }
});
}
}