Click here to Skip to main content
15,886,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method that change the image size and set its scroll position. Both function is working but when I put it in a single call like this
Java
setZoomFactor(1.5);
setScrollPosition(new Point(500, 500)); //lets say this will display the center of the image.
it will zoom the image but not position it. It is working when I put the 2 in separate button and click it one by one. Is one of the line in setZoomFactor runs in thread and continuing to next line? Need an advice. Thank you.
Java
//CODE FOR ZOOMING AND POSITIONING
public void setZoomFactor(double zoomFactor) {
    if(IMAGE_FOUND && image != null && this._zoomFactor != zoomFactor) {
        this._zoomFactor = zoomFactor;
        width = originalWidth;
        height = originalHeight;
        double newwidth = width * zoomFactor;
        double newheight = height * zoomFactor;
        if(newwidth > 3 && newheight > 3) {
            disposeImage();
            image = ImageUtil.scale((BufferedImage) origImage, newwidth, newheight, _antiAlias);
            garbageCollect();
        }
        jLabel1.setIcon(new ImageIcon(image));
        garbageCollect();
    }
}

public void setScrollPosition(Point position) {
    setScrollX(position.x);
    setScrollY(position.y);
}

public int getScrollX() {
    return jScrollPane1.getHorizontalScrollBar().getValue();
}

public void setScrollX(int scroll) {
    jScrollPane1.getHorizontalScrollBar().setValue(scroll);
}

private void garbageCollect() {
    Runtime.getRuntime().gc();
    System.gc();
}

private void disposeImage() {
    if(image != null) image.flush();
    image = null;
    jLabel1.setIcon(null);
    garbageCollect();
}


What I have tried:

I tried putting the 2 method calls an different buttons and it works. I think its because of the delay between clicking.
Posted
Updated 8-Feb-17 2:48am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900