Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.TextArea;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import java.awt.BorderLayout;
- import java.net.*;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JSplitPane;
- import javax.swing.JTextField;
- import javax.swing.Timer;
- import javax.swing.event.MouseInputAdapter;
- import java.util.ArrayList;
- import java.util.Arrays;
- /*
- * TODO ToolBox
- * TODO GUI
- */
- public class ImageMapping extends JPanel
- {
- private static final long serialVersionUID = 1337L/*ol*/;
- public static final Color DARK_BLUE = new Color(0,0,50);
- public static final Color DARK_GREEN = new Color(0,50,0);
- public static final Color DARK_RED = new Color(50,0,0);
- static ImageMapping mapper;
- public static void main(String[] args)
- {
- mapper = new ImageMapping();
- mapper.setVisible(true);
- final Timer timer = new Timer(30, new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- mapper.repaint();
- }});
- timer.start();
- }
- public String toString()
- {
- return iconList.toMapString();
- }
- public String toCookString()
- {
- return iconList.toCookString();
- }
- private final ObjList iconList;
- private BufferedImage Map;
- private String mapName;
- private Icon using = null;
- private DotList usingList = null;
- private final JFrame frame = new JFrame("Image Mapping Tool");
- private final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- public static BufferedImage COMPASS;{
- try {
- COMPASS = ImageIO.read(new URL("http://urbancowgurl777.wikia.com/wiki/Special:FilePath/Compass.png"));
- } catch (MalformedURLException e) {e.printStackTrace();}
- catch (IOException e) {e.printStackTrace();}
- }
- public static BufferedImage TITLEBOX;{
- try {
- TITLEBOX = ImageIO.read(new URL("http://urbancowgurl777.wikia.com/wiki/Special:FilePath/TitleBackground.png"));
- } catch (MalformedURLException e) {e.printStackTrace();}
- catch (IOException e) {e.printStackTrace();}
- }
- private JPanel mapPanel = new JPanel(new BorderLayout());
- private JPanel toolBox = new JPanel(new BorderLayout());
- private JComboBox dotListChooser = new JComboBox();
- public ImageMapping()
- {
- iconList = new ObjList(this);
- addMouse();
- addMenu();
- toolBoxSetUp();
- mapPanel.add(this);
- mapPanel.setLayout(null);
- mapPanel.setMinimumSize(new Dimension(400,300));
- split.setLeftComponent(mapPanel);
- split.setRightComponent(toolBox);
- frame.add(split);
- frame.setSize(800, 500);
- frame.setLocation(400,400);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- private void toolBoxSetUp()
- {
- dotListChooser.addItem("Select a dot");
- JPanel wrapMid = new JPanel(new BorderLayout());
- JPanel wrapMid2 = new JPanel(new BorderLayout());
- toolBox.add(wrapMid,BorderLayout.EAST);
- wrapMid.add(wrapMid2,BorderLayout.NORTH);
- wrapMid2.add(dotListChooser, BorderLayout.NORTH);
- JButton makeList = new JButton("New Dot");
- makeList.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- Object sel = dotListChooser.getSelectedItem();
- if (!(sel instanceof DotList))
- return;
- else if (usingList == sel)
- return;
- else
- {
- if (usingList != null)
- usingList.removeLast();
- usingList = (DotList) sel;
- usingList.add();
- using = usingList.lastDot();
- ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- }
- }});
- wrapMid2.add(makeList, BorderLayout.WEST);
- JButton doneList = new JButton("Done with");
- doneList.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- if (usingList != null){
- usingList.removeLast();
- usingList = null;
- using = null;
- ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
- }
- }});
- wrapMid2.add(doneList, BorderLayout.EAST);
- JButton rmvList = new JButton("Remove this list");
- rmvList.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- Object sel = dotListChooser.getSelectedItem();
- if (!(sel instanceof DotList))
- return;
- dotListChooser.removeItem(sel);
- iconList.remove((Icon) sel);
- if (usingList == sel){
- usingList = null;
- using = null;
- ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
- }
- }});
- wrapMid2.add(rmvList, BorderLayout.SOUTH);
- }
- public int[] mapLocation()
- {
- return new int[]{mapPanel.getX(),mapPanel.getY()};
- }
- public void setVisible(boolean arg)
- {
- frame.setVisible(arg);
- }
- public JPanel panel()
- {
- return mapPanel;
- }
- public void addMouse()
- {
- addMouseListener(new MouseListener(){
- public void mouseClicked(MouseEvent arg0)
- {
- if (using != null)
- {
- Icon ic = using;
- if (ic instanceof Dot)
- {
- ((Dot) ic).setLocation(arg0.getX(), arg0.getY());
- if (usingList == null)
- {
- ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
- using = null;
- }
- else
- {
- usingList.add();
- using = usingList.lastDot();
- }
- return;
- }
- }
- }
- public void mouseEntered(MouseEvent arg0)
- {
- }
- public void mouseExited(MouseEvent arg0)
- {
- }
- public void mousePressed(MouseEvent arg0)
- {
- }
- public void mouseReleased(MouseEvent arg0)
- {
- }});
- }
- public void addMenu()
- {
- JMenuBar menu = new JMenuBar();
- frame.setJMenuBar(menu);
- JMenu info = new JMenu("Info");
- JMenu add = new JMenu("Add icon");
- JMenuItem newDot = new JMenuItem("New dot");
- JMenuItem newDotList = new JMenuItem("New set of dots");
- JMenuItem newImage = new JMenuItem("New Map");
- newImage.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- dialog("Enter the exact filename", 'M', ImageMapping.this);
- }});
- newDot.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- newDot(false);
- }});
- newDotList.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- newDot(true);
- }});
- info.add(newImage);
- add.add(newDot);
- add.add(newDotList);
- menu.add(info);
- menu.add(add);
- JMenu syntax = new JMenu("Print Syntax");
- JMenuItem cook = new JMenuItem("Cook Syntax");
- cook.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- syntax(true);
- }});
- JMenuItem standard = new JMenuItem("Standard");
- standard.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- syntax(false);
- }});
- syntax.add(cook);
- syntax.add(standard);
- menu.add(syntax);
- }
- public void paint(Graphics g)
- {
- g.clearRect(0, 0, getWidth(), getHeight());
- if (Map != null)
- g.drawImage(Map,0,0,null);
- iconList.paint(g);
- }
- public void setMapName(String file)
- {
- file = file.replace(' ', '_');
- file = file.replace(".png", "");
- file = file.replaceFirst("[Ff]ile:", "");
- mapName = file;
- setMapFile();
- }
- public void setMapFile()
- {
- try {
- Map = ImageIO.read(new URL("http://runescape.wikia.com/special:FilePath/"+mapName+".png"));
- super.setBounds(0, 0, Map.getWidth() + 150, Map.getHeight() + 150);
- } catch (MalformedURLException e) {
- warning("The file name entered was bad");
- } catch (IOException e) {
- warning("Failed to fetch the desired file");
- }
- repaint();
- }
- public String mapFileName()
- {
- return mapName + ".png";
- }
- /**
- * Color chooser so I don't have to recreate lulz
- */
- public JComboBox colors()
- {
- String[] cols = {"Black","Blue","Cyan","Dark Blue","Dark Green",
- "Dark Gray","Dark Red","Gray","Green","Light Gray",
- "Magenta","Orange","Pink","Red","White","Yellow"
- };
- JComboBox colors = new JComboBox(cols);
- return colors;
- }
- /**
- * Create a new dot or dot list
- * @param list - true for list, false for dot
- */
- public void newDot(final boolean list)
- {
- usingList = null;
- if (!list && using != null)
- {
- warning("Please place your last dot first");
- return;
- }
- String species = (list) ? "dot list" : "dot";
- final JFrame window = new JFrame("New " + species);
- window.setLocation(300,300);
- window.setSize(200,150);
- window.setResizable(false);
- final JComboBox colors = colors();
- JPanel wrapLink = new JPanel(new BorderLayout());
- JPanel wrap = new JPanel(new BorderLayout());
- final JTextField inputLink = new JTextField();
- wrapLink.add(new JLabel("Page Link"), BorderLayout.NORTH);
- wrapLink.add(inputLink, BorderLayout.SOUTH);
- wrap.add(wrapLink, BorderLayout.SOUTH);
- wrap.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
- window.add(wrap,BorderLayout.NORTH);
- JPanel colorWrap = new JPanel(new BorderLayout());
- JPanel midWrap = new JPanel(new BorderLayout());
- colorWrap.add(colors,BorderLayout.NORTH);
- final JButton colo = new JButton(" ");
- colo.setEnabled(false);
- colorWrap.add(colo,BorderLayout.SOUTH);
- midWrap.add(colorWrap,BorderLayout.NORTH);
- JButton ok = new JButton("Make " + species);
- colors.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- colo.setBackground(getColor((String)colors.getSelectedItem()));
- }});
- ok.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- String col = (String)colors.getSelectedItem();
- String link = inputLink.getText();
- if (link.equals("") || link == null)
- {
- warning("No link was entered");
- return;
- }
- else if (col.equals("") || col == null)
- {
- warning("No color selected");
- return;
- }
- if (list)
- {
- DotList dotList = new DotList(link,col);
- iconList.add(dotList);
- dotListChooser.addItem(dotList);
- }
- else
- {
- Dot dot = new Dot(new int[]{-10,-10},col,link,iconList);
- using = dot;
- iconList.add(dot);
- ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- }
- ImageMapping.this.frame.validate();
- ImageMapping.this.frame.repaint();
- window.setVisible(false);
- }});
- window.add(midWrap);
- midWrap.add(ok, BorderLayout.SOUTH);
- window.setVisible(true);
- }
- /**
- * Create a new window with the syntax for image mapping
- * @param CookMePlox - use Cook's syntax or not
- */
- public void syntax(boolean CookMePlox)
- {
- JFrame frame = new JFrame("Template Syntax");
- TextArea syntax = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
- frame.setLocation(100,100);
- frame.setSize(300,500);
- syntax.setEditable(false);
- String text = (CookMePlox) ? toCookString() : toString();
- syntax.setText(text);
- frame.add(syntax);
- frame.setVisible(true);
- }
- public boolean locationInBounds(int[] xy)
- {
- return locationInBounds(xy[0], xy[1]);
- }
- public boolean locationInBounds(int x, int y)
- {
- if (x < 0 || x > Map.getWidth() || y < 0 || y > Map.getHeight())
- return false;
- else
- return true;
- }
- /**
- * Input a string with a method and then change a string in
- * the passed ImageMapping; the string to change will be
- * identified by a token char
- *
- * Had to implement it this way because apparently
- * <tt>JOptionPane.showInputDialog()</tt> uses <tt>next()</tt>
- * instead of <tt>nextLine</tt> or some other. And forcing
- * underscores instead of spaces isn't very userfriendly
- * now is it?
- * @param message - Message to give to the user
- * @param name - token
- * @param map - ImageMapper
- *
- * TODO Or heck, a showConfirmDialogue and add a text field component to that dialog.
- */
- public static void dialog(String message, final char name, final ImageMapping map)
- {
- final JFrame frame = new JFrame("Input");
- frame.setSize(200,100);
- frame.setLocation(300,300);
- final JTextField input = new JTextField();
- input.setSize(120, 15);
- JPanel wrap = new JPanel(new BorderLayout());
- wrap.add(input,BorderLayout.NORTH);
- JButton ok = new JButton("OK");
- frame.add(new JLabel(message), BorderLayout.NORTH);
- frame.add(wrap);
- frame.add(ok, BorderLayout.SOUTH);
- ok.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- String str = input.getText();
- frame.setVisible(false);
- setString(str, name, map);
- }});
- frame.setVisible(true);
- }
- private static void setString(String text, char name, ImageMapping map)
- {
- switch(name){
- case 'M' :
- case 'm' :
- map.setMapName(text);
- break;
- }
- }
- public static void warning(String message)
- {
- final JFrame frame = new JFrame("Error");
- JTextField warning = new JTextField(message);
- warning.setEditable(false);
- frame.setSize(200,100);
- frame.setLocation(300,300);
- JButton ok = new JButton("OK");
- frame.add(warning);
- frame.add(ok, BorderLayout.SOUTH);
- ok.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- frame.setVisible(false);
- }});
- frame.setVisible(true);
- }
- public static Color getColor(String c)
- {
- Color ret = Color.WHITE;
- c = c.replace(" ", "");
- c = c.replace("_", "");
- if (c.equalsIgnoreCase("BLACK"))
- ret = Color.BLACK;
- else if (c.equalsIgnoreCase("BLUE"))
- ret = Color.BLUE;
- else if (c.equalsIgnoreCase("CYAN"))
- ret = Color.CYAN;
- else if (c.equalsIgnoreCase("DARKBLUE"))
- ret = DARK_BLUE;
- else if (c.equalsIgnoreCase("DARKGRAY"))
- ret = Color.DARK_GRAY;
- else if (c.equalsIgnoreCase("DARKGREEN"))
- ret = DARK_GREEN;
- else if (c.equalsIgnoreCase("DARKRED"))
- ret = DARK_RED;
- else if (c.equalsIgnoreCase("GRAY"))
- ret = Color.GRAY;
- else if (c.equalsIgnoreCase("GREEN"))
- ret = Color.GREEN;
- else if (c.equalsIgnoreCase("LIGHTGRAY"))
- ret = Color.LIGHT_GRAY;
- else if (c.equalsIgnoreCase("MAGENTA"))
- ret = Color.MAGENTA;
- else if (c.equalsIgnoreCase("ORANGE"))
- ret = Color.ORANGE;
- else if (c.equalsIgnoreCase("PINK"))
- ret = Color.PINK;
- else if (c.equalsIgnoreCase("RED"))
- ret = Color.RED;
- else if (c.equalsIgnoreCase("WHITE"))
- ret = Color.WHITE;
- else if (c.equalsIgnoreCase("YELLOW"))
- ret = Color.YELLOW;
- return ret;
- }
- /*
- * All the stupid classes are appearing under this comment
- * This comment is just a delimiter for stuff
- * for me
- * so
- * ignore
- * it
- * Please
- * Go away
- */
- /**
- * This interface forces classes to implement a second species
- * of the <tt>toString()</tt> method which prints the imagemapping code
- * using Cook's imagemapping templates
- */
- public interface CookMePlox
- {
- public String toCookString();
- }
- public interface ImageMaps
- {
- public String toMapString();
- }
- /**
- * The Icon class
- * Icons are either a specific shape or a list of Shapes
- * This doc is kinda just here to delimit
- * So ya...
- */
- public interface Icon extends CookMePlox, ImageMaps
- {
- public abstract String link();
- public abstract void setLink(String s);
- public abstract void paint(Graphics g);
- }
- public class ObjList implements CookMePlox
- {
- private ArrayList<Icon> list = new ArrayList<Icon>();
- private final ImageMapping map;
- public ObjList(ImageMapping mapName)
- {
- map = mapName;
- }
- public void add(Icon i)
- {
- list.add(i);
- }
- public Icon lastIcon()
- {
- if (list.size() > 0)
- return list.get(list.size() - 1);
- else
- return null;
- }
- public void remove(Icon i)
- {
- list.remove(i);
- }
- public String toMapString()
- {
- String returnString = "<imagemap>\n" +
- map.mapFileName() +
- "\ndesc none";
- for (Icon i : list)
- if (checkBounds(i))
- returnString += "\n" + i.toMapString();
- returnString += "\n</imagemap>";
- return returnString;
- }
- public String toCookString()
- {
- String returnString = "<div style=\"position:relative;\">\n" +
- "[[File:"+ map.mapFileName() + "]]";
- for (Icon i : list)
- if (checkBounds(i))
- returnString += "\n" + i.toCookString();
- returnString += "\n</div>";
- return returnString;
- }
- public void paint(Graphics g)
- {
- for (Icon a : list)
- a.paint(g);
- }
- public boolean checkBounds(Icon i)
- {
- if (i instanceof Dot)
- return (mapper.locationInBounds(((Dot) i).locationOfEdge()));
- else if (i instanceof IconList)
- return true;
- else
- return false;
- }
- }
- public class IconList implements Icon
- {
- private ArrayList<Icon> list = new ArrayList<Icon>();
- private String link;
- public String toString()
- {
- return list.size() + " elements";
- }
- public String toMapString()
- {
- String returnString = "";
- for (Icon i : list)
- if (inBounds(i))
- returnString += "\n" + i.toMapString();
- returnString = returnString.trim();
- return returnString;
- }
- public String toCookString()
- {
- String returnString = "";
- for (Icon i : list)
- if (inBounds(i))
- returnString += "\n" + i.toCookString();
- returnString = returnString.trim();
- return returnString;
- }
- public void remove(Icon arg)
- {
- list.remove(arg);
- }
- public String link()
- {
- return link;
- }
- public void setLink(String s)
- {
- link = s;
- }
- public void paint(Graphics g)
- {
- for (Icon i : list)
- i.paint(g);
- }
- public boolean inBounds(Icon i)
- {
- if (i instanceof Dot)
- return (mapper.locationInBounds(((Dot) i).locationOfEdge()));
- else if (i instanceof IconList)
- return true;
- else
- return false;
- }
- }
- /**
- * List for Dots, will create multiple image dots with the same
- * color and same page to which they link
- */
- public class DotList extends IconList
- {
- private String link = "Test";
- private String color;
- private final ArrayList<Dot> dots = new ArrayList<Dot>();
- public DotList(String lnk, String col)
- {
- link = lnk;
- color = col;
- }
- public void add()
- {
- dots.add(new Dot(new int[]{-10,-10}, this));
- }
- public Dot lastDot()
- {
- if (dots.size() == 0)
- return null;
- return dots.get(dots.size()-1);
- }
- public String link()
- {
- return link;
- }
- public void setLink(String l)
- {
- for(Dot d : dots)
- d.setLink(l);
- }
- public String color()
- {
- return color;
- }
- public void setColor(String c)
- {
- for(Dot d : dots)
- d.setColor(c);
- }
- public void remove(Dot d)
- {
- dots.remove(d);
- }
- public void removeLast()
- {
- if (dots.size() == 0)
- return;
- dots.remove(lastDot());
- }
- public String toString()
- {
- return link + "; " + color;
- }
- public String toMapString()
- {
- //Comment labeling what the upcoming list of dots are mapping
- String returnString = "# Dots for " + link;
- for(Dot d : dots)
- if (inBounds(d))
- returnString += "\n" + d.toMapString();
- return returnString.trim();
- }
- public String toCookString()
- {
- String returnString = "";
- for(Dot d : dots)
- if (inBounds(d))
- returnString += "\n" + d.toCookString();
- return returnString.trim();
- }
- public void paint(Graphics g)
- {
- for (Icon i : dots)
- i.paint(g);
- }
- public boolean inBounds(Dot i)
- {
- return (mapper.locationInBounds(i.locationOfEdge()));
- }
- }
- /**
- * The Dot class
- * This doc is kinda just here to delimit
- * So ya...
- */
- public class Dot extends Component implements Icon
- {
- private static final long serialVersionUID = 1L;
- private int[] location = {-10,-10};
- private final Object parent;
- private String link;
- private String color;
- public static final int RADIUS = 4;
- public Dot(int[] l, DotList p)
- {
- location = Arrays.copyOf(l, 2);
- parent = p;
- link = ((DotList) parent).link();
- color = ((DotList) parent).color();
- mapper.panel().add(this, 0);
- setLocation(l);
- addMouse();
- }
- public Dot(int[] l, String c, String w, ObjList p)
- {
- location = Arrays.copyOf(l, 2);
- parent = p;
- color = c;
- link = w;
- mapper.panel().add(this, 0);
- addMouse();
- }
- public String toString()
- {
- return color + "dot; " + link + "; at " + location[0] + ", " + location[1];
- }
- /**
- * Returns the MediaWiki <imagemap> syntax for a circle at the given x then y values
- * and its appropriate link
- */
- public String toMapString()
- {
- return "circle "+locationOfEdge()[0]+" "+locationOfEdge()[1]+" 8 [["+link()+"]]";
- }
- public String toCookString()
- {
- return "{{Dot|"+locationOfEdge()[0]+"|"+locationOfEdge()[1]+"|"+
- color + "|" + link + "}}";
- }
- public String link()
- {
- return link;
- }
- public void setLocation(int[] xy)
- {
- location = xy;
- }
- public void setLocation(int x, int y)
- {
- location[0] = x;
- location[1] = y;
- super.setBounds(x, y, RADIUS * 2, RADIUS * 2);
- }
- public int[] locationOfCenter()
- {
- return new int[]{location[0] + RADIUS, location[1] + RADIUS};
- }
- /**
- * Location of the top left corner;
- * 6 pixels away from center in each direction
- */
- public int[] locationOfEdge()
- {
- return location;
- }
- public void removeSelf()
- {
- if (parent instanceof DotList)
- ((DotList) parent).remove(this);
- else if (parent instanceof ObjList)
- ((ObjList) parent).remove(this);
- }
- public void setLink(String l)
- {
- link = l;
- }
- public void setColor(String c)
- {
- color = c;
- }
- public String color()
- {
- return color;
- }
- public void paint(Graphics g)
- {
- g.setColor(getColor(color));
- g.fillOval(location[0], location[1], RADIUS * 2, RADIUS * 2);
- }
- private void addMouse()
- {
- addMouseMotionListener(new MouseMotionListener(){
- public void mouseDragged(MouseEvent arg0){
- int xx = (arg0.getXOnScreen() - mapper.panel().getLocationOnScreen().x - RADIUS);
- int yy = (arg0.getYOnScreen() - mapper.panel().getLocationOnScreen().y - RADIUS);
- setLocation(xx, yy);
- repaint();
- }
- public void mouseMoved(MouseEvent e) {}
- });
- addMouseListener(new MouseInputAdapter(){
- public void mouseClicked(MouseEvent arg0) {
- if (arg0.getButton() == MouseEvent.BUTTON1 && arg0.isControlDown())
- {
- removeSelf();
- }
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement