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.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.JOptionPane;
- import javax.swing.JPopupMenu;
- 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
- * TODO RightClick menus
- */
- 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.toString();
- }
- public String toCookString()
- {
- return iconList.toCookString();
- }
- private final ObjList iconList;
- private BufferedImage Map;
- private String mapName;
- private final MouseListener mouse;
- 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 p = new JPanel(new BorderLayout());
- public JPanel panel()
- {
- return p;
- }
- public ImageMapping()
- {
- iconList = new ObjList(this);
- mouse = new MouseListener(){
- public void mouseClicked(MouseEvent arg0)
- {
- System.out.println(arg0.getXOnScreen() + " y : " + arg0.getYOnScreen());
- }
- public void mouseEntered(MouseEvent arg0)
- {
- }
- public void mouseExited(MouseEvent arg0)
- {
- }
- public void mousePressed(MouseEvent arg0)
- {
- }
- public void mouseReleased(MouseEvent arg0)
- {
- }};
- this.addMouseListener(mouse);
- addMenu();
- p.add(this);
- p.setLayout(null);
- split.setLeftComponent(p);
- split.setRightComponent(new JPanel());
- frame.add(split);
- frame.setSize(800, 500);
- frame.setLocation(400,400);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public int[] mapLocation()
- {
- return new int[]{p.getX(),p.getY()};
- }
- public void setVisible(boolean arg)
- {
- frame.setVisible(arg);
- }
- 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, Map.getWidth(), Map.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)
- {
- 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)
- {
- iconList.add(new DotList(col,link));
- }
- else
- {
- iconList.add(new Dot(new int[]{77,77},col,link));
- }
- 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);
- }
- /**
- * 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();
- }
- /**
- * 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 abstract class Icon implements CookMePlox
- {
- 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 void remove(Icon i)
- {
- list.remove(i);
- }
- public String toString()
- {
- String returnString = "<imagemap>\n" +
- map.mapFileName() +
- "\ndesc none";
- for (Icon i : list)
- returnString += "\n" + i.toString();
- returnString += "\n</imagemap>";
- return returnString;
- }
- public String toCookString()
- {
- String returnString = "<div style=\"position:relative;\">\n" +
- "[[File:"+ map.mapFileName() + "]]";
- for (Icon i : list)
- returnString += "\n" + i.toCookString();
- returnString += "\n</div>";
- return returnString;
- }
- public void paint(Graphics g)
- {
- for (Icon a : list)
- a.paint(g);
- }
- }
- public class IconList extends Icon
- {
- private ArrayList<Icon> list = new ArrayList<Icon>();
- private String link;
- public String toString()
- {
- String returnString = "";
- for (Icon i : list)
- returnString += "\n" + i.toString();
- returnString = returnString.trim();
- return returnString;
- }
- public String toCookString()
- {
- String returnString = "";
- for (Icon i : list)
- 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);
- }
- }
- /**
- * 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[]{0,0}, this));
- }
- 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 createDot(MouseEvent click)
- {
- dots.add(new Dot(new int[]{0,0},this));
- }
- public void remove(Dot d)
- {
- dots.remove(d);
- }
- public String toString()
- {
- //Comment labeling what the upcoming list of dots are mapping
- String returnString = "# Dots for " + link;
- for(Dot d : dots)
- returnString += "\n" + d.toString();
- return returnString.trim();
- }
- public String toCookString()
- {
- String returnString = "";
- for(Dot d : dots)
- returnString += "\n" + d.toString();
- return returnString.trim();
- }
- public void paint(Graphics g)
- {
- for (Icon i : dots)
- i.paint(g);
- }
- }
- /**
- * The Dot class
- * This doc is kinda just here to delimit
- * So ya...
- */
- public class Dot extends Icon
- {
- private int[] location;
- private final DotList parent;
- private String link;
- private String color;
- private Circle dot;
- public Dot(int[] l, DotList p)
- {
- location = Arrays.copyOf(l, 2);
- parent = p;
- link = parent.link();
- color = parent.color();
- dot = new Circle(this);
- }
- public Dot(int[] l, String c, String w)
- {
- location = Arrays.copyOf(l, 2);
- parent = null;
- color = c;
- link = w;
- dot = new Circle(this);
- }
- /**
- * Returns the MediaWiki <imagemap> syntax for a circle at the given x then y values
- * and its appropriate link
- */
- public String toString()
- {
- 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)
- {
- setLocation(xy[0], xy[1]);
- }
- public void setLocation(int x, int y)
- {
- location[0] = x;
- location[1] = y;
- }
- public int[] locationOfCenter()
- {
- return location;
- }
- /**
- * Location of the top left corner;
- * 6 pixels away from center in each direction
- */
- public int[] locationOfEdge()
- {
- return new int[]{location[0]-4,location[1]-4};
- }
- public void removeSelf()
- {
- 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)
- {
- dot.paint(g);
- }
- }
- public class Circle extends Component
- {
- private static final long serialVersionUID = 4L;
- private int[] pos = new int[2];
- private int radius;
- private String color;
- private Dot pt;
- //private JPopupMenu opt = new JPopupMenu("Options");
- public Circle(Dot p)
- {
- pt = p;
- pos = p.locationOfEdge();
- radius = 4;
- setSize(8,8);
- mapper.panel().add(this, 0);
- setLoc(pos[0], pos[1]);
- color = p.color();
- addMouse();
- System.out.println(getBounds().x);
- System.out.println(getBounds().y);
- System.out.println(getBounds().width);
- System.out.println(getBounds().height);
- }
- public void setLoc(int[] xy)
- {
- setLoc(pos[1], pos[0]);
- }
- public void setLoc(int x, int y)
- {
- pt.setLocation(x, y);
- pos[0] = x;
- pos[1] = y;
- super.setBounds(x, y, 8, 8);
- System.out.println("x : " + x);
- System.out.println(y);
- System.out.println(this.getLocationOnScreen().x);
- System.out.println(this.getLocationOnScreen().y);
- System.out.println(pos[0] + " " + pos[1]);
- System.out.println(mapper.panel().getLocationOnScreen().x + " ");
- System.out.println(mapper.panel().getLocationOnScreen().y + " ");
- }
- public void paint(Graphics g)
- {
- g.setColor(getColor(color));
- g.fillOval(pos[0] - radius, pos[1] - radius, radius * 2, radius * 2);
- }
- public Dot getDot()
- {
- return pt;
- }
- private void addMouse()
- {
- addMouseMotionListener(new MouseMotionListener(){
- public void mouseDragged(MouseEvent arg0){
- System.out.println(pos[0] + " " + pos[1]);
- System.out.println(arg0.getXOnScreen() + " - " + mapper.panel().getLocationOnScreen().x + " ");
- System.out.println(arg0.getYOnScreen() + " - " + mapper.panel().getLocationOnScreen().y + " ");
- int xx = (arg0.getXOnScreen() - mapper.panel().getLocationOnScreen().x - radius) + 3;
- int yy = (arg0.getYOnScreen() - mapper.panel().getLocationOnScreen().y - radius) + 3;
- setLoc(xx, yy);
- repaint();
- }
- public void mouseMoved(MouseEvent e) {}
- });
- addMouseListener(new MouseListener(){
- public void mouseClicked(MouseEvent arg0) {
- }
- public void mouseEntered(MouseEvent arg0) {
- System.out.println("asas");
- }
- public void mouseExited(MouseEvent arg0) {
- }
- public void mousePressed(MouseEvent arg0) {
- }
- public void mouseReleased(MouseEvent arg0) {
- }});
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement