Changeset 11539 in josm


Ignore:
Timestamp:
2017-02-04T16:19:52+01:00 (2 years ago)
Author:
Don-vip
Message:

add unit test

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageDisplay.java

    r11535 r11539  
    166166            if (e.getWhen() - lastTimeForMousePoint > 1500 || mousePointInImg == null) {
    167167                lastTimeForMousePoint = e.getWhen();
    168                 mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY());
     168                mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY(), getSize());
    169169            }
    170170
     
    199199
    200200            // Set the position of the visible rectangle, so that the mouse cursor doesn't move on the image.
    201             Rectangle drawRect = calculateDrawImageRectangle(visibleRect);
     201            Rectangle drawRect = calculateDrawImageRectangle(visibleRect, getSize());
    202202            visibleRect.x = mousePointInImg.x + ((drawRect.x - e.getX()) * visibleRect.width) / drawRect.width;
    203203            visibleRect.y = mousePointInImg.y + ((drawRect.y - e.getY()) * visibleRect.height) / drawRect.height;
     
    235235
    236236            // Calculate the translation to set the clicked point the center of the view.
    237             Point click = comp2imgCoord(visibleRect, e.getX(), e.getY());
     237            Point click = comp2imgCoord(visibleRect, e.getX(), e.getY(), getSize());
    238238            Point center = getCenterImgCoord(visibleRect);
    239239
     
    273273
    274274            if (e.getButton() == DRAG_BUTTON) {
    275                 mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY());
     275                mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY(), getSize());
    276276                mouseIsDragging = true;
    277277                selectedRect = null;
    278278            } else if (e.getButton() == ZOOM_BUTTON) {
    279                 mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY());
     279                mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY(), getSize());
    280280                checkPointInVisibleRect(mousePointInImg, visibleRect);
    281281                mouseIsDragging = false;
     
    310310
    311311            if (mouseIsDragging) {
    312                 Point p = comp2imgCoord(visibleRect, e.getX(), e.getY());
     312                Point p = comp2imgCoord(visibleRect, e.getX(), e.getY(), getSize());
    313313                visibleRect.x += mousePointInImg.x - p.x;
    314314                visibleRect.y += mousePointInImg.y - p.y;
     
    322322
    323323            } else if (selectedRect != null) {
    324                 Point p = comp2imgCoord(visibleRect, e.getX(), e.getY());
     324                Point p = comp2imgCoord(visibleRect, e.getX(), e.getY(), getSize());
    325325                checkPointInVisibleRect(p, visibleRect);
    326326                Rectangle rect = new Rectangle(
     
    432432    }
    433433
     434    /**
     435     * Constructs a new {@code ImageDisplay}.
     436     */
    434437    public ImageDisplay() {
    435438        ImgDisplayMouseListener mouseListener = new ImgDisplayMouseListener();
     
    471474        }
    472475
     476        Dimension size = getSize();
    473477        if (file == null) {
    474478            g.setColor(Color.black);
    475479            String noImageStr = tr("No image");
    476480            Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(noImageStr, g);
    477             Dimension size = getSize();
    478481            g.drawString(noImageStr,
    479482                    (int) ((size.width - noImageSize.getWidth()) / 2),
     
    488491            }
    489492            Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(loadingStr, g);
    490             Dimension size = getSize();
    491493            g.drawString(loadingStr,
    492494                    (int) ((size.width - noImageSize.getWidth()) / 2),
    493495                    (int) ((size.height - noImageSize.getHeight()) / 2));
    494496        } else {
    495             Rectangle target = calculateDrawImageRectangle(visibleRect);
     497            Rectangle target = calculateDrawImageRectangle(visibleRect, size);
    496498            g.drawImage(image,
    497499                    target.x, target.y, target.x + target.width, target.y + target.height,
     
    499501                    null);
    500502            if (selectedRect != null) {
    501                 Point topLeft = img2compCoord(visibleRect, selectedRect.x, selectedRect.y);
     503                Point topLeft = img2compCoord(visibleRect, selectedRect.x, selectedRect.y, size);
    502504                Point bottomRight = img2compCoord(visibleRect,
    503505                        selectedRect.x + selectedRect.width,
    504                         selectedRect.y + selectedRect.height);
     506                        selectedRect.y + selectedRect.height, size);
    505507                g.setColor(new Color(128, 128, 128, 180));
    506508                g.fillRect(target.x, target.y, target.width, topLeft.y - target.y);
     
    514516                String loadingStr = tr("Error on file {0}", file.getName());
    515517                Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(loadingStr, g);
    516                 Dimension size = getSize();
    517518                g.drawString(loadingStr,
    518519                        (int) ((size.width - noImageSize.getWidth()) / 2),
     
    550551    }
    551552
    552     private Point img2compCoord(Rectangle visibleRect, int xImg, int yImg) {
    553         Rectangle drawRect = calculateDrawImageRectangle(visibleRect);
     553    static Point img2compCoord(Rectangle visibleRect, int xImg, int yImg, Dimension compSize) {
     554        Rectangle drawRect = calculateDrawImageRectangle(visibleRect, compSize);
    554555        return new Point(drawRect.x + ((xImg - visibleRect.x) * drawRect.width) / visibleRect.width,
    555556                drawRect.y + ((yImg - visibleRect.y) * drawRect.height) / visibleRect.height);
    556557    }
    557558
    558     private Point comp2imgCoord(Rectangle visibleRect, int xComp, int yComp) {
    559         Rectangle drawRect = calculateDrawImageRectangle(visibleRect);
     559    static Point comp2imgCoord(Rectangle visibleRect, int xComp, int yComp, Dimension compSize) {
     560        Rectangle drawRect = calculateDrawImageRectangle(visibleRect, compSize);
    560561        return new Point(visibleRect.x + ((xComp - drawRect.x) * visibleRect.width) / drawRect.width,
    561562                visibleRect.y + ((yComp - drawRect.y) * visibleRect.height) / drawRect.height);
    562563    }
    563564
    564     private static Point getCenterImgCoord(Rectangle visibleRect) {
     565    static Point getCenterImgCoord(Rectangle visibleRect) {
    565566        return new Point(visibleRect.x + visibleRect.width / 2,
    566                 visibleRect.y + visibleRect.height / 2);
    567     }
    568 
    569     private Rectangle calculateDrawImageRectangle(Rectangle visibleRect) {
    570         return calculateDrawImageRectangle(visibleRect, new Rectangle(0, 0, getSize().width, getSize().height));
     567                         visibleRect.y + visibleRect.height / 2);
     568    }
     569
     570    static Rectangle calculateDrawImageRectangle(Rectangle visibleRect, Dimension compSize) {
     571        return calculateDrawImageRectangle(visibleRect, new Rectangle(0, 0, compSize.width, compSize.height));
    571572    }
    572573
     
    579580     */
    580581    static Rectangle calculateDrawImageRectangle(Rectangle imgRect, Rectangle compRect) {
    581         int x, y, w, h;
    582         x = 0;
    583         y = 0;
    584         w = compRect.width;
    585         h = compRect.height;
     582        int x = 0;
     583        int y = 0;
     584        int w = compRect.width;
     585        int h = compRect.height;
    586586
    587587        int wFact = w * imgRect.height;
     
    633633    }
    634634
    635     private static void checkVisibleRectPos(Image image, Rectangle visibleRect) {
     635    static void checkVisibleRectPos(Image image, Rectangle visibleRect) {
    636636        if (visibleRect.x < 0) {
    637637            visibleRect.x = 0;
     
    648648    }
    649649
    650     private static void checkVisibleRectSize(Image image, Rectangle visibleRect) {
     650    static void checkVisibleRectSize(Image image, Rectangle visibleRect) {
    651651        if (visibleRect.width > image.getWidth(null)) {
    652652            visibleRect.width = image.getWidth(null);
Note: See TracChangeset for help on using the changeset viewer.