import com.aspose.barcode.*;

import java.awt.*;
import java.awt.image.RenderedImage;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;


public class AwtSample extends Panel implements TextListener, ItemListener, ActionListener {
    private static final long serialVersionUID = 1L;
    private Frame _frame;
    private TextField _codeText;
    private TextField _xDimension;
    private TextField _rotationAngle;
    private Canvas _canvas;
    private Choice _symbology;
    private Button _print;
    private Button _save;
    private Checkbox _border;

    public static void main(String args[])
            throws Exception {
        Frame f = new Frame("Aspose BarCode for Java sample");
        f.add(new AwtSample(f));
        f.pack();
        f.setSize(400, 400);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    class MyCanvas extends Canvas {
        private BarCodeBuilder _bb = null;
        private static final long serialVersionUID = 1L;

        public BarCodeBuilder getBarCodeBuilder() {
            if (_bb == null) {
                return _bb = new BarCodeBuilder();
            } else {
                return _bb;
            }
        }

        public void paint(Graphics g) {
            BarCodeBuilder b = this.getBarCodeBuilder();
            //...
            BufferedImage bimg = (BufferedImage) b.generateBarCodeImage();
            int w = bimg.getWidth();
            int h = bimg.getHeight();
            int[] rgb = new int[w * h];
            bimg.getRGB(0, 0, w, h, rgb, 0, w);
            //...
            if (rgb.length > 0) {
                System.out.println("RGB OK.");
            }
            this.getBarCodeBuilder().render(g);
        }

        public Dimension getPreferredSize() {
            return new Dimension(1200, 300);
        }
    }

    public void doPaint(Graphics g) {
        BarCodeBuilder b;
        b = new BarCodeBuilder();

        b.render(g);


    }

    public AwtSample(Frame f) {
        _frame = f;
        GridBagLayout l = new GridBagLayout();
        GridBagConstraints gc;
        Component com;
        int dy = 0;

        setLayout(new FlowLayout(FlowLayout.LEFT));
        Panel panel = new Panel(l);
        add(panel);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        //placeholder
        l.setConstraints(com = new Label("                                                                                                      "), gc);
        panel.add(com);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 0;
        gc.gridy = dy;
        l.setConstraints(com = new Label("CodeText:"), gc);
        panel.add(com);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        l.setConstraints(_codeText = new TextField(27), gc);
        _codeText.setText("12345678");
        _codeText.addTextListener(this);
        panel.add(_codeText);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 0;
        gc.gridy = dy;
        l.setConstraints(com = new Label("Symbology:"), gc);
        panel.add(com);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        l.setConstraints(_symbology = new Choice(), gc);
        _symbology.addItem("CODE128");
        _symbology.addItem("CODABAR");
        _symbology.addItem("CODE11");
        _symbology.addItem("CODE39STANDARD");
        _symbology.addItem("CODE39EXTENDED");
        _symbology.addItem("CODE93STANDARD");
        _symbology.addItem("CODE93EXTENDED");
        _symbology.addItem("STANDARD2OF5");
        _symbology.addItem("INTERLEAVED2OF5");
        _symbology.addItem("UPCA");
        _symbology.addItem("UPCE");
        _symbology.addItem("MSI");
        _symbology.addItem("BOOKLANDEAN");
        _symbology.addItem("DATAMATRIX");
        _symbology.addItem("EAN128");
        _symbology.addItem("EAN13");
        _symbology.addItem("EAN14");
        _symbology.addItem("EAN8");
        _symbology.addItem("ITF14");
        _symbology.addItem("PDF417");
        _symbology.addItem("MACROPDF417");
        _symbology.addItem("PLANET");
        _symbology.addItem("POSTNET");
        _symbology.addItem("QR");
        _symbology.addItem("SSCC18");
        _symbology.addItemListener(this);
        panel.add(_symbology);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 0;
        gc.gridy = dy;
        l.setConstraints(com = new Label("xDimension:"), gc);
        panel.add(com);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        l.setConstraints(_xDimension = new TextField(27), gc);
        _xDimension.addTextListener(this);
        _xDimension.setText("0.6");
        panel.add(_xDimension);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 0;
        gc.gridy = dy;
        l.setConstraints(com = new Label("rotationAngle:"), gc);
        panel.add(com);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        l.setConstraints(_rotationAngle = new TextField(27), gc);
        _rotationAngle.addTextListener(this);
        _rotationAngle.setText("0");
        panel.add(_rotationAngle);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        l.setConstraints(_border = new Checkbox(), gc);
        _border.setLabel("Border");
        _border.setState(true);
        _border.addItemListener(this);
        panel.add(_border);

        Panel buttons = new Panel();
        _print = new Button("Print");
        _print.addActionListener(this);
        _save = new Button("Save");
        _save.addActionListener(this);
        _save.setSize(100, 20);
        buttons.add(_save);
        buttons.add(_print);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy++;
        l.setConstraints(buttons, gc);
        panel.add(buttons);

        gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.gridx = 1;
        gc.gridy = dy;
        l.setConstraints(_canvas = new MyCanvas(), gc);
        panel.add(_canvas);

        _canvas.repaint();
    }

    public void textValueChanged(TextEvent e) {
        TextComponent text = (TextComponent) e.getSource();
        try {
            if (text == _codeText) {
                ((MyCanvas) _canvas).getBarCodeBuilder().setCodeText(_codeText.getText());
            } else if (text == _xDimension) {
                ((MyCanvas) _canvas).getBarCodeBuilder().setXDimension(Float.parseFloat(_xDimension.getText()));
            } else if (text == _rotationAngle) {
                ((MyCanvas) _canvas).getBarCodeBuilder().setRotationAngle(Float.parseFloat(_rotationAngle.getText()));
            }
            _canvas.repaint();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public void itemStateChanged(ItemEvent e) {
        try {
            if (e.getItemSelectable() == _symbology) {
                String symbology = (String) e.getItem();
                if (symbology.equals("CODABAR")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODABAR);
                } else if (symbology.equals("CODE11")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODE11);
                } else if (symbology.equals("CODE128")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODE128);
                } else if (symbology.equals("CODE39STANDARD")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODE39STANDARD);
                } else if (symbology.equals("CODE39EXTENDED")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODE39EXTENDED);
                } else if (symbology.equals("CODE93STANDARD")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODE93STANDARD);
                } else if (symbology.equals("CODE93EXTENDED")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.CODE93EXTENDED);
                } else if (symbology.equals("INTERLEAVED2OF5")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.INTERLEAVED2OF5);
                } else if (symbology.equals("STANDARD2OF5")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.STANDARD2OF5);
                } else if (symbology.equals("UPCA")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.UPCA);
                } else if (symbology.equals("UPCE")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.UPCE);
                } else if (symbology.equals("MSI")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.MSI);
                } else if (symbology.equals("BOOKLANDEAN")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.BOOKLANDEAN);
                } else if (symbology.equals("DATAMATRIX")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.DATAMATRIX);
                } else if (symbology.equals("EAN128")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.EAN128);
                } else if (symbology.equals("EAN13")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.EAN13);
                } else if (symbology.equals("EAN14")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.EAN14);
                } else if (symbology.equals("EAN8")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.EAN8);
                } else if (symbology.equals("ITF14")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.ITF14);
                } else if (symbology.equals("PDF417")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.PDF417);
                }else if (symbology.equals("MACROPDF417")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.MACROPDF417);
                }
                else if (symbology.equals("PLANET")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.PLANET);
                } else if (symbology.equals("POSTNET")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.POSTNET);
                } else if (symbology.equals("QR")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.QR);
                } else if (symbology.equals("SSCC18")) {
                    ((MyCanvas) _canvas).getBarCodeBuilder().setSymbology(Symbology.SSCC18);
                }
                this._canvas.repaint();
            } else if (e.getItemSelectable() == _border) {
                ((MyCanvas) _canvas).getBarCodeBuilder().setBorderVisible(_border.getState());
                this._canvas.repaint();
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e) {
        try {
            if (e.getSource() == _print) {
                this.doPrint();
            } else if (e.getSource() == _save) {
                this.doSave();
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void doPrint() {
        PrintJob printJob = getToolkit().getPrintJob(_frame, "Print", null);
        if (printJob != null) {
            Graphics g = printJob.getGraphics();
            if (g != null) {
                ((MyCanvas) _canvas).getBarCodeBuilder().render(g);
            }
            printJob.end();
        }
    }

    private void doSave()
            throws Exception {
        JFileChooser fc = new JFileChooser();
        String[] exts = ((MyCanvas) _canvas).getBarCodeBuilder().getSupportedImageFormats();
        for (int i = 0; i < exts.length; i++) {
            Filter filter = new Filter(exts[i], exts[i] + " files");
            fc.addChoosableFileFilter(filter);
        }
        if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            ((MyCanvas) _canvas).getBarCodeBuilder().save(fc.getSelectedFile().getPath());
        }
    }

    class Filter extends FileFilter {
        private String desc = "";
        private String ext = "";

        public Filter(String ext, String desc) {
            this.desc = desc;
            this.ext = ext;
        }

        public boolean accept(File f) {
            if (f.isDirectory() || f.getName().endsWith(this.ext)) {
                return true;
            }
            return false;
        }

        public String getDescription() {
            return this.desc;
        }
    }
}  
