Advertisement
Guest User

ImageMapping tool

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