📄 barcode.java
字号:
*
* @return The width of this component
*/
public int getWidth() {
return (int) getActualSize().getWidth();
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @return The height of this component
*/
public int getHeight() {
return (int) getActualSize().getHeight();
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @return The bounds of this component
*/
public Rectangle getBounds() {
return getBounds(new Rectangle());
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @param rv The rectangle to set the bounds on
* @return The updated rv
*/
public Rectangle getBounds(Rectangle rv) {
rv.setBounds(getX(), getY(), (int) getActualSize().getWidth() + getX(),
(int) getActualSize().getHeight() + getY());
return rv;
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @return The preferred size of this component
*/
public Dimension getPreferredSize() {
return getActualSize();
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @return The minimum size of this component
*/
public Dimension getMinimumSize() {
return getActualSize();
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @return The maximum size of this component
*/
public Dimension getMaximumSize() {
return getActualSize();
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @return The actual size of this component
*/
public Dimension getSize() {
return getActualSize();
}
/**
* Renders this <code>Barcode</code> at the specified location in
* the specified {@link java.awt.Graphics2D Graphics2D} context.
* The origin of the layout is placed at x, y. Rendering may touch
* any point within <code>getBounds()</code> of this position. This
* leaves the <code>g2</code> unchanged.
*
* @param g The graphics context
* @param x The horizontal value of the upper left co-ordinate of the bounding box
* @param y The vertical value of the upper left co-ordinate of the bounding box
*/
public void draw(Graphics2D g, int x, int y) throws OutputException {
this.x = x;
this.y = y;
Output output = new GraphicsOutput(g, font, getForeground(), getBackground());
size = draw(output, x, y, barWidth, barHeight);
}
public void output(Output output) throws OutputException {
draw(output, 0, 0, barWidth, barHeight);
}
protected abstract Module[] encodeData();
protected abstract Module calculateChecksum();
protected abstract Module getPreAmble();
protected abstract Module getPostAmble();
protected abstract Dimension draw(Output output, int x, int y, int barWidth, int barHeight) throws OutputException;
/**
* Returns the text that will be displayed underneath the barcode (if requested).
*
* @return The text label for the barcode
*/
public String getLabel() {
if (label != null) {
return label;
} else {
return beautify(data);
}
}
/**
* Sets the human readable text to be displayed underneath the barcode.
* If set to null then the text will automaticaly be generated.
*
* @param label the human readable barcode text
* @see #getLabel()
*/
public void setLabel(String label) {
this.label = label;
}
protected int calculateMinimumBarHeight(int resolution) {
return 0;
}
/**
* From {@link javax.swing.JComponent JComponent}.
*
* @param g The graphics to paint the component onto
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getInsets();
try {
draw((Graphics2D) g, insets.left, insets.top);
} catch (OutputException e) {
// Don't draw anything
}
}
// TODO: Move this to the output
protected int getResolution() {
if (resolution > 0) {
return resolution;
}
return EnvironmentFactory.getEnvironment().getResolution();
}
protected int drawModule(Module module, Output output, int x, int y, int barWidth, int barHeight) throws OutputException {
if (module == null) {
return 0;
}
return module.draw(output, x, y, barWidth, barHeight);
}
protected String beautify(String s) {
StringBuffer buf = new StringBuffer();
StringCharacterIterator iter = new StringCharacterIterator(s);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
if (Character.isDefined(c) && !Character.isISOControl(c)) {
buf.append(c);
}
}
return buf.toString();
}
private void invalidateSize() {
size = null;
}
private Dimension getActualSize() {
if (size == null) {
size = calculateSize();
}
return size;
}
private Dimension calculateSize() {
Dimension d = new Dimension();
if (EnvironmentFactory.getEnvironment() instanceof HeadlessEnvironment) {
try {
d = draw(new SizingOutput(font, getForeground(), getBackground()), 0, 0, barWidth, barHeight);
} catch (OutputException e) {
}
} else {
try {
FontMetrics fontMetrics = null;
if (font != null) {
fontMetrics = getFontMetrics(font);
}
d = draw(new SizingOutput(font, fontMetrics, getForeground(), getBackground()), 0, 0, barWidth, barHeight);
} catch (OutputException e) {
}
}
return d;
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
try
{
this.draw( (Graphics2D) g, 0, 0);
return Printable.PAGE_EXISTS;
}
catch (OutputException ex)
{
throw new PrinterException(ex.getMessage());
}
}
public String toString()
{
return this.getData();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -