0

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;
    }
}
AndreeaA
  • 75
  • 6
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. .. – Andrew Thompson Jun 10 '20 at 21:53
  • .. Please [edit] the question to add 4 space chars ahead of the closing `}` at the end of the code. – Andrew Thompson Jun 10 '20 at 21:53

1 Answers1

1
 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.
camickr
  • 321,443
  • 19
  • 166
  • 288