//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.io.*;  // FileOutputsStreanm  PrintWriter

public class drucken1 extends JFrame {

  JButton Bn1 = new JButton("Bn1");
  JButton Bn2 = new JButton("Bn2");
  JButton Bn3 = new JButton("Bn3");
  JButton Bn4 = new JButton("Bn4");
  JButton Bn5 = new JButton("Bn5");
  JButton BnOk = new JButton("Ok");
  JButton BnEsc = new JButton("Abbruch");


  private JTextArea _editor;


  public drucken1() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(525, 430);
    setTitle("JFrame als Dialog");

    Bn1.setFont( new Font("Arial", Font.BOLD,18)  );
    Bn2.setFont( new Font("Arial", Font.BOLD,18)  );
    Bn3.setFont( new Font("Arial", Font.BOLD,18)  );
    Bn4.setFont( new Font("Arial", Font.BOLD,18)  );
    Bn5.setFont( new Font("Arial", Font.BOLD,18)  );
    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );
    BnOk.setPreferredSize( BnEsc.getPreferredSize() );

    this.getContentPane().setLayout( new BorderLayout() );
    _editor = new JTextArea("abc\ndef");
    this.getContentPane().add( new JScrollPane(_editor), BorderLayout.CENTER);

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(Bn1);
	pnButton.add(Bn2);
	pnButton.add(Bn3);
	pnButton.add(Bn4);
	pnButton.add(Bn5);
	pnButton.add(BnOk);
	pnButton.add(BnEsc);

    this.getContentPane().add( pnButton, BorderLayout.SOUTH );

          BnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnOk_Click();
            }
          });

          Bn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnBsp1_Click();
            }
          });
          Bn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnBsp2_Click();
            }
          });
          Bn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnBsp3_Click();
            }
          });
          Bn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnBsp4_Click();
            }
          });
          Bn5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnBsp5_Click();
            }
          });

          BnEsc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnEsc_Click();
            }
          });


  } // setGUI

    private void BnBsp1_Click() {
        Toolkit tk = Toolkit.getDefaultToolkit();
		PrintJob pj = tk.getPrintJob( new Frame(), "", null );
		Font font = new Font( "Times", Font.PLAIN, 20 );
		Graphics g;
		if ( pj != null )
	    {
    	  int res = pj.getPageResolution();
	      Dimension d = pj.getPageDimension();
          _editor.setText( "Resolution : " + res + "\n" +
                          "Breite : " + d.width + "\n" +
                          "Höehe : " + d.height + "\n" +
                          "Pixel pro Seite : " + (res * d.width * d.height) );
          g= pj.getGraphics();
          g.setFont( font );
          g.drawString( "Hallo auf Seite 1", 100, 100 );
             // lastPageFirst()
          pj.end();  
	    }
    } // Bsp1

    private void BnBsp2_Click()  {
       try {
          Toolkit tk = Toolkit.getDefaultToolkit();
          PrintJob pj = tk.getPrintJob( new Frame(), "", null );
          if ( pj != null ) {
            int res = pj.getPageResolution();
            Dimension d = pj.getPageDimension();
            _editor.setText( "Resolution : " + res + "\n" +
                            "Breite : " + d.width + "\n" +
                            "Höehe : " + d.height + "\n" +
                            "Pixel pro Seite : " + (res * d.width * d.height) );
  
            PrintWriter pw = new PrintWriter( new FileOutputStream( "PRN:" ));
            pw.println( "Hallo auf Seite 1" );
            pw.println( "das ist die zeite Zeile" );
            pw.println( "das ist die dritte Zeile" );
            pw.close();  // resource freigeben
            pj.end();  
         }
       }
       catch (IOException e) {
       }  
    }  //Bsp2

    private void BnBsp3_Click() {
     PrintJob prjob = getToolkit().getPrintJob( this, "Testseite", null );
     if( null != prjob )
     {
       final int iScreenResol           = getToolkit().getScreenResolution();
       final int iPageResol             = prjob.getPageResolution();
       final Dimension dimScreenSize    = getToolkit().getScreenSize();
       final Dimension dimPageDimension = prjob.getPageDimension();
       Graphics pg = prjob.getGraphics();
       if( null != pg && 0 < iPageResol )
       {
         int iAddY = 20;
         int iRand = (int)Math.round( iPageResol * 2. / 2.54 );  // 2 cm Rand
         int iPosX = iRand + iRand/4;                            // Textposition
         int iPosY = iPosX - iAddY/2;
         int iWdth = dimPageDimension.width  - iRand * 2;        // innere Breite
         int iMidY = dimPageDimension.height / 2;
         pg.setFont( new Font( "SansSerif", Font.PLAIN, iAddY*2/3 ) );
         pg.drawString( "Betriebssystem: "
                        + System.getProperty( "os.name" ) + " "
                        + System.getProperty( "os.version" ), iPosX, iPosY+=iAddY );
         pg.drawString( "Java-Version: JDK "
                        + System.getProperty( "java.version" ), iPosX, iPosY+=iAddY );
         pg.drawString( "getScreenResolution: "
                        + iScreenResol + " dpi", iPosX, iPosY+=iAddY );
         pg.drawString( "getScreenSize: "
                        + dimScreenSize.width  + " x "
                        + dimScreenSize.height + " Pixel", iPosX, iPosY+=iAddY );
         pg.drawString( "PrintJob.getPageResolution: "
                        + iPageResol + " dpi", iPosX, iPosY+=iAddY );
         pg.drawString( "PrintJob.getPageDimension: "
                        + dimPageDimension.width  + " x "
                        + dimPageDimension.height + " Pixel", iPosX, iPosY+=iAddY );
         pg.drawString( "errechnete Papiergröße: "
                        + Math.round( 254. * dimPageDimension.width  / iPageResol / 10. ) + " x "
                        + Math.round( 254. * dimPageDimension.height / iPageResol / 10. ) + " mm",
                        iPosX, iPosY+=iAddY );
         pg.drawRect( iRand, iRand, iWdth, dimPageDimension.height - iRand * 2 );
         pg.drawLine( iRand, iMidY + iWdth/50, dimPageDimension.width - iRand, iMidY - iWdth/50 );
         pg.drawLine( iRand, iMidY - iWdth/50, dimPageDimension.width - iRand, iMidY + iWdth/50 );
         iWdth -= 4;
         pg.drawOval( iRand + 2, dimPageDimension.height - iRand - iWdth - 2, iWdth, iWdth );
         pg.dispose();
       }
       prjob.end();
     }
 
    }
    private void BnBsp4_Click() {
    }
    private void BnBsp5_Click() {
    }

    private void BnOk_Click() {
    }

    private void BnEsc_Click() {
        setVisible(false);
        System.exit(0);
    }







  public static void main(String[] args) {
    drucken1 frame = new drucken1();
    frame.setVisible(true);
 }

}  // drucken1



