Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图形。无光栅填充(形状)_Java_Swing_Graphics_Printing - Fatal编程技术网

Java 图形。无光栅填充(形状)

Java 图形。无光栅填充(形状),java,swing,graphics,printing,Java,Swing,Graphics,Printing,我正在将swing组件打印到各种打印机上。其中一台打印机的alpha通道存在严重问题。如果屏幕上的任何东西都有一个alpha通道,那么产生的假脱机大小是巨大的。在大多数情况下,我已经消除了alpha通道。但是,有一个元素使用Graphics2d.fill(Shape)用一些散列线填充一个形状,因此您可以看到散列后面的内容。有没有一种方法可以在打印过程中不将alpha信息光栅化到图形对象而实现这一点 我相信如果使用调用Graphics.drawLine(),而不是fill(Shape),生成散列线

我正在将swing组件打印到各种打印机上。其中一台打印机的alpha通道存在严重问题。如果屏幕上的任何东西都有一个alpha通道,那么产生的假脱机大小是巨大的。在大多数情况下,我已经消除了alpha通道。但是,有一个元素使用
Graphics2d.fill(Shape)
用一些散列线填充一个形状,因此您可以看到散列后面的内容。有没有一种方法可以在打印过程中不将alpha信息光栅化到图形对象而实现这一点

我相信如果使用调用
Graphics.drawLine()
,而不是
fill(Shape)
,生成散列线,打印机会更高兴,但我填充的形状非常复杂(它是一个折线图)。有没有一种方法可以用画好的线来填充形状,从而使打印机更快乐?看起来所有的绘制实现都是基于光栅的

除此之外,有没有办法告诉图像是完全透明还是完全不透明?这对打印机的线轴大小有帮助吗

下面是用垂直散列线填充圆圈的代码,允许水平散列线显示出来

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;
        }
    }
}
公共类BufferedImagePrint扩展JComponent实现可运行、可打印{
公共静态void main(字符串[]args){
调用器(新的BufferedImagePrint());
}
公开募捐{
setPreferredSize(新维度(128128));
final JOptionPane optionPane=new JOptionPane(此,JOptionPane.PLAIN_消息,JOptionPane.OK_取消_选项,null,新对象[]){
“打印”,
“取消”});
最终JDialog dialog=optionPane.createDialog(“绘制透明图像”);
对话框。可设置大小(true);
对话框.setVisible(true);
if(“Print”.equals(optionPane.getValue())){
doPrint();
}
}
私有void doPrint(){
试一试{
final PrinterJob PrinterJob=PrinterJob.getPrinterJob();
printerJob.setPrintable(此);
if(printerJob.printDialog()){
printerJob.print();
}
}捕获(例外e){
抛出新的运行时异常(e);
}
}
@凌驾
受保护组件(最终图形g){
//画水平线
g、 设置颜色(颜色为黑色);

对于(int i=0;i您是否可以将形状绘制为临时图像,然后绘制图像(而不是形状)到打印机?

您是否考虑过使用复杂形状作为剪辑绘制散列线?

我绘制的任何透明图像都会导致卷轴大小膨胀到千兆字节范围。您是否尝试过使用alpha绘制图像而不是使用alpha绘制图像?处理这些图像的方式可能会有奇怪的差异。请寻求更好的帮助更快,发布一个。@AndrewThompson,上面的内容是我能做的最简短的示例代码。我可能不需要打印支持。但是,我如何在不向图形对象发送透明光栅的情况下用垂直线填充椭圆形呢?我不建议发布“示例代码”,而是一个SSCCE。发布的不是SSCCE。