📄 barcodesample.java
字号:
package barcode.awt.sample;
// BarcodeSample.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 BarcodeSample
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.PrintJob;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import com.bokai.barcodes.Barcode;
import com.bokai.barcodes.BarcodeExtraTextPosition;
import com.bokai.barcodes.BarcodeOrientation;
import com.bokai.barcodes.BarcodeTextPosition;
import com.bokai.barcodes.BarcodeType;
import com.bokai.barcodes.CheckCharShowMode;
import com.bokai.drawing.SimpleColor;
import com.bokai.drawing.SimpleFont;
public class BarcodeSample extends Panel implements ItemListener, TextListener,
ActionListener {
private static final long serialVersionUID = -9020479145381685773L;
private Frame _frame;
private Barcode _barcode;
private Choice _type;
private Choice _orientation;
private Choice _renderMethod;
private Button _print;
private TextField _data;
private TextField _addOnData;
private TextArea _extraText;
private Canvas _canvas;
@SuppressWarnings("deprecation")
public static void main(String args[]) throws Exception {
Frame f = new Frame("Bokai Barcode/Java V4.0 Sample");
f.add(new BarcodeSample(f));
f.pack();
f.show();
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
class MyCanvas extends Canvas {
private static final long serialVersionUID = -1865815656341507710L;
public void paint(Graphics g) {
doPaint(g);
}
public Dimension getPreferredSize() {
return new Dimension(280, 280);
}
}
public BarcodeSample(Frame frame) throws Exception {
_frame = frame;
GridBagLayout gblayout;
GridBagConstraints gbc;
setLayout(new FlowLayout(FlowLayout.LEFT));
Panel top = new Panel(gblayout = new GridBagLayout());
add(top);
Component comp;
int k = 0;
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(comp = new Label("Barcode Type:"), gbc);
top.add(comp);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_type = new Choice(), gbc);
_type.addItemListener(this);
top.add(_type);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(comp = new Label("Data:"), gbc);
top.add(comp);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_data = new TextField(38), gbc);
_data.addTextListener(this);
top.add(_data);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(comp = new Label("Add-On Data:"), gbc);
top.add(comp);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_addOnData = new TextField(38), gbc);
_addOnData.addTextListener(this);
top.add(_addOnData);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(comp = new Label("Extra Text:"), gbc);
top.add(comp);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_extraText = new TextArea("", 1, 38,
TextArea.SCROLLBARS_NONE), gbc);
_extraText.addTextListener(this);
top.add(_extraText);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(comp = new Label("Orientation:"), gbc);
top.add(comp);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_orientation = new Choice(), gbc);
_orientation.addItemListener(this);
top.add(_orientation);
_orientation.addItem("bottomFacing");
_orientation.addItem("rightFacing");
_orientation.addItem("topFacing");
_orientation.addItem("leftFacing");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(comp = new Label("Render Method:"), gbc);
top.add(comp);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_renderMethod = new Choice(), gbc);
_renderMethod.addItemListener(this);
top.add(_renderMethod);
_renderMethod.addItem("Image");
_renderMethod.addItem("Draw");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 0;
gbc.gridy = k;
gblayout.setConstraints(_print = new Button("Print Barcode"), gbc);
_print.addActionListener(this);
top.add(_print);
if (frame == null) {
_print.setEnabled(false);
_print.setLabel("Applet can't print");
}
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = k++;
gblayout.setConstraints(_canvas = new MyCanvas(), gbc);
top.add(_canvas);
java.util.Enumeration en = Barcode.enumerateBarcodeTypes();
while (en.hasMoreElements()) {
BarcodeType barcodeType = (BarcodeType) en.nextElement();
String str = new Integer(barcodeType.getType()).toString();
if (str.length() == 1)
str = "0" + str;
_type.addItem(str + " " + barcodeType.getName());
}
try {
_barcode = new Barcode(Barcode.BCT_CODE39);
} catch (Exception e) {
e.printStackTrace();
}
_type.select(0);
barcodeTypeSelected(_type.getSelectedItem());
setSize(480, 510);
}
private void barcodeTypeSelected(String text) {
try {
int type = Integer.parseInt(text.substring(0, text.indexOf(' ')),
10);
_barcode.setBarcodeType(type);
_data.setText(new String(_barcode.getActualData()));
_addOnData.setText(_barcode.getActualAddOnData() == null ? ""
: new String(_barcode.getActualAddOnData()));
_extraText.setText("");
_canvas.repaint();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getItemSelectable() == _type) {
barcodeTypeSelected((String) e.getItem());
} else {
_canvas.repaint();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == _print)
doPrint();
}
public void textValueChanged(TextEvent e) {
java.awt.TextComponent text = (java.awt.TextComponent) e.getSource();
try {
if (text == _data)
_barcode.setData(text.getText());
else if (text == _addOnData)
_barcode.setAddOnData(text.getText());
else if (text == _extraText)
_barcode.setExtraText(text.getText());
_canvas.repaint();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private int getOrientation() {
return _orientation.getSelectedIndex();
}
public void doPaint(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.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));
// ...
}
private void drawBarcode(Graphics g, int x, int y) {
// minimum number of pixels that the barcode requires
int width = _barcode.getModuleCount();
// unit of measure is (equivalent) screen pixels even on printer
Rectangle rect = new Rectangle(x, y, width, 80);
// customizeBarcode();
if (_renderMethod.getSelectedIndex() == 0) {
//
// METHOD 1: generate an image and then draw image
//
int scale = 1;
int orient = getOrientation();
_barcode.setOrientation(orient);
if (orient == BarcodeOrientation.leftFacing
|| orient == BarcodeOrientation.rightFacing) {
int tmp = rect.width;
rect.width = rect.height;
rect.height = tmp;
}
try {
java.awt.Image img = _barcode.makeSimpleImage(rect.width,
rect.height, false, scale, null);
// java.awt.Image img = _barcode.makeImage(rect.width,
// rect.height, false, scale, null);
g.drawImage(img, rect.x, rect.y, this);
// Call this instead, e.g., to draw a smaller image than the
// generated image size;
// just use a smaller with and height in this call:
// g.drawImage(img, rect.x, rect.y, rect.width, rect.height,
// this);
// if can also call this to save the image to a file,
// _barcode.saveSimpleImage(Barcode.BARCODE_PNG, rect.width,
// rect.height, false, scale, null, "c:\\tmp01.png");
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
//
// METHOD 2: call draw() directly, bypassing image generation
//
// font, color, orientation, etc., are not managed by the component
// if you use draw().
// Also, instead of various properties, you need to use draw flags
// BDF_*; you
// can convert the property values to drawFlags via
// getDrawFlagsFromProperties().
g.setColor(Color.black);
_barcode.draw(g, rect, _barcode.getDrawFlagsFromProperties(),
Color.white);
}
}
public void doPrint() {
PrintJob printJob = getToolkit().getPrintJob(_frame, "Print Barcode",
null);
if (printJob != null) {
Graphics g = printJob.getGraphics();
if (g != null) {
drawBarcode(g, 50, 50);
g.dispose();
}
printJob.end();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -