Graphics.fill(Shape)没有光栅

时间:2012-01-24 19:15:10

标签: java swing graphics printing

我正在为各种打印机打印摆动组件。其中一台打印机的alpha通道存在很大问题。如果屏幕上的任何内容都有alpha通道,则生成的线轴尺寸很大。在大多数情况下,我已经消除了alpha通道。但是,有一个元素使用Graphics2d.fill(Shape)来填充带有一些散列线的形状,因此您可以看到散列背后的内容。有没有办法实现这一点,而不会在打印过程中将alpha信息转移到Graphics对象?

我相信如果使用Graphics.drawLine()而不是fill(Shape)调用生成的散列线,打印机会更开心,但我填充的形状非常复杂(这是折线图)。有没有办法用绘制的线条填充Shape,这会让打印机更快乐?看起来所有的Paint实现都是基于栅格的。

除此之外,有没有办法让图像完全透明或根本不透明?这对打印机上的线轴大小有帮助吗?

这是用垂直散列线填充圆形的代码,允许水平散列线显示。

public class BufferedImagePrint extends JComponent implements Runnable, Printable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new BufferedImagePrint());
    }

    public void run() {
        setPreferredSize(new Dimension(128, 128));
        final JOptionPane optionPane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, new Object[]{
                "Print",
                "Cancel"});
        final JDialog dialog = optionPane.createDialog("Painting Transparent Image");
        dialog.setResizable(true);
        dialog.setVisible(true);
        if ("Print".equals(optionPane.getValue())) {
            doPrint();
        }
    }

    private void doPrint() {
        try {
            final PrinterJob printerJob = PrinterJob.getPrinterJob();
            printerJob.setPrintable(this);
            if(printerJob.printDialog()) {
            printerJob.print();
        }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void paintComponent(final Graphics g) {
        // draw horizontal lines
        g.setColor(Color.BLACK);
        for (int i=0; i<getWidth(); i+=3) {
            g.drawLine(0, i, getWidth(), i);
        }

        final BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics2D textureGraphics = img.createGraphics();
        textureGraphics.setColor(Color.BLACK);
        for (int i=0; i<32; i+=4) {
            textureGraphics.drawLine(i, 0, i, 32);
        }
        textureGraphics.dispose();
        final TexturePaint paint = new TexturePaint(img, new Rectangle(32, 32));
        ((Graphics2D)g).setPaint(paint);
        g.fillOval(0, 0, getWidth(),  getHeight());
    }

    public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex) throws PrinterException {
        if (pageIndex == 0) {
            paintComponent(graphics);
            return PAGE_EXISTS;
        } else {
            return NO_SUCH_PAGE;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将形状绘制到临时图像,然后将图像(而不是形状)绘制到打印机吗?

答案 1 :(得分:0)

您是否考虑使用复杂形状作为剪辑绘制哈希线?

相关问题