Commit 927a9cbb authored by kirandjiska's avatar kirandjiska

tmp commit before modif

parent 24058a56
......@@ -15,6 +15,7 @@ import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.UIManager;
......@@ -29,16 +30,15 @@ public class ZoomBoxView extends JPanel {
protected static final int ZOOM_AREA = 40;
private JComponent parent;
// private BufferedImage originalImage;
private JWindow popup;
private Boolean zoomBoxActivated = false;
private ConstraintsSelectionDialog dialog = null;
private BufferedImage buffer;
private float zoomLevel = 4f;
public ZoomBoxView(JComponent parent) {
this.parent = parent;
public ZoomBoxView(ConstraintsSelectionDialog dialog) {
this.dialog = dialog;
popup = new JWindow();
popup.setLayout(new BorderLayout());
popup.add(this);
......@@ -48,10 +48,12 @@ public class ZoomBoxView extends JPanel {
@Override
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
// System.out.println(p);
Point pos = e.getLocationOnScreen();
updateBuffer(p);
popup.setLocation(pos.x + 16, pos.y + 16);
// popup.setLocation(pos.x - popup.getWidth()/2, pos.y - popup.getHeight()/2);
Point origPoint = getOriginalPointCoordinates(p); // get the original position of the point coordinate on the zoomed image
updateBuffer(origPoint);
// popup.setLocation(pos.x + 16, pos.y + 16);
popup.setLocation(pos.x - popup.getWidth()/2, pos.y - popup.getHeight()/2);
repaint();
}
......@@ -69,18 +71,19 @@ public class ZoomBoxView extends JPanel {
};
parent.addMouseListener(ma);
parent.addMouseMotionListener(ma);
dialog.getImgLabel().addMouseListener(ma);
dialog.getImgLabel().addMouseMotionListener(ma);
}
/** p - cursor coordinates relative to parent window*/
private void updateBuffer(Point p) {
/**private void updateBuffer(Point p) {
int width = Math.round(ZOOM_AREA);
int height = Math.round(ZOOM_AREA);
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = buffer.createGraphics();
Graphics2D g2d =(Graphics2D) originalImage.getGraphics();
AffineTransform at = new AffineTransform();
// center cursor in the zoomBox
int xPos = (ZOOM_AREA / 2) - p.x;
int yPos = (ZOOM_AREA / 2) - p.y;
......@@ -92,6 +95,7 @@ public class ZoomBoxView extends JPanel {
}
// if outside the parent window
// fix parent size on original image size
if ((xPos * -1) + ZOOM_AREA > parent.getWidth()) {
xPos = (parent.getWidth() - ZOOM_AREA) * -1;
}
......@@ -103,6 +107,15 @@ public class ZoomBoxView extends JPanel {
g2d.setTransform(at);
parent.paint(g2d);
g2d.dispose();
}*/
private void updateBuffer(Point p) {
int width = Math.round(ZOOM_AREA);
int height = Math.round(ZOOM_AREA);
buffer = dialog.getImage().getSubimage(p.x - ZOOM_AREA/2, p.y - ZOOM_AREA/2, width, height);
//rajoute condition s'il sort dehors
}
@Override
......@@ -125,7 +138,7 @@ public class ZoomBoxView extends JPanel {
g2d.drawLine(getWidth()/2-1, 0, getWidth()/2-1, getHeight()-1);
g2d.drawLine(0,getHeight()/2-1,getWidth()-1,getHeight()/2-1 );
g2d.drawRect(getWidth()/2-1-4, getHeight()/2-1-4, 8, 8);
// g2d.dispose();
g2d.dispose();
}
......@@ -134,24 +147,18 @@ public class ZoomBoxView extends JPanel {
zoomBoxActivated = visibility;
}
private Point getOriginalPointCoordinates(Point p) {
//point coordinated relative to original image
int x = (int) ((p.getX()
- (int) ((dialog.getImgLabel().getWidth() - (dialog.getImgLabel().getIcon().getIconWidth())) / 2.0)) / dialog.getmZoomRate());
int y = (int) ((p.getY()
- (int) ((dialog.getImgLabel().getHeight() - (dialog.getImgLabel().getIcon().getIconHeight())) / 2.0)) / dialog.getmZoomRate());
// if (x >= 0 && y >= 0 && x < originalImage.getWidth() * zoomLevel
// && y < originalImage.getHeight() * zoomLevel) {
return new Point(x,y);
}
/** public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
// TestPane pane = new TestPane();
// ZoomPane zoomPane = new ZoomPane(pane);
}
});
}*/
}
}
package mustic.gui.dialog.classifier;
public class ZoomBoxWindow {
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
}
public class ZoomBoxWindow /*extends JPanel*/ {
private ConstraintsSelectionDialog dialog = null;
private Point zoomPoint; // cursor point relative to img panel
private boolean zoomEnabled = true; // a variable indicating if the zoom box panel is enabled
private int zoomArea = 80; // size of zoom box panel
private float zoom = 2.0f; // zoomRate
public ZoomBoxWindow(ConstraintsSelectionDialog dialog) {
this.dialog = dialog;
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
zoomPoint = getOriginalPointCoordinates(e.getPoint()); // at every mouse motion get coordinates of cursor point and repaint the zoom box panel
// System.out.println("ZoomPoint on mouse motion " + zoomPoint);
paintZoomBox();
// repaint(); //calls paintComponent()
// dialog.getImgLabel().validate();
// dialog.getImgLabel().repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
if (zoomEnabled) {
// repaint();
paintZoomBox();
// dialog.getImgLabel().repaint();
}
}
@Override
public void mouseExited(MouseEvent e) {
zoomPoint = null;
// repaint();
paintZoomBox();
// dialog.getImgLabel().repaint();
}
};
dialog.getImgLabel().addMouseListener(ma);
dialog.getImgLabel().addMouseMotionListener(ma);
}
public float getZoom() {
return zoom;
}
public void setZoom(float zoom) {
this.zoom = zoom;
paintZoomBox();
dialog.getImgLabel().repaint();
}
public int getZoomArea() {
return zoomArea;
}
public void setZoomArea(int zoomArea) {
this.zoomArea = zoomArea;
paintZoomBox();
dialog.getImgLabel().repaint();
}
public boolean isZoomEnabled() {
return zoomEnabled;
}
public void setZoomEnabled(boolean zoomEnabled) {
this.zoomEnabled = zoomEnabled;
paintZoomBox();
dialog.getImgLabel().repaint();
}
// @Override
// public Dimension getPreferredSize() {
// return new Dimension(Math.round(zoomArea * zoom), Math.round(zoomArea * zoom));
// }
//offset of image relative to JLabel
protected Point getOffset() {
if (dialog.getImage() == null) {
return new Point(0, 0);
}
int x = (dialog.getImgLabel().getWidth() - dialog.getImgLabel().getIcon().getIconWidth()) / 2;
int y = (dialog.getImgLabel().getHeight() - dialog.getImgLabel().getIcon().getIconHeight()) / 2;
return new Point(x, y);
}
// set a rectangle frame around image
// create bounds of image size, the offset needed is the offset of the getOffset function
// return this rectangle frame
protected Rectangle getImageBounds() {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (dialog.getImage() != null) {
bounds.setLocation(getOffset());
bounds.setSize(dialog.getImage().getWidth(), dialog.getImage().getHeight());
}
return bounds;
}
//method called on mouse motion
protected void paintZoomBox() {
if(dialog.getImage() != null) {
Graphics g2d = dialog.getImage().getGraphics(); // get parent image graphics
if(zoomPoint != null) {
BufferedImage zoomBuffer = updateBuffer(zoomPoint); // create the zoomed image
if(zoomBuffer != null) {
Rectangle bounds = getZoomBounds();
System.out.println(zoomPoint);
System.out.println(bounds);
g2d.drawImage(zoomBuffer, bounds.x, bounds.y,100,100, null); // draw zoomed image on top of the another
g2d.setColor(Color.RED);
g2d.drawRect(bounds.x, bounds.y,bounds.width,bounds.height);
}
}
// g2d.dispose();
}
}
/** @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
Graphics2D g2d = (Graphics2D) g.create();
Point offset = getOffset();
g2d.drawImage(img, offset.x, offset.y, this);
if (zoomPoint != null) { // if clicked inside image bounds
BufferedImage zoomBuffer = updateBuffer(zoomPoint); // create the zoomed image
if (zoomBuffer != null) {
Rectangle bounds = getZoomBounds();
g2d.drawImage(zoomBuffer, bounds.x, bounds.y, this);
g2d.setColor(Color.RED);
g2d.draw(bounds); // draw a red rectangle frame around zoom box panel
}
}
g2d.dispose();
}
}*/
protected Rectangle getZoomBounds() {
Rectangle bounds = null;
if (zoomPoint != null && dialog.getImage() != null) {
int zoomArea = getZoomArea();
//top left point of zoom box panel
int xPos = zoomPoint.x - (zoomArea / 2);
int yPos = zoomPoint.y - (zoomArea / 2);
//create a rectangle around zoom box panel
Rectangle zoomBounds = new Rectangle(xPos, yPos, zoomArea, zoomArea);
Rectangle imageBounds = getImageBounds();
// a voir
bounds = imageBounds.intersection(zoomBounds);
System.out.println(bounds);
}
return bounds;
}
//create a zoomed picture
protected BufferedImage updateBuffer(Point p) {
if (zoomPoint == null) {
return null;
}
Rectangle bounds = getZoomBounds(); // rectangle around zoom box panel
Point offset = getOffset(); // top left point of image
bounds.translate(-offset.x, -offset.y); // move rectangle to position (-offset.x, -offset.y) A VOIR?
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); // A VOIR
Graphics2D g2d = zoomBuffer.createGraphics();
BufferedImage sample = dialog.getImage().getSubimage(bounds.x, bounds.y, bounds.width, bounds.height); //get subimage of image of size zoom box panel starting of its location
double zoom = getZoom();
//scale the image of rate bounds.width(height) * zoom along the x(y)axis
Image scaled = sample.getScaledInstance((int) (bounds.width * zoom), (int) (bounds.height * zoom), Image.SCALE_SMOOTH);
g2d.drawImage(scaled, 0, 0, null); // draw scaled image inside of zoomBuffer image bounds
g2d.dispose();
return zoomBuffer;
}
protected void setZoomBoxVisibility(boolean visibility) {
// popup.setVisible(visibility);
zoomEnabled = visibility;
}
private Point getOriginalPointCoordinates(Point p) {
//point coordinated relative to original image
int x = (int) ((p.getX()
- (int) ((dialog.getImgLabel().getWidth() - (dialog.getImgLabel().getIcon().getIconWidth())) / 2.0)) / dialog.getmZoomRate());
int y = (int) ((p.getY()
- (int) ((dialog.getImgLabel().getHeight() - (dialog.getImgLabel().getIcon().getIconHeight())) / 2.0)) / dialog.getmZoomRate());
// if (x >= 0 && y >= 0 && x < originalImage.getWidth() * zoomLevel
// && y < originalImage.getHeight() * zoomLevel) {
return new Point(x,y);
}
}
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