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();
}
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...
Thanks for your help.