WordPress.com



import java.awt.Color;

import java.awt.Cursor;

import java.awt.EventQueue;

import java.awt.Graphics2D;

import java.awt.Toolkit;

import java.awt.event.KeyEvent;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;

import javax.swing.JLabel;

import java.awt.event.KeyAdapter;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class QRCodeGenerator extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel contentPane;

private JTextField textField;

private JLabel lblImage;

private JButton btnQrcode;

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

QRCodeGenerator frame = new QRCodeGenerator();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public QRCodeGenerator() {

setTitle("QR Code Generator - J. Marcos B.");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

// Disable the Maximize

this.setResizable(false);

// Center window on screen

this.setLocationRelativeTo(null);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

textField = new JTextField();

textField.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_ENTER) {

String phrase = textField.getText();

lblImage.setText("");

BufferedImage image = new BufferedImage(200, 200,

BufferedImage.TYPE_INT_RGB);

image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();

graphics.setColor(Color.WHITE);

graphics.fillRect(0, 0, 200, 200);

// Paint and save the image

graphics.setColor(Color.BLACK);

for (int i = 0; i < 200; i++) {

for (int j = 0; j < 200; j++) {

if (lblImage != null) {

graphics.fillRect(i, j, 1, 1);

}

}

}

}

}

});

textField.setText("Type the text and press Enter");

textField.setToolTipText("Type the text and press Enter");

textField.setBounds(10, 11, 320, 20);

contentPane.add(textField);

textField.setColumns(10);

lblImage = new JLabel("");

lblImage.setBounds(112, 42, 200, 200);

contentPane.add(lblImage);

JButton btnCopy = new JButton("Copy");

btnCopy.setToolTipText("Copy");

btnCopy.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnCopy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

BufferedImage img = new BufferedImage(lblImage.getWidth(),

lblImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = img.createGraphics();

lblImage.paintAll(g2d);

g2d.dispose();

try {

// Saved image in png

ImageIO.write(img, "png", new File("QRCode.png"));

JOptionPane.showMessageDialog(null, "Saved image.",

"QR Code Generator",

RMATION_MESSAGE);

} catch (IOException e) {

Toolkit.getDefaultToolkit().beep();

JOptionPane.showMessageDialog(null, "ERROR: " + e,

"QR Code Generator", JOptionPane.ERROR_MESSAGE);

e.printStackTrace();

}

}

});

btnCopy.setBounds(335, 42, 89, 23);

contentPane.add(btnCopy);

btnQrcode = new JButton("QR Code");

btnQrcode.setToolTipText("QR Code");

btnQrcode.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnQrcode.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String phrase = textField.getText();

lblImage.setText("");

BufferedImage image = new BufferedImage(200, 200,

BufferedImage.TYPE_INT_RGB);

image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();

graphics.setColor(Color.WHITE);

graphics.fillRect(0, 0, 200, 200);

// Paint and save the image

graphics.setColor(Color.BLACK);

for (int i = 0; i < 200; i++) {

for (int j = 0; j < 200; j++) {

if (lblImage != null) {

graphics.fillRect(i, j, 1, 1);

}

}

}

}

});

btnQrcode.setBounds(335, 10, 89, 23);

contentPane.add(btnQrcode);

JLabel lblBlog = new JLabel("");

lblBlog.setToolTipText("Blogger");

lblBlog.setHorizontalAlignment(SwingConstants.CENTER);

lblBlog.setCursor(new Cursor(Cursor.HAND_CURSOR));

lblBlog.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent arg0) {

String url = "";

BareBonesBrowserLaunch.openURL(url);

}

});

lblBlog.setBounds(10, 246, 414, 14);

contentPane.add(lblBlog);

}

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download