소프트웨어 (과거)/자바 GUI & C# 등...

2. 자바 프린터 사용 방법

dgmayor 2022. 1. 28. 09:05
728x90


import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.PrintJob;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
  * This application illustrates printing in Java. The
  * frame displays an image.
 **/
public class Printer extends JFrame
             implements ActionListener
{

  PrintImagePanel fImgPanel;
  String fImageName = "/turtle.jpg"; //★파일을 구해서 인쇄하도록!
  Image fImg;

  Printer (String name) {
    super (name);
  } // ctor

  /** Create the interface. **/
  public void init () {
    Container content_pane = getContentPane ();

    // Grab the image.
    fImg = getToolkit ().getImage (fImageName);

    // Create an instance of DrawingPanel
    fImgPanel = new PrintImagePanel (fImg);
    buildMenu (this);

    // Add the ImagePanel to the contentPane.
    content_pane.add (fImgPanel);
  }

  /** Create the "File" menu for the frame menu bar.**/
  void buildMenu (JFrame frame) {
    MenuBar mb = new MenuBar ();
    Menu m = new Menu ("File");

    MenuItem mi = new MenuItem ("Print");
    mi.addActionListener (this);
    m.add (mi);

    mi = new MenuItem ("Quit");
    mi.addActionListener (this);
    m.add (mi);

    mb.add (m);
    frame.setMenuBar (mb);
  } // init

  /** Execute the menu events here. **/
  public void actionPerformed (ActionEvent e) {
    String command = e.getActionCommand ();
    if (command.equals ("Quit")) {
        dispose ();
        System.exit (0);
    } else if (command.equals ("Print")) {
      print ();
    }
  } // actionPerformed

  /** Do the print setup up here. **/
  //★================================================================================================
  public void print ()// Create the PrintJob object    
  {                //┌> Toolkit(Window운영체제를 이용하기 위한)객체가 나옴  
    PrintJob pjb = getToolkit ().getPrintJob (this,
         "Print Test", null);                 //└> 프레임, 현재윈도우 출력
                  //└> 운영체제에 호환이 될 수 있도록 null로 줌!

    if (pjb != null) {       //┌> 모니터에서 출력하는 것이 아니라, 프린터로 출력하도록 하는 Graphics 객체!
        Graphics pg = pjb.getGraphics (); 
       if (pg != null) {
           paint (pg);//JFrame의 paint()메서드 호출하면서, 프린터에 출력하도록 객체(pg)를 줌!
           pg.dispose (); // flush page
       }
       pjb.end (); // close print job(공식)
    }
  } // print
  //★================================================================================================

  /** Create a frame and add the applet to it.**/
  public static void main (String[] args) {
    //
    int frame_width  = 360;
    int frame_height = 360;

    Printer f = new Printer("Frame Print Demo");
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    f.init ();
    f.setSize (frame_width, frame_height);
    f.setVisible (true);
  } // main

} // class

/**
  * Draw an image on a JPanel. The constructor is passed
  * a reference to the image.
 **/
class PrintImagePanel extends JPanel
{
  Image fImg;

  /** Constructor is passed the image reference. **/
  PrintImagePanel (Image img) {
    fImg = img;
  } // ctor

  /** Paint the the image in the center of a blue background.**/
  public void paintComponent (Graphics g) {
    super.paintComponent (g);

    g.setFont (new Font ("Dialog", Font.BOLD, 30));
    String msg = "Print this window";

    int wd = getSize ().width;
    int ht = getSize ().height;

    int str_wd = g.getFontMetrics ().stringWidth (msg);
    int str_ht = g.getFontMetrics ().getHeight ();

    int x_str = (wd - str_wd)/2;
    int y_str = str_ht;

    g.drawString (msg, x_str, y_str);

    g.setColor (Color.blue);
    int img_wd = fImg.getWidth (this);
    int img_ht = fImg.getHeight (this);

    int x = (wd - img_wd)/2;
    int y = 2 * str_ht;

    g.fillRect (x-2, y-2, img_wd+4, img_ht+4);
    if (fImg != null) g.drawImage (fImg, x, y, this);

  } // paintComponent

} // PrintImagePanel

 

 

프린터 문에 사용되는 모듈입니다.

 

try{
    String txt = "(적을 내용)";
 
    String pathF = System.getProperty("user.dir"+ "/txt.txt";
 
 
    File file = new File(pathF) ;
 
    FileWriter filewriter = new FileWriter(file, false) ;
    filewriter.write(txt);
    filewriter.flush();
    filewriter.close();
 
    JEditorPane text = new JEditorPane("file:///" + pathF);
    text.print(nullnulltruenullnullfalse);
}catch(Exception txt_e){
    txt_e.printStackTrace();
    JOptionPane.showMessageDialog(null, txt_e);
}
cs

 

인쇄 부분 ↓

JEditorPane text = new JEditorPane("file:///" + pathF);
text.print(null, null, true, null, null, false);

 

이것은 또 다른 예제

728x90