Advertisement
Guest User

ImageMapping GUI

a guest
Jun 10th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.87 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Cursor;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.TextArea;
  7. import java.awt.image.BufferedImage;
  8. import java.io.IOException;
  9. import javax.imageio.ImageIO;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17. import java.awt.BorderLayout;
  18. import java.net.*;
  19. import javax.swing.BorderFactory;
  20. import javax.swing.JButton;
  21. import javax.swing.JComboBox;
  22. import javax.swing.JLabel;
  23. import javax.swing.JMenu;
  24. import javax.swing.JMenuBar;
  25. import javax.swing.JMenuItem;
  26. import javax.swing.JSplitPane;
  27. import javax.swing.JTextField;
  28. import javax.swing.Timer;
  29. import javax.swing.event.MouseInputAdapter;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. /*
  33.  * TODO ToolBox
  34.  * TODO GUI
  35.  */
  36. public class ImageMapping extends JPanel
  37. {
  38.     private static final long serialVersionUID = 1337L/*ol*/;
  39.     public static final Color DARK_BLUE = new Color(0,0,50);
  40.     public static final Color DARK_GREEN = new Color(0,50,0);
  41.     public static final Color DARK_RED = new Color(50,0,0);
  42.     static ImageMapping mapper;
  43.    
  44.     public static void main(String[] args)
  45.     {
  46.         mapper = new ImageMapping();
  47.         mapper.setVisible(true);
  48.         final Timer timer = new Timer(30, new ActionListener(){
  49.  
  50.             public void actionPerformed(ActionEvent e) {
  51.                 mapper.repaint();
  52.             }});
  53.         timer.start();
  54.     }
  55.    
  56.     public String toString()
  57.     {
  58.         return iconList.toMapString();
  59.     }
  60.    
  61.     public String toCookString()
  62.     {
  63.         return iconList.toCookString();
  64.     }
  65.    
  66.     private final ObjList iconList;
  67.     private BufferedImage Map;
  68.     private String mapName;
  69.     private Icon using = null;
  70.     private DotList usingList = null;
  71.     private final JFrame frame = new JFrame("Image Mapping Tool");
  72.     private final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  73.    
  74.     public static BufferedImage COMPASS;{
  75.     try {
  76.         COMPASS = ImageIO.read(new URL("http://urbancowgurl777.wikia.com/wiki/Special:FilePath/Compass.png"));
  77.     } catch (MalformedURLException e) {e.printStackTrace();}
  78.       catch (IOException e) {e.printStackTrace();}
  79.     }
  80.    
  81.     public static BufferedImage TITLEBOX;{
  82.     try {
  83.         TITLEBOX = ImageIO.read(new URL("http://urbancowgurl777.wikia.com/wiki/Special:FilePath/TitleBackground.png"));
  84.     } catch (MalformedURLException e) {e.printStackTrace();}
  85.       catch (IOException e) {e.printStackTrace();}
  86.     }
  87.  
  88.     private JPanel mapPanel = new JPanel(new BorderLayout());
  89.     private JPanel toolBox = new JPanel(new BorderLayout());
  90.    
  91.     private JComboBox dotListChooser = new JComboBox();
  92.    
  93.     public ImageMapping()
  94.     {
  95.         iconList = new ObjList(this);
  96.         addMouse();
  97.         addMenu();
  98.         toolBoxSetUp();
  99.         mapPanel.add(this);
  100.         mapPanel.setLayout(null);
  101.         mapPanel.setMinimumSize(new Dimension(400,300));
  102.         split.setLeftComponent(mapPanel);
  103.         split.setRightComponent(toolBox);
  104.         frame.add(split);
  105.         frame.setSize(800, 500);
  106.         frame.setLocation(400,400);
  107.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  108.     }
  109.    
  110.     private void toolBoxSetUp()
  111.     {
  112.         dotListChooser.addItem("Select a dot");
  113.         JPanel wrapMid = new JPanel(new BorderLayout());
  114.         JPanel wrapMid2 = new JPanel(new BorderLayout());
  115.         toolBox.add(wrapMid,BorderLayout.EAST);
  116.         wrapMid.add(wrapMid2,BorderLayout.NORTH);
  117.         wrapMid2.add(dotListChooser, BorderLayout.NORTH);
  118.         JButton makeList = new JButton("New Dot");
  119.         makeList.addActionListener(new ActionListener(){
  120.  
  121.             public void actionPerformed(ActionEvent arg0) {
  122.                 Object sel = dotListChooser.getSelectedItem();
  123.                 if (!(sel instanceof DotList))
  124.                     return;
  125.                 else if (usingList == sel)
  126.                     return;
  127.                 else
  128.                 {
  129.                     if (usingList != null)
  130.                         usingList.removeLast();
  131.                     usingList = (DotList) sel;
  132.                     usingList.add();
  133.                     using = usingList.lastDot();
  134.                     ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  135.                 }
  136.             }});
  137.         wrapMid2.add(makeList, BorderLayout.WEST);
  138.        
  139.         JButton doneList = new JButton("Done with");
  140.         doneList.addActionListener(new ActionListener(){
  141.  
  142.             public void actionPerformed(ActionEvent arg0) {
  143.                 if (usingList != null){
  144.                     usingList.removeLast();
  145.                     usingList = null;
  146.                     using = null;
  147.                     ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  148.                 }
  149.             }});
  150.         wrapMid2.add(doneList, BorderLayout.EAST);
  151.        
  152.         JButton rmvList = new JButton("Remove this list");
  153.         rmvList.addActionListener(new ActionListener(){
  154.  
  155.             public void actionPerformed(ActionEvent arg0) {
  156.                 Object sel = dotListChooser.getSelectedItem();
  157.                 if (!(sel instanceof DotList))
  158.                     return;
  159.                 dotListChooser.removeItem(sel);
  160.                 iconList.remove((Icon) sel);
  161.                 if (usingList == sel){
  162.                     usingList = null;
  163.                     using = null;
  164.                     ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  165.                 }
  166.             }});
  167.         wrapMid2.add(rmvList, BorderLayout.SOUTH);
  168.     }
  169.    
  170.     public int[] mapLocation()
  171.     {
  172.         return new int[]{mapPanel.getX(),mapPanel.getY()};
  173.     }
  174.    
  175.     public void setVisible(boolean arg)
  176.     {
  177.         frame.setVisible(arg);
  178.     }
  179.    
  180.     public JPanel panel()
  181.     {
  182.         return mapPanel;
  183.     }
  184.    
  185.     public void addMouse()
  186.     {
  187.         addMouseListener(new MouseListener(){
  188.  
  189.             public void mouseClicked(MouseEvent arg0)
  190.             {
  191.                 if (using != null)
  192.                 {
  193.                     Icon ic = using;
  194.                     if (ic instanceof Dot)
  195.                     {
  196.                         ((Dot) ic).setLocation(arg0.getX(), arg0.getY());
  197.                         if (usingList == null)
  198.                         {
  199.                             ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  200.                             using = null;
  201.                         }
  202.                         else
  203.                         {
  204.                             usingList.add();
  205.                             using = usingList.lastDot();
  206.                         }
  207.                         return;
  208.                     }
  209.                 }
  210.             }
  211.  
  212.             public void mouseEntered(MouseEvent arg0)
  213.             {
  214.             }
  215.  
  216.             public void mouseExited(MouseEvent arg0)
  217.             {
  218.                
  219.             }
  220.  
  221.             public void mousePressed(MouseEvent arg0)
  222.             {
  223.                
  224.             }
  225.  
  226.             public void mouseReleased(MouseEvent arg0)
  227.             {
  228.                
  229.             }});
  230.     }
  231.     public void addMenu()
  232.     {
  233.         JMenuBar menu = new JMenuBar();
  234.         frame.setJMenuBar(menu);
  235.         JMenu info = new JMenu("Info");
  236.         JMenu add = new JMenu("Add icon");
  237.        
  238.         JMenuItem newDot = new JMenuItem("New dot");
  239.         JMenuItem newDotList = new JMenuItem("New set of dots");
  240.  
  241.         JMenuItem newImage = new JMenuItem("New Map");
  242.         newImage.addActionListener(new ActionListener(){
  243.  
  244.             public void actionPerformed(ActionEvent arg0) {
  245.                     dialog("Enter the exact filename", 'M', ImageMapping.this);
  246.             }});
  247.        
  248.         newDot.addActionListener(new ActionListener(){
  249.  
  250.             public void actionPerformed(ActionEvent arg0) {
  251.                     newDot(false);
  252.             }});
  253.        
  254.         newDotList.addActionListener(new ActionListener(){
  255.  
  256.             public void actionPerformed(ActionEvent arg0) {
  257.                     newDot(true);
  258.             }});
  259.        
  260.         info.add(newImage);
  261.         add.add(newDot);
  262.         add.add(newDotList);
  263.         menu.add(info);
  264.         menu.add(add);
  265.        
  266.         JMenu syntax = new JMenu("Print Syntax");
  267.         JMenuItem cook = new JMenuItem("Cook Syntax");
  268.         cook.addActionListener(new ActionListener(){
  269.  
  270.             public void actionPerformed(ActionEvent arg0) {
  271.                     syntax(true);
  272.             }});
  273.        
  274.         JMenuItem standard = new JMenuItem("Standard");
  275.         standard.addActionListener(new ActionListener(){
  276.  
  277.             public void actionPerformed(ActionEvent arg0) {
  278.                     syntax(false);
  279.             }});
  280.        
  281.         syntax.add(cook);
  282.         syntax.add(standard);
  283.         menu.add(syntax);
  284.     }
  285.    
  286.     public void paint(Graphics g)
  287.     {
  288.         g.clearRect(0, 0, getWidth(), getHeight());
  289.         if (Map != null)
  290.             g.drawImage(Map,0,0,null);
  291.         iconList.paint(g);
  292.     }
  293.    
  294.     public void setMapName(String file)
  295.     {
  296.         file = file.replace(' ', '_');
  297.         file = file.replace(".png", "");
  298.         file = file.replaceFirst("[Ff]ile:", "");
  299.         mapName = file;
  300.         setMapFile();
  301.     }
  302.    
  303.     public void setMapFile()
  304.     {
  305.         try {
  306.             Map = ImageIO.read(new URL("http://runescape.wikia.com/special:FilePath/"+mapName+".png"));
  307.             super.setBounds(0, 0, Map.getWidth() + 150, Map.getHeight() + 150);
  308.         } catch (MalformedURLException e) {
  309.             warning("The file name entered was bad");
  310.         } catch (IOException e) {
  311.             warning("Failed to fetch the desired file");
  312.         }
  313.         repaint();
  314.     }
  315.    
  316.     public String mapFileName()
  317.     {
  318.         return mapName + ".png";
  319.     }
  320.     /**
  321.      * Color chooser so I don't have to recreate lulz
  322.      */
  323.     public JComboBox colors()
  324.     {
  325.         String[] cols = {"Black","Blue","Cyan","Dark Blue","Dark Green",
  326.                 "Dark Gray","Dark Red","Gray","Green","Light Gray",
  327.                 "Magenta","Orange","Pink","Red","White","Yellow"
  328.                 };
  329.         JComboBox colors = new JComboBox(cols);
  330.         return colors;
  331.     }
  332.    
  333.     /**
  334.      * Create a new dot or dot list
  335.      * @param list - true for list, false for dot
  336.      */
  337.     public void newDot(final boolean list)
  338.     {
  339.         usingList = null;
  340.         if (!list && using != null)
  341.         {
  342.             warning("Please place your last dot first");
  343.             return;
  344.         }
  345.         String species = (list) ? "dot list" : "dot";
  346.         final JFrame window = new JFrame("New " + species);
  347.         window.setLocation(300,300);
  348.         window.setSize(200,150);
  349.         window.setResizable(false);
  350.         final JComboBox colors = colors();
  351.         JPanel wrapLink = new JPanel(new BorderLayout());
  352.         JPanel wrap = new JPanel(new BorderLayout());
  353.         final JTextField inputLink = new JTextField();
  354.         wrapLink.add(new JLabel("Page Link"), BorderLayout.NORTH);
  355.         wrapLink.add(inputLink, BorderLayout.SOUTH);
  356.         wrap.add(wrapLink, BorderLayout.SOUTH);
  357.         wrap.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
  358.         window.add(wrap,BorderLayout.NORTH);
  359.         JPanel colorWrap = new JPanel(new BorderLayout());
  360.         JPanel midWrap = new JPanel(new BorderLayout());
  361.         colorWrap.add(colors,BorderLayout.NORTH);
  362.         final JButton colo = new JButton("   ");
  363.         colo.setEnabled(false);
  364.         colorWrap.add(colo,BorderLayout.SOUTH);
  365.         midWrap.add(colorWrap,BorderLayout.NORTH);
  366.         JButton ok = new JButton("Make " + species);
  367.         colors.addActionListener(new ActionListener(){
  368.  
  369.             public void actionPerformed(ActionEvent arg0) {
  370.                 colo.setBackground(getColor((String)colors.getSelectedItem()));        
  371.             }});
  372.         ok.addActionListener(new ActionListener(){
  373.  
  374.             public void actionPerformed(ActionEvent arg0) {
  375.                 String col = (String)colors.getSelectedItem();
  376.                 String link = inputLink.getText();
  377.                 if (link.equals("") || link == null)
  378.                 {
  379.                     warning("No link was entered");
  380.                     return;
  381.                 }
  382.                 else if (col.equals("") || col == null)
  383.                 {
  384.                     warning("No color selected");
  385.                     return;
  386.                 }
  387.                 if (list)
  388.                 {
  389.                     DotList dotList = new DotList(link,col);
  390.                     iconList.add(dotList);
  391.                     dotListChooser.addItem(dotList);
  392.                 }
  393.                 else
  394.                 {
  395.                     Dot dot = new Dot(new int[]{-10,-10},col,link,iconList);
  396.                     using = dot;
  397.                     iconList.add(dot);
  398.                     ImageMapping.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  399.                 }
  400.                 ImageMapping.this.frame.validate();
  401.                 ImageMapping.this.frame.repaint();
  402.                 window.setVisible(false);
  403.             }});
  404.         window.add(midWrap);
  405.         midWrap.add(ok, BorderLayout.SOUTH);
  406.         window.setVisible(true);
  407.     }
  408.    
  409.     /**
  410.      * Create a new window with the syntax for image mapping
  411.      * @param CookMePlox - use Cook's syntax or not
  412.      */
  413.     public void syntax(boolean CookMePlox)
  414.     {
  415.         JFrame frame = new JFrame("Template Syntax");
  416.         TextArea syntax = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
  417.         frame.setLocation(100,100);
  418.         frame.setSize(300,500);
  419.         syntax.setEditable(false);
  420.         String text = (CookMePlox) ? toCookString() : toString();
  421.         syntax.setText(text);
  422.         frame.add(syntax);
  423.         frame.setVisible(true);
  424.     }
  425.    
  426.     public boolean locationInBounds(int[] xy)
  427.     {
  428.         return locationInBounds(xy[0], xy[1]);
  429.     }
  430.    
  431.     public boolean locationInBounds(int x, int y)
  432.     {
  433.         if (x < 0 || x > Map.getWidth() || y < 0 || y > Map.getHeight())
  434.             return false;
  435.         else
  436.             return true;
  437.     }
  438.     /**
  439.      * Input a string with a method and then change a string in
  440.      * the passed ImageMapping; the string to change will be
  441.      * identified by a token char
  442.      *
  443.      * Had to implement it this way because apparently
  444.      * <tt>JOptionPane.showInputDialog()</tt> uses <tt>next()</tt>
  445.      * instead of <tt>nextLine</tt> or some other. And forcing
  446.      * underscores instead of spaces isn't very userfriendly
  447.      * now is it?
  448.      * @param message - Message to give to the user
  449.      * @param name - token
  450.      * @param map - ImageMapper
  451.      *
  452.      * TODO Or heck, a showConfirmDialogue and add a text field component to that dialog.
  453.      */
  454.     public static void dialog(String message, final char name, final ImageMapping map)
  455.     {
  456.         final JFrame frame = new JFrame("Input");
  457.         frame.setSize(200,100);
  458.         frame.setLocation(300,300);
  459.         final JTextField input = new JTextField();
  460.         input.setSize(120, 15);
  461.         JPanel wrap = new JPanel(new BorderLayout());
  462.         wrap.add(input,BorderLayout.NORTH);
  463.         JButton ok = new JButton("OK");
  464.         frame.add(new JLabel(message), BorderLayout.NORTH);
  465.         frame.add(wrap);
  466.         frame.add(ok, BorderLayout.SOUTH);
  467.         ok.addActionListener(new ActionListener(){
  468.  
  469.             public void actionPerformed(ActionEvent arg0) {
  470.                  String str = input.getText();
  471.                  frame.setVisible(false);
  472.                  setString(str, name, map);
  473.             }});
  474.         frame.setVisible(true);
  475.     }
  476.    
  477.     private static void setString(String text, char name, ImageMapping map)
  478.     {
  479.         switch(name){
  480.         case 'M' :
  481.         case 'm' :
  482.             map.setMapName(text);
  483.             break;
  484.         }
  485.     }
  486.    
  487.     public static void warning(String message)
  488.     {
  489.         final JFrame frame = new JFrame("Error");
  490.         JTextField warning = new JTextField(message);
  491.         warning.setEditable(false);
  492.         frame.setSize(200,100);
  493.         frame.setLocation(300,300);
  494.         JButton ok = new JButton("OK");
  495.         frame.add(warning);
  496.         frame.add(ok, BorderLayout.SOUTH);
  497.         ok.addActionListener(new ActionListener(){
  498.  
  499.             public void actionPerformed(ActionEvent arg0) {
  500.                  frame.setVisible(false);
  501.             }});
  502.         frame.setVisible(true);
  503.     }
  504.    
  505.     public static Color getColor(String c)
  506.     {
  507.         Color ret = Color.WHITE;
  508.         c = c.replace(" ", "");
  509.         c = c.replace("_", "");
  510.         if (c.equalsIgnoreCase("BLACK"))
  511.             ret = Color.BLACK;
  512.         else if (c.equalsIgnoreCase("BLUE"))
  513.             ret = Color.BLUE;
  514.         else if (c.equalsIgnoreCase("CYAN"))
  515.             ret = Color.CYAN;
  516.         else if (c.equalsIgnoreCase("DARKBLUE"))
  517.             ret = DARK_BLUE;
  518.         else if (c.equalsIgnoreCase("DARKGRAY"))
  519.             ret = Color.DARK_GRAY;
  520.         else if (c.equalsIgnoreCase("DARKGREEN"))
  521.             ret = DARK_GREEN;
  522.         else if (c.equalsIgnoreCase("DARKRED"))
  523.             ret = DARK_RED;
  524.         else if (c.equalsIgnoreCase("GRAY"))
  525.             ret = Color.GRAY;
  526.         else if (c.equalsIgnoreCase("GREEN"))
  527.             ret = Color.GREEN;
  528.         else if (c.equalsIgnoreCase("LIGHTGRAY"))
  529.             ret = Color.LIGHT_GRAY;
  530.         else if (c.equalsIgnoreCase("MAGENTA"))
  531.             ret = Color.MAGENTA;
  532.         else if (c.equalsIgnoreCase("ORANGE"))
  533.             ret = Color.ORANGE;
  534.         else if (c.equalsIgnoreCase("PINK"))
  535.             ret = Color.PINK;
  536.         else if (c.equalsIgnoreCase("RED"))
  537.             ret = Color.RED;
  538.         else if (c.equalsIgnoreCase("WHITE"))
  539.             ret = Color.WHITE;
  540.         else if (c.equalsIgnoreCase("YELLOW"))
  541.             ret = Color.YELLOW;
  542.         return ret;
  543.     }
  544.     /*
  545.      * All the stupid classes are appearing under this comment
  546.      * This comment is just a delimiter for stuff
  547.      * for me
  548.      * so
  549.      * ignore
  550.      * it
  551.      * Please
  552.      * Go away
  553.      */
  554.     /**
  555.      * This interface forces classes to implement a second species
  556.      * of the <tt>toString()</tt> method which prints the imagemapping code
  557.      * using Cook's imagemapping templates
  558.      */
  559.     public interface CookMePlox
  560.     {
  561.         public String toCookString();
  562.     }
  563.    
  564.     public interface ImageMaps
  565.     {
  566.         public String toMapString();
  567.     }
  568.     /**
  569.      * The Icon class
  570.      * Icons are either a specific shape or a list of Shapes
  571.      * This doc is kinda just here to delimit
  572.      * So ya...
  573.      */
  574.     public interface Icon extends CookMePlox, ImageMaps
  575.     {
  576.         public abstract String link();
  577.         public abstract void setLink(String s);
  578.         public abstract void paint(Graphics g);
  579.     }
  580.    
  581.     public class ObjList implements CookMePlox
  582.     {
  583.         private ArrayList<Icon> list = new ArrayList<Icon>();
  584.         private final ImageMapping map;
  585.        
  586.         public ObjList(ImageMapping mapName)
  587.         {
  588.             map = mapName;
  589.         }
  590.        
  591.         public void add(Icon i)
  592.         {
  593.             list.add(i);
  594.         }
  595.         public Icon lastIcon()
  596.         {
  597.             if (list.size() > 0)
  598.                 return list.get(list.size() - 1);
  599.             else
  600.                 return null;
  601.         }
  602.         public void remove(Icon i)
  603.         {
  604.             list.remove(i);
  605.         }
  606.        
  607.         public String toMapString()
  608.         {
  609.             String returnString = "<imagemap>\n" +
  610.                     map.mapFileName() +
  611.                     "\ndesc none";
  612.             for (Icon i : list)
  613.                 if (checkBounds(i))
  614.                     returnString += "\n" + i.toMapString();
  615.             returnString += "\n</imagemap>";
  616.             return returnString;
  617.         }
  618.        
  619.         public String toCookString()
  620.         {
  621.             String returnString = "<div style=\"position:relative;\">\n" +
  622.                     "[[File:"+ map.mapFileName() + "]]";
  623.             for (Icon i : list)
  624.                 if (checkBounds(i))
  625.                     returnString += "\n" + i.toCookString();
  626.             returnString += "\n</div>";
  627.             return returnString;
  628.         }
  629.        
  630.         public void paint(Graphics g)
  631.         {
  632.             for (Icon a : list)
  633.                 a.paint(g);
  634.         }
  635.        
  636.         public boolean checkBounds(Icon i)
  637.         {
  638.             if (i instanceof Dot)
  639.                 return (mapper.locationInBounds(((Dot) i).locationOfEdge()));
  640.             else if (i instanceof IconList)
  641.                 return true;
  642.             else
  643.                 return false;
  644.         }
  645.     }
  646.    
  647.     public class IconList implements Icon
  648.     {
  649.         private ArrayList<Icon> list = new ArrayList<Icon>();
  650.         private String link;
  651.        
  652.         public String toString()
  653.         {
  654.             return list.size() + " elements";
  655.         }
  656.         public String toMapString()
  657.         {
  658.             String returnString = "";
  659.             for (Icon i : list)
  660.                 if (inBounds(i))
  661.                     returnString += "\n" + i.toMapString();
  662.             returnString = returnString.trim();
  663.             return returnString;
  664.         }
  665.        
  666.         public String toCookString()
  667.         {
  668.             String returnString = "";
  669.             for (Icon i : list)
  670.                 if (inBounds(i))
  671.                     returnString += "\n" + i.toCookString();
  672.             returnString = returnString.trim();
  673.             return returnString;
  674.         }
  675.        
  676.         public void remove(Icon arg)
  677.         {
  678.             list.remove(arg);
  679.         }
  680.  
  681.         public String link()
  682.         {
  683.             return link;
  684.         }
  685.  
  686.         public void setLink(String s)
  687.         {
  688.             link = s;
  689.         }
  690.  
  691.         public void paint(Graphics g)
  692.         {
  693.             for (Icon i : list)
  694.                 i.paint(g);
  695.         }
  696.        
  697.         public boolean inBounds(Icon i)
  698.         {
  699.             if (i instanceof Dot)
  700.                 return (mapper.locationInBounds(((Dot) i).locationOfEdge()));
  701.             else if (i instanceof IconList)
  702.                 return true;
  703.             else
  704.                 return false;
  705.         }
  706.     }
  707.     /**
  708.      * List for Dots, will create multiple image dots with the same
  709.      * color and same page to which they link
  710.      */
  711.     public class DotList extends IconList
  712.     {
  713.         private String link = "Test";
  714.         private String color;
  715.         private final ArrayList<Dot> dots = new ArrayList<Dot>();
  716.        
  717.         public DotList(String lnk, String col)
  718.         {
  719.             link = lnk;
  720.             color = col;
  721.         }
  722.         public void add()
  723.         {
  724.             dots.add(new Dot(new int[]{-10,-10}, this));
  725.         }
  726.        
  727.         public Dot lastDot()
  728.         {
  729.             if (dots.size() == 0)
  730.                 return null;
  731.             return dots.get(dots.size()-1);
  732.         }
  733.         public String link()
  734.         {
  735.             return link;
  736.         }
  737.        
  738.         public void setLink(String l)
  739.         {
  740.             for(Dot d : dots)
  741.                 d.setLink(l);
  742.         }
  743.        
  744.         public String color()
  745.         {
  746.             return color;
  747.         }
  748.        
  749.         public void setColor(String c)
  750.         {
  751.             for(Dot d : dots)
  752.                 d.setColor(c);
  753.         }
  754.        
  755.         public void remove(Dot d)
  756.         {
  757.             dots.remove(d);
  758.         }
  759.        
  760.         public void removeLast()
  761.         {
  762.             if (dots.size() == 0)
  763.                 return;
  764.             dots.remove(lastDot());
  765.         }
  766.        
  767.         public String toString()
  768.         {
  769.             return link + "; " + color;
  770.         }
  771.         public String toMapString()
  772.         {
  773.             //Comment labeling what the upcoming list of dots are mapping
  774.             String returnString = "# Dots for " + link;
  775.             for(Dot d : dots)
  776.                 if (inBounds(d))
  777.                     returnString += "\n" + d.toMapString();
  778.             return returnString.trim();
  779.         }
  780.        
  781.         public String toCookString()
  782.         {
  783.             String returnString = "";
  784.             for(Dot d : dots)
  785.                 if (inBounds(d))
  786.                     returnString += "\n" + d.toCookString();
  787.             return returnString.trim();
  788.         }
  789.        
  790.         public void paint(Graphics g)
  791.         {
  792.             for (Icon i : dots)
  793.                 i.paint(g);
  794.         }
  795.        
  796.         public boolean inBounds(Dot i)
  797.         {
  798.             return (mapper.locationInBounds(i.locationOfEdge()));
  799.         }
  800.     }
  801.    
  802.     /**
  803.      * The Dot class
  804.      * This doc is kinda just here to delimit
  805.      * So ya...
  806.      */
  807.     public class Dot extends Component implements Icon
  808.     {
  809.         private static final long serialVersionUID = 1L;
  810.         private int[] location = {-10,-10};
  811.         private final Object parent;
  812.         private String link;
  813.         private String color;
  814.         public static final int RADIUS = 4;
  815.         public Dot(int[] l, DotList p)
  816.         {
  817.             location = Arrays.copyOf(l, 2);
  818.             parent = p;
  819.             link = ((DotList) parent).link();
  820.             color = ((DotList) parent).color();
  821.             mapper.panel().add(this, 0);
  822.             setLocation(l);
  823.             addMouse();
  824.         }
  825.        
  826.         public Dot(int[] l, String c, String w, ObjList p)
  827.         {
  828.             location = Arrays.copyOf(l, 2);
  829.             parent = p;
  830.             color = c;
  831.             link = w;
  832.             mapper.panel().add(this, 0);
  833.             addMouse();
  834.         }
  835.        
  836.         public String toString()
  837.         {
  838.             return color + "dot; " + link + "; at " + location[0] + ", " + location[1];
  839.         }
  840.         /**
  841.          * Returns the MediaWiki &lt;imagemap> syntax for a circle at the given x then y values
  842.          * and its appropriate link
  843.          */
  844.         public String toMapString()
  845.         {
  846.             return "circle "+locationOfEdge()[0]+" "+locationOfEdge()[1]+" 8 [["+link()+"]]";
  847.         }
  848.        
  849.         public String toCookString()
  850.         {
  851.             return "{{Dot|"+locationOfEdge()[0]+"|"+locationOfEdge()[1]+"|"+
  852.                     color + "|" + link + "}}";
  853.         }
  854.        
  855.         public String link()
  856.         {
  857.             return link;
  858.         }
  859.        
  860.         public void setLocation(int[] xy)
  861.         {
  862.             location = xy;
  863.         }
  864.        
  865.         public void setLocation(int x, int y)
  866.         {
  867.             location[0] = x;
  868.             location[1] = y;
  869.             super.setBounds(x, y, RADIUS * 2, RADIUS * 2);
  870.         }
  871.        
  872.        
  873.         public int[] locationOfCenter()
  874.         {
  875.             return new int[]{location[0] + RADIUS, location[1] + RADIUS};
  876.         }
  877.        
  878.         /**
  879.          * Location of the top left corner;
  880.          * 6 pixels away from center in each direction
  881.          */
  882.         public int[] locationOfEdge()
  883.         {
  884.             return location;
  885.         }
  886.        
  887.         public void removeSelf()
  888.         {
  889.             if (parent instanceof DotList)
  890.                 ((DotList) parent).remove(this);
  891.             else if (parent instanceof ObjList)
  892.                 ((ObjList) parent).remove(this);
  893.         }
  894.        
  895.         public void setLink(String l)
  896.         {
  897.             link = l;
  898.         }
  899.        
  900.         public void setColor(String c)
  901.         {
  902.             color = c;
  903.         }
  904.  
  905.         public String color()
  906.         {
  907.             return color;
  908.         }
  909.        
  910.         public void paint(Graphics g)
  911.         {
  912.             g.setColor(getColor(color));
  913.             g.fillOval(location[0], location[1], RADIUS * 2, RADIUS * 2);
  914.         }
  915.        
  916.         private void addMouse()
  917.         {
  918.             addMouseMotionListener(new MouseMotionListener(){
  919.                
  920.                 public void mouseDragged(MouseEvent arg0){
  921.                     int xx = (arg0.getXOnScreen() - mapper.panel().getLocationOnScreen().x - RADIUS);
  922.                     int yy = (arg0.getYOnScreen() - mapper.panel().getLocationOnScreen().y - RADIUS);
  923.                     setLocation(xx, yy);
  924.                     repaint();
  925.                 }
  926.                 public void mouseMoved(MouseEvent e) {}
  927.             });
  928.            
  929.             addMouseListener(new MouseInputAdapter(){
  930.  
  931.                 public void mouseClicked(MouseEvent arg0) {
  932.                     if (arg0.getButton() == MouseEvent.BUTTON1 && arg0.isControlDown())
  933.                     {
  934.                         removeSelf();
  935.                     }
  936.                 }
  937.             });
  938.         }
  939.     }
  940. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement