Created
April 16, 2013 04:24
-
-
Save Bradsta/5393330 to your computer and use it in GitHub Desktop.
MapLabel - created in 2009/2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Cursor; | |
import java.awt.Point; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.datatransfer.ClipboardOwner; | |
import java.awt.datatransfer.StringSelection; | |
import java.awt.datatransfer.Transferable; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.net.URL; | |
import javax.swing.ImageIcon; | |
import javax.swing.JLabel; | |
import worldmaptool.WorldMapTool.PicInfo; | |
/** | |
* @author Bradsta | |
* | |
*Panel used for image and calculations | |
*/ | |
@SuppressWarnings("serial") | |
public class MapLabel extends JLabel { | |
private final URL imageURL; | |
private Point hoveredPoint = null; | |
private Point clickedPoint = null; | |
private double pixelsPerTileH; | |
private double pixelsPerTileW; | |
MapLabel(URL picURL) { | |
this.imageURL = picURL; | |
} | |
public void setIcon() { | |
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
setIcon(new ImageIcon(imageURL)); | |
} | |
static StringSelection st = new StringSelection(""); | |
static ClipboardOwner c = new ClipboardOwner() { | |
public void lostOwnership(Clipboard clipboard, Transferable contents) { | |
//added to clipboard in getHoveringTile() | |
} | |
}; | |
public Point getHoveringTile(PicInfo PI) throws InterruptedException { | |
this.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseClicked(MouseEvent arg0) { | |
clickedPoint = arg0.getPoint(); | |
} | |
}); | |
pixelsPerTileH = (double) ((double) PI.dimensions.y / (double) (PI.originTile.y - PI.northTile.y)); | |
pixelsPerTileW = (double) ((double) PI.dimensions.x / (double) (PI.eastTile.x - PI.northTile.x)); | |
hoveredPoint = getMousePosition(); | |
if (clickedPoint != null) { | |
Point clicked = new Point((int) Math.nextUp((double) PI.northTile.x + ((double) clickedPoint.getX() / (double) pixelsPerTileW)), | |
(int) Math.nextUp((double) PI.northTile.y + ((double) clickedPoint.getY() / (double) pixelsPerTileH))); | |
st = new StringSelection("(" + (int) clicked.x + "," | |
+ (int) clicked.y + ")"); | |
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(st, c); | |
clickedPoint = null; | |
} | |
if (hoveredPoint != null) | |
return new Point((int) Math.nextUp((double) PI.northTile.x + ((double) hoveredPoint.getX() / (double) pixelsPerTileW)), | |
(int) Math.nextUp((double) PI.northTile.y + ((double) hoveredPoint.getY() / (double) pixelsPerTileH))); | |
else | |
return new Point(0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment