Langsung Print Dengan Bahasa Java

sesuai permintaan mas gilang pada commentar Contoh Code Java Sederhana berikut saya sertakan source code dari bahasa java untuk Langsung Cetak Printer Dengan Bahasa Java moga bermanfaat dan silahkan di kembangkan sendiri, semangat…😀

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
public class JustPrint extends JPanel implements ActionListener, Printable{
public JustPrint() {
//memberi background dan membuat tombol
setBackground(Color.black);
JButton tombol = new JButton(“Just Print”);
tombol.addActionListener(this);
add(tombol);
}

public void actionPerformed(ActionEvent e) {
//untuk pemanggilan printer beserta penanganan error
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
try { printJob.print(); }
catch (Exception PrintException) { }
}

public int print(Graphics g,PageFormat pf, int pi) throws PrinterException {
//untuk eksekusi printer
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
g.translate(100, 100);
Font  f = new Font(“Monospaced”,Font.PLAIN,12);
g.setFont (f);
paint (g);
return Printable.PAGE_EXISTS;
}

public static void main(String s[]) {
//memanggil dan menyesuaikan frame serta tombol
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
Frame f = new Frame(“Just Wanna Print”);
f.addWindowListener(l);
f.add(“Center”, new JustPrint());
f.pack();
f.setSize(new Dimension(400,300));
f.show();
}
}

berikut tampilkan prigramnya :

berikut tampilan setelah tombol di klik :
misalkan saya gunakan Canon iP1300

referensi : defeloper.sun

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s