0

I would kindly ask you for help with the code involving zoom operations with raster and vector data (represented by the line). Sorry for a complete code, which is slightly longer:

public class ZoomOperations extends JPanel {
  private java.awt.image.BufferedImage image = null;
  private Line2D.Double line = null;
  private double scale = 1.0, sx = 0.0, sy = 0.0;
  private int ratio = 20;
  private MouseManager manager = null;

  public ZoomOperations() throws IOException {
    line = new Line2D.Double();
    image = javax.imageio.ImageIO.read(new java.io.File("E:\\test.jpg"));
    manager = new MouseManager(this);
    addMouseListener(manager);
    addMouseMotionListener(manager);
    addMouseWheelListener(manager);
}

protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    AffineTransform trans = g2.getTransform();
    double x = 0.5*(getWidth() - scale * image.getWidth());
    double y = 0.5*(getHeight() - scale * image.getHeight());
    g2.translate(x, y); 
    g2.scale(scale, scale); 
    g2.drawImage(image, 0, 0, this);
    g2.setTransform(trans);                       //Incorrect line?
    g2.draw(line);
}

public void setPoints(Point start, Point end) { line.setLine(start, end); repaint();}
public void ZoomPlus() {scale = (++ratio + 2) / 10.0; repaint();}
public void ZoomMinus(){scale = (--ratio + 2) / 10.0; repaint();}

class MouseManager extends MouseAdapter {
    ZoomOperations item;
    Point start;

    public MouseManager(ZoomOperations item_){item = item_;}
    public void mousePressed(MouseEvent e){start = e.getPoint();}
    public void mouseDragged(MouseEvent e){Point end = e.getPoint();
        item.setPoints(start, end);
        item.repaint();
    }

    public void mouseWheelMoved(MouseWheelEvent e){
            int notches = e.getWheelRotation();
            if (notches < 0) ZoomPlus();
            else ZoomMinus();          
    }
}

public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ZoomOperations zoom = new ZoomOperations();
    frame.add(zoom);
    frame.setSize(new Dimension(800, 600));
    frame.setVisible(true);
  }
}

Unfortunately, the zoom operation is applied only to the raster data. The vector line keeps its position and size.

Initial figure without zoom:

initial figure

Applying the zoom plus operation:

zoom plus

In my opinion, the following line is probably incorrect

g2.setTransform(trans); 

the accumulated shifts x, y and scale are not involved... However, the error might be elsewhere :-)

An additional question: Is there any easier way to implement zoom operations in Java? Thank you very much for your help...

justik
  • 4,145
  • 6
  • 32
  • 53
  • Might be good idea to provide a picture of how it looks before and after zooming and another on how you think it should look after zooming. – NESPowerGlove Sep 06 '16 at 21:47
  • @ NESPowerGlove : Figures added.... – justik Sep 06 '16 at 22:00
  • You are resetting the transform to the original one (`trans`) before drawing the line - which other effect should this have than painting the UN-transformed line? It might already work when you remove the `g2.setTransform(trans);` line. However, note that it will then scale the whole line when zooming (i.e. the line will be thicker). If you want to avoid this, keep the `setTransform` but create a scaled version of the line. (BTW: Once I created https://github.com/javagl/Viewer for cases like this. The screenshot shows the difference between "scaling the graphics" and "scaling the objects"). – Marco13 Sep 06 '16 at 22:14
  • @ Marco: removing g2.setTransform(trans) the line disappears. It is drawn outside the frame. – justik Sep 06 '16 at 22:48
  • It is drawn with the same transform as the image. When you zoom out so that you see the whole image, you will also see the line (not where you moved the mouse, but ... MANY details of what you actually want to achieve are missing ....) – Marco13 Sep 06 '16 at 23:17

0 Answers0