📄 visualization2dpanelwithfdp.java
字号:
/*
* This file is part of Caliph & Emir.
*
* Caliph & Emir is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Caliph & Emir is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Caliph & Emir; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright statement:
* --------------------
* (c) 2002-2005 by Mathias Lux (mathias@juggle.at)
* http://www.juggle.at, http://caliph-emir.sourceforge.net
*/
package at.lux.fotoretrieval.panels;
import at.lux.fotoretrieval.EmirConfiguration;
import at.lux.retrieval.calculations.DistanceMatrix;
import at.lux.retrieval.fdp.FDP;
import at.lux.retrieval.fdp.FDPParameters;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
/**
* Date: 14.01.2005
* Time: 00:00:38
*
* @author Mathias Lux, mathias@juggle.at
*/
public class Visualization2DPanelWithFdp extends JPanel implements MouseMotionListener, MouseListener, MouseWheelListener {
private EmirConfiguration emirConfiguration = EmirConfiguration.getInstance();
private float points[][];
private DistanceMatrix matrixFastmap;
private float maxX = 0f, maxY = 0f, minX = 0f, minY = 0f;
private java.util.List<String> fileList;
private static final int OFFSET = 20;
private java.util.List<BufferedImage> imageList;
private static final float IMG_MAXIMUM_SIDE = 20f;
private int imagesLoaded = 0;
private AffineTransform transform;
private double moveX = 0d, moveY = 0d;
private FdpThread fdpThread;
private double zoom = 1d;
private enum MouseState {
NONE, BUTTON1_PRESSED
};
private MouseState state = MouseState.NONE;
private Point lastPoint = null;
private FDP fdp = null;
/**
* Creates a new <code>JPanel</code> with a double buffer
* and a flow layout.
*/
public Visualization2DPanelWithFdp(float[][] points, DistanceMatrix matrixFastmap, java.util.List<String> files, boolean autoStartFDP) {
this.points = points;
this.matrixFastmap = matrixFastmap;
fileList = files;
addMouseMotionListener(this);
addMouseListener(this);
addMouseWheelListener(this);
init();
ImageLoaderThread lt = new ImageLoaderThread(this);
new Thread(lt).start();
FDPParameters fdpParameters = EmirConfiguration.getInstance().getFDPParameters();
fdpParameters.setGravitation(3d/Math.sqrt(points.length));
fdp = new FDP(matrixFastmap, fdpParameters, points);
if (autoStartFDP) {
fdpThread = new FdpThread(fdp, this);
fdpThread.start();
}
}
private void init() {
imageList = new LinkedList<BufferedImage>();
for (int i = 0; i < points.length; i++) {
float[] point = points[i];
if (point[0] > maxX) maxX = point[0];
if (point[1] > maxY) maxY = point[1];
if (point[0] < minX) minX = point[0];
if (point[1] < minY) minX = point[1];
}
}
/**
* Allows the iterative loading of image files ...
*
* @return true if another image file can be loaded, false otherwise.
*/
public boolean initNextImage() {
if (imagesLoaded < points.length) {
String pathname = fileList.get(imagesLoaded);
pathname = pathname.replace(".mp7.xml", ".jpg");
File f = new File(pathname);
BufferedImage bi = null;
try {
// System.out.println("Reading file " + imagesLoaded + ": " + pathname);
bi = ImageIO.read(f);
if (bi != null) bi = resize(bi);
} catch (IOException e) {
System.err.println("Error reading image " + pathname);
}
imageList.add(bi);
}
imagesLoaded++;
repaint();
if (imagesLoaded > points.length)
return false;
else
return true;
}
private BufferedImage resize(BufferedImage img) {
int height = img.getHeight();
int width = img.getWidth();
float scaleFactor = ((float) width) / IMG_MAXIMUM_SIDE;
if (height > width) {
scaleFactor = ((float) height) / IMG_MAXIMUM_SIDE;
}
int widthNew = (int) (((float) width) / scaleFactor);
int heightNew = (int) (((float) height) / scaleFactor);
BufferedImage bi = new BufferedImage(widthNew, heightNew, BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(img, 0, 0, bi.getWidth(), bi.getHeight(), null);
return bi;
}
/**
* Calls the UI delegate's paint method, if the UI delegate
* is non-<code>null</code>. We pass the delegate a copy of the
* <code>Graphics</code> object to protect the rest of the
* paint code from irrevocable changes
* (for example, <code>Graphics.translate</code>).
* <p/>
* If you override this in a subclass you should not make permanent
* changes to the passed in <code>Graphics</code>. For example, you
* should not alter the clip <code>Rectangle</code> or modify the
* transform. If you need to do these operations you may find it
* easier to create a new <code>Graphics</code> from the passed in
* <code>Graphics</code> and manipulate it. Further, if you do not
* invoker super's implementation you must honor the opaque property,
* that is
* if this component is opaque, you must completely fill in the background
* in a non-opaque color. If you do not honor the opaque property you
* will likely see visual artifacts.
* <p/>
* The passed in <code>Graphics</code> object might
* have a transform other than the identify transform
* installed on it. In this case, you might get
* unexpected results if you cumulatively apply
* another transform.
*
* @param g the <code>Graphics</code> object to protect
* @see #paint
* @see javax.swing.plaf.ComponentUI
*/
protected void paintComponent(Graphics g) {
maxX = getMax(0);
maxY = getMax(1);
minX = getMin(0);
minY = getMin(1);
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// save old transformations ...
AffineTransform old = g2.getTransform();
// erase background
g2.setColor(Color.black);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.green);
// Apply move
transform = AffineTransform.getTranslateInstance(moveX, moveY);
AffineTransform scale = AffineTransform.getScaleInstance(zoom,zoom);
transform.concatenate(scale);
g2.setTransform(transform);
float width = this.getWidth() - 2 * OFFSET;
float height = this.getHeight() - 2 * OFFSET;
for (int i = 0; i < points.length; i++) {
float[] point = points[i];
float x = (point[0] - minX) / (maxX - minX);
float y = (point[1] - minY) / (maxY - minY);
int projectionX = (int) (x * width) + OFFSET;
int projectionY = (int) (y * height) + OFFSET;
BufferedImage bi = null;
if (i < imageList.size()) bi = imageList.get(i);
if (bi != null) {
g2.drawImage(bi, projectionX - bi.getWidth() / 2, projectionY - bi.getHeight() / 2, null);
} else {
g2.fillOval(projectionX - 2, projectionY - 2, 4, 4);
}
}
// restore old transformations:
g2.setTransform(old);
g2.setFont(g2.getFont().deriveFont(Font.ITALIC, 8.5f));
g2.setColor(Color.gray);
g2.drawString("Click <alt> + <right mouse button> to start / stop FDP, zoom with mouse wheel.", 5, this.getHeight()-5);
// g2.setColor(Color.white);
// g2.drawString("Click <alt> + <left mouse button> to start / stop FDP.", 9, this.getHeight()-11);
}
/**
* Invoked when a mouse button is pressed on a component and then
* dragged. <code>MOUSE_DRAGGED</code> events will continue to be
* delivered to the component where the drag originated until the
* mouse button is released (regardless of whether the mouse position
* is within the bounds of the component).
* <p/>
* Due to platform-dependent Drag&Drop implementations,
* <code>MOUSE_DRAGGED</code> events may not be delivered during a native
* Drag&Drop operation.
*/
public void mouseDragged(MouseEvent e) {
// System.out.println("Dragged: " + e.getPoint());
if (lastPoint == null) lastPoint = e.getPoint();
if (state == MouseState.BUTTON1_PRESSED) {
moveX += e.getPoint().x - lastPoint.x;
moveY += e.getPoint().y - lastPoint.y;
repaint();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -