java打印图片到页面_在Java中打印BufferedImage的正确方法

这段Java代码演示了如何在打印机上缩放并打印一个BufferedImage图像。通过创建一个PrintActionListener类实现Runnable,设置Printable接口以处理图像打印,并在用户确认打印对话框后调用PrinterJob的print()方法。
摘要由CSDN通过智能技术生成

这是我的一个Java项目中的一个.此代码将在打印机页面上缩放和打印一个图像.

你这样称呼它:

printButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent event) {

//Start a Thread

new Thread(new PrintActionListener(image)).start();

}

});

这是Runnable:

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

public class PrintActionListener implements Runnable {

private BufferedImage image;

public PrintActionListener(BufferedImage image) {

this.image = image;

}

@Override

public void run() {

PrinterJob printJob = PrinterJob.getPrinterJob();

printJob.setPrintable(new ImagePrintable(printJob, image));

if (printJob.printDialog()) {

try {

printJob.print();

} catch (PrinterException prt) {

prt.printStackTrace();

}

}

}

public class ImagePrintable implements Printable {

private double x, y, width;

private int orientation;

private BufferedImage image;

public ImagePrintable(PrinterJob printJob, BufferedImage image) {

PageFormat pageFormat = printJob.defaultPage();

this.x = pageFormat.getImageableX();

this.y = pageFormat.getImageableY();

this.width = pageFormat.getImageableWidth();

this.orientation = pageFormat.getOrientation();

this.image = image;

}

@Override

public int print(Graphics g, PageFormat pageFormat, int pageIndex)

throws PrinterException {

if (pageIndex == 0) {

int pWidth = 0;

int pHeight = 0;

if (orientation == PageFormat.PORTRAIT) {

pWidth = (int) Math.min(width, (double) image.getWidth());

pHeight = pWidth * image.getHeight() / image.getWidth();

} else {

pHeight = (int) Math.min(width, (double) image.getHeight());

pWidth = pHeight * image.getWidth() / image.getHeight();

}

g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);

return PAGE_EXISTS;

} else {

return NO_SUCH_PAGE;

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java显示图片方法有很多种,其比较常用的包括: 1. 使用AWT的Image类和Graphics类来加载和显示图片: ```java import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class ImagePanel extends JPanel { private Image image; public ImagePanel() { try { // 加载图片 BufferedImage bufferedImage = ImageIO.read(new File("image.jpg")); image = Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource()); } catch (IOException e) { e.printStackTrace(); } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); // 显示图片 g.drawImage(image, 0, 0, getWidth(), getHeight(), this); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(500, 500); frame.add(new ImagePanel()); frame.setVisible(true); } } ``` 2. 使用Swing的JLabel组件来显示图片: ```java import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class ImageLabel { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(500, 500); try { // 加载图片 BufferedImage bufferedImage = ImageIO.read(new File("image.jpg")); // 创建图标 ImageIcon imageIcon = new ImageIcon(bufferedImage); // 创建标签 JLabel label = new JLabel(imageIcon); // 添加标签 frame.add(label); } catch (IOException e) { e.printStackTrace(); } frame.setVisible(true); } } ``` 以上两种方法都可以实现在Java显示图片,但是使用Swing的JLabel组件比较简单,而且可以实现更多的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值