Java:绘制缩放对象(缓冲图像和矢量图形)
Java: drawing scaled objects (buffered image and vector graphics)
我想绘制包含光栅和矢量数据的缩放对象。目前,我正在绘制一个缩放的 Graphics2D 对象
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();
}
不幸的是,这种方法有一些缺点(影响描边宽度,缩放图像质量较差)。
相反,我想绘制缩放对象。对于矢量数据,方法很简单:
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 对象的情况下将变换应用于缓冲图像?这个方法不行
g2d.draw(at.createTransformedShape((Shape)img));
因为栅格图像不能理解为矢量形状...
感谢您的帮助。
可能的解决方案可能表示栅格的仿射变换:
//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 像素),一个简单的双线性插值
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
大约需要 1.5 秒...相反,使用缩放 Graphics2D 对象要快得多(< 0.1 秒),图像可以实时移动和缩放。
在我看来,与栅格数据一起重新缩放对象比重新缩放 Graphics2D 对象慢....
我想绘制包含光栅和矢量数据的缩放对象。目前,我正在绘制一个缩放的 Graphics2D 对象
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();
}
不幸的是,这种方法有一些缺点(影响描边宽度,缩放图像质量较差)。
相反,我想绘制缩放对象。对于矢量数据,方法很简单:
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 对象的情况下将变换应用于缓冲图像?这个方法不行
g2d.draw(at.createTransformedShape((Shape)img));
因为栅格图像不能理解为矢量形状...
感谢您的帮助。
可能的解决方案可能表示栅格的仿射变换:
//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 像素),一个简单的双线性插值
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
大约需要 1.5 秒...相反,使用缩放 Graphics2D 对象要快得多(< 0.1 秒),图像可以实时移动和缩放。
在我看来,与栅格数据一起重新缩放对象比重新缩放 Graphics2D 对象慢....