Search code examples
javaswingzoomingscalepanel

Zoom on drawline


I have an image and on this I drew some lines. When I use zoom on this panel, only the image changes its size (if i get to the large dimension of image, in my case with width = 5550 and height = 7800 that line disappear).

How can I use zoom to images and for the lines that I drew?

The code is from this website: https://coderanch.com/t/338284/java/zoom-zoom-picture-swing

What I did it's to put a function draw in the class ImagePanel and to call this in the function paintComponent().

This is my code:

 public void draw(Graphics2D g) {
int val1=127, val2= 65;
    for(int i=1; i<20; i++) {
        g.setStroke(new BasicStroke(10)); 
        g.setColor(Color.black);
        g.drawLine(val1, val2, val1+10, val2+20);
        val1+=10;
        val2+=20;
    }
}

Solution

  •  public void draw(Graphics2D g) {
    int val1=127, val2= 65;
        for(int i=1; i<20; i++) {
            g.setStroke(new BasicStroke(5)); 
            g.setColor(Color.red);
            g.drawLine(val1, val2, val1+10, val2+20);
            val1+=10;
            val2+=20;
        }
    }
    

    I would guess your problem is that the code painting the lines assumes a normal scale. That is you always incement the line value by the same amount not matter what the scale factor is.

    I would suggest a better approach would be to:

    1. Create a BufferedImage
    2. draw your original image onto the BufferedImage
    3. draw the lines onto the BufferedImage
    4. Now in the paintComponent() method you simply scale the BufferedImage. No need for any code to draw the lines again.