Commit 7484167d authored by kirandjiska's avatar kirandjiska

Added the possibility of zooming in the zoom box panel and resizing the zoom box panel

parent 75fd0e0f
......@@ -16,6 +16,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
......@@ -104,7 +105,7 @@ public class ConstraintsSelectionDialog extends JInternalFrame implements Zoomab
/** Image displayed */
private BufferedImage mNew = null;
/** Panl that containsthe displayed image */
/** Panel that contains the displayed image */
private JPanel displayPanel = null;
public JPanel mainPanel;
......@@ -221,37 +222,35 @@ public class ConstraintsSelectionDialog extends JInternalFrame implements Zoomab
// display the image
this.displayPanel = (JPanel) new ZoomBoxPanel(this);
// key listener for the key "h" which hides/displays the current constraints on the image
this.displayPanel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_H) {
// active JCheckBox check
check.doClick();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
this.displayPanel.setFocusable(true);
this.displayPanel.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getUnitsToScroll() > 0) {
zoomOut();
} else {
zoomIn();
// zoom in zoom box
if((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
if (e.getUnitsToScroll() > 0) {
((ZoomBoxPanel) displayPanel).zoomInBox();
} else {
((ZoomBoxPanel) displayPanel).zoomOutBox();
}
}
// resize zoom box area
else if((e.getModifiers() & InputEvent.ALT_MASK) == InputEvent.ALT_MASK) {
if (e.getUnitsToScroll() > 0) {
((ZoomBoxPanel) displayPanel).increaseZoomBoxArea();
} else {
((ZoomBoxPanel) displayPanel).decreaseZoomBoxArea();
}
}
// zoom in image
else {
if (e.getUnitsToScroll() > 0) {
zoomOut();
} else {
zoomIn();
}
}
}
});
......@@ -374,8 +373,35 @@ public class ConstraintsSelectionDialog extends JInternalFrame implements Zoomab
otherComponentList.add(constraintsColorChooser);
JCheckBox zoomBoxChkBox = new JCheckBox("Zoom Box");
zoomBoxChkBox.setMnemonic(KeyEvent.VK_Z);
// zoomBoxChkBox.setSelected(zoomBoxActivated);
this.displayPanel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// enable zoom box
if(e.getKeyCode() == KeyEvent.VK_Z) {
zoomBoxChkBox.doClick();
}
// hide constraints
if(e.getKeyCode() == KeyEvent.VK_H) {
check.doClick();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
this.displayPanel.setFocusable(true);
zoomBoxChkBox.addItemListener(new ItemListener() {
@Override
......
......@@ -19,120 +19,175 @@ import javax.swing.JPanel;
*
*/
public class ZoomBoxPanel extends JPanel{
/** */
private static final long serialVersionUID = 1L;
private Point zoomPoint;
private Zoomable dialog;
private float zoom = 2.0f;
private int zoomArea = 120;
private boolean zoomBoxEnabled = false;
private float zoomMin = 1.5f;
private float zoom = 2.0f;
private final float zoomStep = 0.1f;
private final int zoomAreaAdd = 5;
private final int zoomAreaMin = 50;
private final int zoomAreaMax = 300;
private int zoomArea = 120;
private boolean zoomBoxEnabled = false;
public ZoomBoxPanel(Zoomable dialog, float zoom, int zoomArea) {
this(dialog);
this.zoom = zoom;
this.zoomArea = zoomArea;
}
public ZoomBoxPanel(Zoomable dialog) {
this.dialog = dialog;
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
zoomPoint = e.getPoint();
// method paintComponent() called
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
// method paintComponent() called
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
zoomPoint = null;
// method paintComponent() called
repaint();
}
});
@Override
public void mouseMoved(MouseEvent e) {
zoomPoint = e.getPoint();
// method paintComponent() called
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
// method paintComponent() called
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
zoomPoint = null;
// method paintComponent() called
repaint();
}
});
}
/**
* This method paints the image and the zoom box.
* If the mouse point is out of the image bounds the zoom box is not displayed
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (dialog.getDisplayedImage() != null) {
Graphics2D g2d = (Graphics2D) g.create();
Point offset = getOffset();
g2d.drawImage(dialog.getDisplayedImage(), offset.x, offset.y, this);
if (zoomBoxEnabled && zoomPoint != null) {
BufferedImage zoomBuffer = updateBuffer(zoomPoint);
if (zoomBuffer != null) {
Rectangle bounds = getZoomBounds();
Rectangle imgBounds = getImageBounds();
if(zoomPoint.x > imgBounds.x && zoomPoint.x < offset.x + imgBounds.width && zoomPoint.y > offset.y && zoomPoint.y < offset.y + imgBounds.height) {
g2d.drawImage(zoomBuffer, bounds.x, bounds.y, this);
g2d.setColor(Color.RED);
g2d.draw(bounds);
}
}
}
g2d.dispose();
}
}
/**
* Getter of the zoom box visibility
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (dialog.getDisplayedImage() != null) {
Graphics2D g2d = (Graphics2D) g.create();
Point offset = getOffset();
g2d.drawImage(dialog.getDisplayedImage(), offset.x, offset.y, this);
if (zoomBoxEnabled && zoomPoint != null) {
BufferedImage zoomBuffer = updateBuffer(zoomPoint);
if (zoomBuffer != null) {
Rectangle bounds = getZoomBounds();
Rectangle imgBounds = getImageBounds();
if(zoomPoint.x > imgBounds.x && zoomPoint.x < offset.x + imgBounds.width && zoomPoint.y > offset.y && zoomPoint.y < offset.y + imgBounds.height) {
g2d.drawImage(zoomBuffer, bounds.x, bounds.y, this);
g2d.setColor(Color.RED);
g2d.draw(bounds);
}
}
}
g2d.dispose();
}
}
/**
* Getter of the zoom box visibility
* @return true if the zoom box is enabled,
* false otherwise
*/
public boolean zoomBoxEnabled() {
return zoomBoxEnabled;
}
public boolean zoomBoxEnabled() {
return zoomBoxEnabled;
}
/**
/**
* Setter for the visibility of the zoom box.
*/
public void setZoomBoxEnabled(boolean zoomBoxEnabled) {
this.zoomBoxEnabled = zoomBoxEnabled;
repaint();
}
public void setZoomBoxEnabled(boolean zoomBoxEnabled) {
this.zoomBoxEnabled = zoomBoxEnabled;
repaint();
}
/**
* Setter for the rate of zoom in the zoom box.
*/
public void setZoom(float zoom) {
this.zoom = zoom;
repaint();
}
/**
* Zoom in the zoom box.
*/
public void zoomInBox() {
this.zoom += zoomStep;
repaint();
}
/**
* Zoom out of the zoom box.
*/
public void zoomOutBox() {
if (this.zoom - this.zoomStep > zoomMin ) {
this.zoom -= this.zoomStep;
}
repaint();
}
/**
* Increase the zoom box area by ZOOM_AREA_ADD.
*/
public void increaseZoomBoxArea() {
if(this.zoomArea + this.zoomAreaAdd <= zoomAreaMax) {
this.zoomArea += this.zoomAreaAdd;
}
repaint();
}
/**
* Decrease the zoom box area by ZOOM_AREA_ADD.
*/
public void decreaseZoomBoxArea() {
if (this.zoomArea - this.zoomAreaAdd >= zoomAreaMin) {
this.zoomArea -= this.zoomAreaAdd;
}
repaint();
}
/**
* @return the offset of the image displayed in the panel
*/
protected Point getOffset() {
if (dialog.getDisplayedImage() == null) {
return new Point(0, 0);
}
int x = (getWidth() - dialog.getDisplayedImage().getWidth()) / 2;
int y = (getHeight() - dialog.getDisplayedImage().getHeight()) / 2;
return new Point(x, y);
}
if (dialog.getDisplayedImage() == null) {
return new Point(0, 0);
}
int x = (getWidth() - dialog.getDisplayedImage().getWidth()) / 2;
int y = (getHeight() - dialog.getDisplayedImage().getHeight()) / 2;
return new Point(x, y);
}
/**
* @return the bounds around the image displayed in the panel
*/
protected Rectangle getImageBounds() {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (dialog.getDisplayedImage() != null) {
bounds.setLocation(getOffset());
bounds.setSize(dialog.getDisplayedImage().getWidth(), dialog.getDisplayedImage().getHeight());
}
return bounds;
}
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (dialog.getDisplayedImage() != null) {
bounds.setLocation(getOffset());
bounds.setSize(dialog.getDisplayedImage().getWidth(), dialog.getDisplayedImage().getHeight());
}
return bounds;
}
/**
* This method creates the zoomed image around the mouse point.
*
......@@ -141,107 +196,111 @@ public class ZoomBoxPanel extends JPanel{
*
* @return the zoomed image that fits the zoom box
*/
protected BufferedImage updateBuffer(Point p) {
if (zoomPoint == null) {
return null;
}
Rectangle bounds = getZoomBounds();
Point offset = getOffset();
bounds.x = bounds.x - offset.x;
bounds.y = bounds.y - offset.y;
// if out of bounds
if (bounds.x < 0 || bounds.y < 0 || bounds.width <= 0 || bounds.height <= 0) {
return null;
}
BufferedImage zoomBuffer = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = zoomBuffer.createGraphics();
Image sample = dialog.getMatchingImage(bounds.x, bounds.y, bounds.width, bounds.height, zoom);
// if the zoom box is within image area
if(bounds.width == zoomArea && bounds.height == zoomArea) {
int srcx = (sample.getWidth(null)-bounds.width)/2;
int srcy = (sample.getHeight(null)-bounds.height)/2;
g2d.drawImage(sample, 0,0, bounds.width, bounds.height, srcx, srcy, srcx +bounds.width, srcy + bounds.height, this);
}
// if the zoom box intersects with image area
else {
// mouse point coordinates relative to image
int p1x = p.x - offset.x;
int p1y = p.y - offset.y;
int p2x;
int p2y;
// left
if(bounds.x == 0) {
// mouse point coordinates relative to subimage
p2x = p1x;
p2y = p1y - bounds.y;
}
// down
else if(bounds.y + bounds.height == dialog.getDisplayedImage().getHeight()) {
// mouse point coordinates relative to subimage
p2x = p1x - bounds.x;
p2y = p1y - bounds.y;
}
// right
else if(bounds.x + bounds.width == dialog.getDisplayedImage().getWidth()) {
// mouse point coordinates relative to subimage
p2x = p1x - bounds.x;
p2y = p1y - bounds.y;
}
// up
else {
// mouse point coordinates relative to subimage
p2x = p1x - bounds.x;
p2y = p1y;
}
// mouse point coordinates relative to zoomed subimage
int p3x = (int) (p2x*zoom);
int p3y = (int) (p2y*zoom);
// top left point of cropped area of the zoomed image
int p4x = p3x - (p2x);
int p4y = p3y - (p2y);
// specify the area of the zoomed image that needs to be drawn over the zoom box area
g2d.drawImage(sample, 0,0, bounds.width, bounds.height, p4x, p4y, p4x + bounds.width, p4y + bounds.height, this);
}
g2d.dispose();
return zoomBuffer;
}
/**
protected BufferedImage updateBuffer(Point p) {
if (zoomPoint == null) {
return null;
}
Rectangle bounds = getZoomBounds();
Point offset = getOffset();
Image scaled;
// coordinates relative to image
bounds.x = bounds.x - offset.x;
bounds.y = bounds.y - offset.y;
// if out of bounds
if (bounds.x < 0 || bounds.y < 0 || bounds.width <= 0 || bounds.height <= 0) {
return null;
}
BufferedImage zoomBuffer = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = zoomBuffer.createGraphics();
scaled = dialog.getMatchingImage(bounds.x, bounds.y, bounds.width, bounds.height, zoom);
// if the zoom box is within image area
if(bounds.width == zoomArea && bounds.height == zoomArea) {
int srcx = (scaled.getWidth(null)-bounds.width)/2;
int srcy = (scaled.getHeight(null)-bounds.height)/2;
g2d.drawImage(scaled, 0,0, bounds.width, bounds.height, srcx, srcy, srcx +bounds.width, srcy + bounds.height, this);
}
// if the zoom box intersects with image area
else {
// mouse point coordinates relative to image
int p1x = p.x - offset.x;
int p1y = p.y - offset.y;
int p2x;
int p2y;
// left
if(bounds.x == 0) {
// mouse point coordinates relative to subimage
p2x = p1x;
p2y = p1y - bounds.y;
}
// down
else if(bounds.y + bounds.height == dialog.getDisplayedImage().getHeight()) {
// mouse point coordinates relative to subimage
p2x = p1x - bounds.x;
p2y = p1y - bounds.y;
}
// right
else if(bounds.x + bounds.width == dialog.getDisplayedImage().getWidth()) {
// mouse point coordinates relative to subimage
p2x = p1x - bounds.x;
p2y = p1y - bounds.y;
}
// up
else {
// mouse point coordinates relative to subimage
p2x = p1x - bounds.x;
p2y = p1y;
}
// mouse point coordinates relative to zoomed subimage
int p3x = (int) (p2x*zoom);
int p3y = (int) (p2y*zoom);
// top left point of cropped area of the zoomed image
int p4x = p3x - (p2x);
int p4y = p3y - (p2y);
// specify the area of the zoomed image that needs to be drawn over the zoom box area
g2d.drawImage(scaled, 0,0, bounds.width, bounds.height, p4x, p4y, p4x + bounds.width, p4y + bounds.height, this);
}
g2d.dispose();
return zoomBuffer;
}
/**
* @return the bounds around the zoom box relative to the panel
*/
protected Rectangle getZoomBounds() {
Rectangle bounds = null;
if (zoomPoint != null && dialog.getDisplayedImage() != null) {
// zoom area created in such way so that mouse point is centered in the zoom box
int xPos = zoomPoint.x - (zoomArea / 2);
int yPos = zoomPoint.y - (zoomArea / 2);
Rectangle zoomBounds = new Rectangle(xPos, yPos, zoomArea, zoomArea);
Rectangle imageBounds = getImageBounds();
// create the zoom box as intersection of the image bounds and zoom bounds
bounds = imageBounds.intersection(zoomBounds);
}
return bounds;
}
protected Rectangle getZoomBounds() {
Rectangle bounds = null;
if (zoomPoint != null && dialog.getDisplayedImage() != null) {
// zoom area created in such way so that mouse point is centered in the zoom box
int xPos = zoomPoint.x - (zoomArea / 2);
int yPos = zoomPoint.y - (zoomArea / 2);
Rectangle zoomBounds = new Rectangle(xPos, yPos, zoomArea, zoomArea);
Rectangle imageBounds = getImageBounds();
// create the zoom box as intersection of the image bounds and zoom bounds
bounds = imageBounds.intersection(zoomBounds);
}
return bounds;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment