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));
因爲光柵圖像不能被理解爲一個矢量形狀...
感謝您的幫助。
而是標有'// Error'行的,你可以做'g2d.transform(在); g2d.drawImage(img,0,0,null);'。如果您遇到質量問題,請考慮更改Graphics2D的渲染提示。 – Marco13