2016-12-04 129 views
0

我想繪製包含光柵以及矢量數據的縮放對象。目前,我畫成比例Graphics2D對象Java:繪製縮放對象(緩衝圖像和矢量圖形)

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)); 

因爲光柵圖像不能被理解爲一個矢量形狀...

感謝您的幫助。

+0

而是標有'// Error'行的,你可以做'g2d.transform(在); g2d.drawImage(img,0,0,null);'。如果您遇到質量問題,請考慮更改Graphics2D的渲染提示。 – Marco13

回答

1

一種可能的解決方案可以表示光柵的仿射變換:

//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)的簡單雙線性插值

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

約需1.5秒...相反,使用縮放Graphics2D對象是顯著更快(< 0.1秒)時,圖像可以是實時移動和放大。

在我看來,與柵格數據重新調整對象組合在一起比重新調整Graphics2D對象慢....