0

印刷用のページを生成しているパネルとコンポーネントを使用して、テキストや画像などのさまざまな要素を印刷していますが、印刷も予定されていますが、ハード印刷には物理紙の印刷ボタンもあります。ページから印刷ボタンを削除します。ここにコードがあります

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

public class Printing extends JFrame
                          implements ActionListener {
  public static void main(String[] args) 
  {
   // intialise

  }

  public Printing(String Firstname,String LastName,String contactid) 
  {
    super("Print badge");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    JButton printButton = new JButton("Print");
    printButton.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.setBackground(Color.BLACK);
    buttonPanel.add(printButton);
    content.add(buttonPanel, BorderLayout.SOUTH);
    DrawingPanel drawingPanel = new DrawingPanel(Firstname,LastName,contactid);
    content.add(drawingPanel, BorderLayout.CENTER);
    pack();
    setVisible(true);
  }

  public void actionPerformed(ActionEvent event) 
  {
        //call for printing


  }
}

および実際の印刷パネルのコード

public class DrawingPanel extends JPanel 
{
  private int fontSize1 = 32;

  private Image img1=null;


  public DrawingPanel(String n1,String n2,String n3) 
  {

    String path="D:"+"\\25175.jpg";



    setBackground(Color.white);
    Font font = new Font("Serif", Font.PLAIN, 32);
    setFont(font);

    img1=new ImageIcon(path).getImage();


    setPreferredSize(new Dimension(400, 400));
  }

  public void paintComponent(Graphics g) 
  {

    Graphics2D g2d = (Graphics2D)g;

    g2d.translate(x, y);
    g2d.setPaint(Color.lightGray);
    AffineTransform origTransform = g2d.getTransform();
    g2d.shear(-0.95, 0);
    g2d.scale(1, 3);

    g2d.setTransform(origTransform);
    g2d.setPaint(Color.BLUE);
    g2d.drawString(string,25 , 50);
    g2d.drawString(string, 125,100);
    g.drawImage(img1, 280, 190, null);
  }
}

印刷方法の設定はこちら

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

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;

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

  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }



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

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

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

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

2 に答える 2

0

by this call PrintUtilities.printComponent(this) in button action listener

This would seem to be your problem.

This will, technically, print the contents of the entire frame, you only need to print the DrawingPanel.

This is going to require you to make some minor changes to your code so you access the instance of the DrawingPanel from your actionPerformed method...

private DrawingPanel drawingPanel;
public Printing(String Firstname,String LastName,String contactid) 
{
    super("Print badge");
    //...
    drawingPanel = new DrawingPanel(Firstname,LastName,contactid);
    //...
}

This should no allow you to do something like...

PrintUtilities.printComponent(drawingPanel);

Instead...

于 2013-09-27T07:34:32.267 に答える