Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions CodenameOne/src/com/codename1/components/ImageViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,17 +813,12 @@ public void paint(Graphics g) {
}

g.drawImage(image,
(imageDrawWidth <= getInnerWidth()) ? getX() + imageX : Math.max(
Math.min( //
getX(),
getX() + imageX
),
getX() - imageDrawWidth + getInnerWidth()),
(imageDrawHeight <= getInnerHeight()) ? getY() + imageY : Math.max(
Math.min(
getY(), getY() + imageY
), getY() - imageDrawHeight + getInnerHeight()
),
(imageDrawWidth <= getInnerWidth()) ? getX() + imageX : getX() + Math.max(
Math.min(0, imageX),
-imageDrawWidth + getInnerWidth()),
(imageDrawHeight <= getInnerHeight()) ? getY() + imageY : getY() + Math.max(
Math.min(0, imageY),
-imageDrawHeight + getInnerHeight()),
imageDrawWidth, imageDrawHeight);


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.codename1.components;

import com.codename1.testing.AbstractTest;
import com.codename1.ui.Image;
import com.codename1.ui.Form;

/**
* Test for ImageViewer overlapping images fix
*/
public class ImageViewerCoordinateTest extends AbstractTest {

@Override
public boolean shouldExecuteOnEDT() {
return true;
}

@Override
public boolean runTest() throws Exception {
testZoomAndPanCoordinateConsistency();
return true;
}

/**
* Test that coordinate calculations in updatePositions and paint methods are consistent
*/
public void testZoomAndPanCoordinateConsistency() throws Exception {
System.out.println("Testing ImageViewer coordinate consistency");

// Create a test form and image viewer
Form testForm = new Form();
ImageViewer viewer = new ImageViewer();

// Create a large test image (bigger than typical screen)
Image testImage = Image.createImage(2000, 1500);
viewer.setImage(testImage);

// Set viewer size (simulate a typical mobile screen)
viewer.setX(50); // Not starting at 0,0 to test coordinate translation
viewer.setY(100);
viewer.setWidth(400);
viewer.setHeight(600);

// Add to form and lay out
testForm.addComponent(viewer);
testForm.show();
testForm.revalidate();

// Test scenario 1: High zoom with right pan
float zoom = 3.0f;
float panX = 0.8f; // Panned to the right
float panY = 0.5f; // Centered vertically

viewer.setZoom(zoom, panX, panY);

// The key test: verify that the image coordinates are properly constrained
// With the fix, the image should not extend beyond the viewer bounds
int imageX = viewer.getImageX();
int imageY = viewer.getImageY();

// Log the values for debugging
System.out.println("Zoom: " + zoom + ", PanX: " + panX + ", PanY: " + panY);
System.out.println("ImageX: " + imageX + ", ImageY: " + imageY);
System.out.println("Viewer bounds: X=" + viewer.getX() + ", Y=" + viewer.getY() +
", W=" + viewer.getWidth() + ", H=" + viewer.getHeight());

// Test scenario 2: Maximum reasonable zoom at far right
zoom = 5.0f;
panX = 1.0f; // Fully right
viewer.setZoom(zoom, panX, panY);

imageX = viewer.getImageX();
imageY = viewer.getImageY();

System.out.println("High zoom test - ImageX: " + imageX + ", ImageY: " + imageY);

// Test scenario 3: Test IMAGE_FILL mode to ensure it works with the fix
viewer.setImageInitialPosition(ImageViewer.IMAGE_FILL);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have some asserts in this test to check and verify that the bug is fixed.

viewer.setZoom(2.0f, 0.9f, 0.5f);

imageX = viewer.getImageX();
imageY = viewer.getImageY();

System.out.println("IMAGE_FILL mode test - ImageX: " + imageX + ", ImageY: " + imageY);

System.out.println("ImageViewer coordinate consistency test completed successfully");
}
}