[Pemrograman]Print Screen menggunakan Java Swing



Hello world !
Di sesi kali ini kita akan belajar bagaimana cara mengeprint layar menggunakan java swing
setPrintable harus memiliki metode cetak yang menjelaskan bagaimana cara mengirim gambar ke printer. maka kita harus mulai menggambar objek Graphics untuk Graphics2D dulu,membuat skala resolusi untuk printer, dan memanggil metode cat komponen dengan skala Graphics2D.

Langkah 1 :
Buat class “PrintExample.java” untuk menampilkan frame

package DemoPrintScreenJava;

import java.awt.*;
import javax.swing.*;
import com.jtattoo.plaf.mcwin.McWinLookAndFeel;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.geom.*;
/*
@Anugrah Bagus Susilo
*/
public class PrintExample extends JFrame implements ActionListener {
	public static void main(String[] args) throws UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(new McWinLookAndFeel());
		new PrintExample();
	}

	public PrintExample() {
		super("Demo Print Screen");
		WindowShow.setNativeLookAndFeel();
		addWindowListener(new ExitListener());
		Container content = getContentPane();
		JButton printButton = new JButton("Print");
		printButton.addActionListener(this);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.white);
		buttonPanel.add(printButton);
		content.add(buttonPanel, BorderLayout.SOUTH);
		DrawingPane drawingPanel = new DrawingPane();
		content.add(drawingPanel, BorderLayout.CENTER);
		setSize(800,300);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent event) {
		PrintableDocument.printComponent(this);
	}

	class DrawingPane extends JPanel {
		private int fontSize = 90;
		private String message = "Newbie IImu";
		private int messageWidth;

		public DrawingPane() {
			setBackground(Color.green);
			Font font = new Font("Serif", Font.PLAIN, fontSize);
			setFont(font);
			FontMetrics metrics = getFontMetrics(font);
			messageWidth = metrics.stringWidth(message);
			int width = messageWidth*5/3;
			int height = fontSize*3;
			setPreferredSize(new Dimension(width, height));
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D graph = (Graphics2D)g;
			int x = messageWidth/10;
			int y = fontSize*5/2;
			graph.translate(x, y);
			graph.setPaint(Color.white);

			AffineTransform origTransform = graph.getTransform();
			graph.shear(-0.95, 0);
			graph.scale(1, 3);
			graph.drawString(message, 0, 0);
			graph.setTransform(origTransform);
			graph.setPaint(Color.black);
			graph.drawString(message, 0, 0);
		}
	}
}

Langkah 2:
Buat “PrintableDocument.java” untuk mencetak nilai tampilan.
Dalam langkah ini menggunakan metode print(). Metode ini set halaman.
PrinterJob: Sebuah benda harus dibuat dengan PrinterJob menggunakan getPrinterJob() statis metode.

Kelas PrinterJob adalah kelas utama yang mengontrol pencetakan. Sebuah aplikasi panggilan metode di kelas ini untuk mengatur pekerjaan, opsional untuk memanggil dialog cetak dengan pengguna, dan kemudian untuk mencetak halaman dari pekerjaan.

Graphic2D:

Kelas ini Graphics2D memperluas kelas Graphics untuk memberikan kontrol yang lebih canggih atas geometri, transformasi koordinat, manajemen warna, dan tata letak teks. Ini adalah kelas dasar untuk rendering 2-dimensi bentuk, teks dan gambar pada platform Java.

package DemoPrintScreenJava;

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
/*
@Anugrah Bagus Susilo
*/
public class PrintableDocument implements Printable {
  private Component compent;

  public static void printComponent(Component c) {
    new PrintableDocument(c).print();
  }

  public PrintableDocument(Component compent) {
    this.compent = compent;
  }

  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if(printJob.printDialog())
      try {
      printJob.print();
    }
    catch(PrinterException pe) {
      System.out.println("Error printing: " + pe);
    }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    }
    else {
      Graphics2D graph = (Graphics2D)g;
      graph.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      disableBuffering(compent);
      compent.paint(graph);
      enableBuffering(compent);
      return(PAGE_EXISTS);
    }
  }

  public static void disableBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  public static void enableBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
}

Langkah 3: Buat kelas “WindowShow.java” untuk mengeset Look and Feel.

package DemoPrintScreenJava;

import javax.swing.*;
import com.jtattoo.plaf.mcwin.McWinLookAndFeel;
import java.awt.*;
import java.awt.event.*;
/*
@Anugrah Bagus Susilo
*/

public class WindowShow { 

  public static void setNativeLookAndFeel() {
    try{
      UIManager.setLookAndFeel(new McWinLookAndFeel());
    }
    catch(Exception e) {
      System.out.println("Error setting : " + e);
    }
  }

  public static JFrame openInJFrame
     (Container content, int width, int height, String title, Color bgColor) {
    class ExitListener extends WindowAdapter {
      public void windowClosing(WindowEvent event) {
        System.exit(0);
      }
    }
    JFrame frame = new JFrame(title);
    frame.setBackground(bgColor);
    content.setBackground(bgColor);
    frame.setSize(width, height);
    frame.setContentPane(content);
    frame.addWindowListener(new ExitListener());
    frame.setVisible(true);
    return(frame);
  }

  public static JFrame openInJFrame
                     (Container content, int width, int height, String title) {
    return(openInJFrame(content, width, height, title, Color.green));
  }

  public static JFrame openInJFrame(Container content, int width, int height) {
    return(openInJFrame(content, width, height,content.getClass().getName(), Color.green));
  }
}

Langkah 4: Buat kelas “ExitListener.java” untuk melakukan action.

package DemoPrintScreenJava;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/*
@Anugrah Bagus Susilo
*/

public class ExitListener implements WindowListener {

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

}

Nah kalau benar maka outputnya akan seperti ini:

download project ini:
Print Screen menggunakan java
referensi : http://www.roseindia.net/java/example/java/swing/Print.shtml
Happy Coding !! ^^

Anugrah Bagus Susilo

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s