-1

I am able to define,drag and remove the objects in JApplet. Please help me how to find the the final coordinates(x and y) of the objects position in JApplet and writing them to a text file. Please help me with this. Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;

This is my main class

public class MouseTest extends JApplet
{
    public void init()
    {
    EventQueue.invokeLater(new Runnable() {
        public void run()
        {
        MousePanel panel = new MousePanel();
        add(panel);
        }
    });
    }
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new mainThread());
   }
}

class mainThread implements Runnable
{
    public void run()
    {
    JPanel panel = new MousePanel();
    JFrame frame = new JFrame("MouseTest");
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,200);
    frame.setVisible(true);
    }
}

class MousePanel extends JPanel
{
   public MousePanel()
   {
      squares = new ArrayList<Rectangle2D>();
      current = null;

      addMouseListener(new MouseHandler());
      addMouseMotionListener(new MouseMotionHandler());
   }

   public void paintComponent(Graphics g)
   {
       super.paintComponent(g);

       Graphics2D g2 = (Graphics2D) g;

      // draw all squares
      for (Rectangle2D r : squares)
         g2.draw(r);
   }

   /**
    * Finds the first square containing a point.
    * @param p a point
    * @return the first square that contains p
    */
   public Rectangle2D find(Point2D p)
   {
      for (Rectangle2D r : squares)
      {
         if (r.contains(p)) return r;
      }
      return null;
   }

   /**
    * Adds a square to the collection.
    * @param p the center of the square
    */
   public void add(Point2D p)
   {
      double x = p.getX();
      double y = p.getY();

      current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH,
            SIDELENGTH);
      squares.add(current);
      repaint();
   }

   /**
    * Removes a square from the collection.
    * @param s the square to remove
    */
   public void remove(Rectangle2D s)
   {
      if (s == null) return;
      if (s == current) current = null;
      squares.remove(s);
      repaint();
   }

   private static final int SIDELENGTH = 10;
   private ArrayList<Rectangle2D> squares;
   private Rectangle2D current;

   // the square containing the mouse cursor

these are methods for defining and dragging and removing the object

   private class MouseHandler extends MouseAdapter
   {

      public void mousePressed(MouseEvent event)
      {
         // add a new square if the cursor isn't inside a square
         current = find(event.getPoint());
         if (current == null) add(event.getPoint());
      }    
      public void mouseClicked(MouseEvent event)
      {
         // remove the current square if double clicked
         current = find(event.getPoint());
         if (current != null && event.getClickCount() >= 2) remove(current);
      }
   }

   private class MouseMotionHandler implements MouseMotionListener
   {
      public void mouseMoved(MouseEvent event)
      {
         // set the mouse cursor to cross hairs if it is inside
         // a rectangle

         if (find(event.getPoint()) == null) setCursor(Cursor.getDefaultCursor());
         else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
      } 
      public void mouseDragged(MouseEvent event)
      {
         if (current != null)
         {
            int x = event.getX();
            int y = event.getY();

            // drag the current rectangle to center it at (x, y)
            current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
            repaint();
         }
      }
   }
}
Sai
  • 13
  • 5
  • 1
    You're asking your question as if we understand what you mean by `"to find the the final coordinates of the objects and writing them to a text file..."`. Please edit your question and fill in the missing pieces for us. – Hovercraft Full Of Eels Jul 20 '14 at 02:00
  • I am able to define,drag and remove the objects in JApplet. Please help me how to find the the final coordinates(x and y) of the objects position in JApplet and writing them to a text file. Please help me with this. Thanks in advance. – Sai Jul 20 '14 at 02:04
  • 1
    How is this question any clearer than your last [question](http://stackoverflow.com/questions/24806114/finding-coordinates-in-java-graphics2d). The reason that question wasn't answered is because people didn't understand the question. Same with this question. So what is your problem? 1) do you know how to use the `getLocation()` method of a component? 2) do you know how to write data to a file? 3) Do you know how to determine when the user has finished dragging a component? – camickr Jul 20 '14 at 02:05
  • Sorry about that.. My question is, i am able to define objects and able to drag them randomly in JApplet. After dragging them i want to find their position coordinates and write their position coordinates to a text file. Please help me with this. – Sai Jul 20 '14 at 02:08
  • So what don't you know how to do. You did not answer any of my questions. You just keep restating the requirement for the 3rd time. – camickr Jul 20 '14 at 02:14
  • @camickr Sorry about that.. My question is, i am able to define objects on a mouse click and able to drag them randomly and able to remove them by double clicking on the object. After dragging them randomly i want to put a button in JApplet which writes their final position coordinates to a text file. Please help me with this. – Sai Jul 20 '14 at 02:15
  • @camickr 1)i know how to get location() 2)i know how to write data to a file. 3)i dont know how to determine when the user has finished dragging a component? – Sai Jul 20 '14 at 02:17

1 Answers1

1

i dont know how to determine when the user has finished dragging a component?

Finally an actual requirement.

Override the mouseReleased() method of your MouseListener.

Then in this code you get the location and write the location to a file.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Seriously? You know how to implement the mousePressed() and mouseClicked() methods, what is so difficult about implementing the mouseReleased() method? You just said you know how to get the location and write it to a file, so what is the problem? – camickr Jul 20 '14 at 02:22
  • @user3849513: Oh come on -- for fark's sake, at least try. This is going beyond the bounds of ridiculous. – Hovercraft Full Of Eels Jul 20 '14 at 02:22
  • ok thank you i understand how to do that. But i need to write the position coordinates of all components at once after moving all component randomly by using a button. How to do that?. Thanks in advance – Sai Jul 20 '14 at 02:25
  • 1
    camickr, an unrelated comment but [this guy](http://stackoverflow.com/a/24846545/522444) is stealing one of your answers word for word. I've flagged the moderators, but you might want to do that as well. 1+ for this answer. – Hovercraft Full Of Eels Jul 20 '14 at 02:26
  • 1
    @HovercraftFullOfEels, thanks, and he didn't even do a good job, since that is not a appropriate answer for the given question :) – camickr Jul 20 '14 at 02:30
  • i want to write the position coordinates of all components at once after moving all component randomly by using a button. How to do that?. – Sai Jul 20 '14 at 02:30