Java:绘制缩放对象(缓冲的图像和矢量图形) [英] Java: drawing scaled objects (buffered image and vector graphics)

查看:112
本文介绍了Java:绘制缩放对象(缓冲的图像和矢量图形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制包含栅格以及矢量数据的缩放对象。目前,我正在绘制缩放的Graphics2D对象

I would like to draw scaled objects containing raster as well as vector data. Currently, I am drawing into a scaled Graphics2D object

protected void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    //Get transformation, apply scale and shifts
    at = g2d.getTransform();
    at.translate(x, y);
    at.scale(zoom, zoom);      

    //Transform Graphics2D object  
    g2d.setTransform(at);

    //Draw buffered image into the transformed object
    g2d.drawImage(img, 0, 0, this);

    //Draw line into transformed Graphics2D object
    Line2D.Double line = new Line2D.Double();
    line.x1 = (int)p1.getX();
    line.y1 = (int)p1.getY();
    line.x2 = (int)p2.getX();
    line.y2 = (int)p2.getY();

    g2d.draw(line);

    g2d.dispose();
}

不幸的是,这种方法有一些缺点(影响笔划宽度,较差的质量

Unfortunately, this approach has some disadvantages (affects the Stroke width, worse quality of zoomed images).

相反,我想绘制缩放的对象。对于矢量数据,该方法很简单:

Instead of that I would like to draw scaled objects. For the vector data, the approach is simple:

protected void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    //Update affine transformation
    at = AffineTransform.getTranslateInstance(x, y);
    at = AffineTransform.getScaleInstance(zoom, zoom);

    //Transform line and draw
    Line2D.Double line = new Line2D.Double();
    line.x1 = (int)p1.getX();
    line.y1 = (int)p1.getY();
    line.x2 = (int)p2.getX();
    line.y2 = (int)p2.getY();

    g2d.draw(at.createTransformedShape((Shape)line));

    //Transform buffered image and draw ???
    g2d.draw(at.createTransformedShape((Shape)img));  //Error

    g2d.dispose();
}

但是,如何在不重新缩放Graphics2D对象的情况下将转换应用于缓冲的图像?这种方法不起作用

However, how to apply the transformation to the buffered image without rescaling the Graphics2D object? This approach does not work

g2d.draw(at.createTransformedShape((Shape)img)); 

因为不能将光栅图像理解为矢量形状...

because raster image cannot be understood as a vector shape...

感谢您的帮助。

推荐答案

可能的解决方案可能表示栅格的仿射变换:

A possible solution may represent an affine transformation of the raster:

//Update affine transformation
at = AffineTransform.getTranslateInstance(x, y);
at = AffineTransform.getScaleInstance(zoom, zoom);

at.translate(x, y);
at.scale(zoom, zoom);      

//Create affine transformation
AffineTransformOp op = new AffineTransformOp(at,  AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

//Apply transformation
BufferedImage img2 = op.filter(img, null);

//Draw image
g2d.drawImage(img2, 0, 0, this);

不幸的是,这种方法不适用于缩放操作;它在计算上更加昂贵。对于栅格数据(JPG,5000 x 7000 pix),简单的双线性插值

Unfortunately, this approach is inappropriate for the zoom operations; it is more computationally expensive. For the raster data (JPG, 5000 x 7000 pix) a simple bilinear interpolation

AffineTransformOp op = new AffineTransformOp(at,  AffineTransformOp.TYPE_BILINEAR);

大约需要1.5秒...相反,使用可缩放的Graphics2D对象要快得多(<0.1 s),可以实时移动和缩放图像。

takes about 1.5 sec... Conversely, using a scaling Graphics2D object is significantly faster (< 0.1 s), the image can be shifted and zoomed in the real-time.

在我看来,与光栅数据一起缩放对象比缩放Graphics2D对象要慢。 ..

In my opinion, rescaling objects together with the raster data is slower than rescaling Graphics2D object....

这篇关于Java:绘制缩放对象(缓冲的图像和矢量图形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆