0

This program is about creating components and moving them randomly in JApplet and removing the components if required. Every mouse click creates a new component. After creating component we can click on it and drag it where ever we want. Double clicking on the component removes it. I was successful in creating, moving and removing components.

For example i created three components on the JApplet by clicking three times randomly at different places on the JApplet.Please click here to view created components I moved them randomly around the screen. Please click here to view JApplet after randomly moving them around screen My goal is to write the final coordinates(x,y) of their position on the screen for all components created in to a text file by clicking a write button. I have no idea about mouseReleased method. Please help me with this. Thank you. Your help is very much appreciated.

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
    Start by choosing a development strategy. Either use a `JAppelt` OR a `JFrame`, you shouldn't be mixing them together like this. Second, applets have very tight security restrictions, one of which is the ability to write to the local file system, so you might want to reconsider that. Third, I see no button in your code, see [How to use buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [How to write an action listener](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html). – MadProgrammer Jul 20 '14 at 04:03
  • Forth, take a look at [Basic I/O](http://docs.oracle.com/javase/tutorial/essential/io/) – MadProgrammer Jul 20 '14 at 04:04
  • 1
    How does this differ from your [previous question](https://stackoverflow.com/questions/24846610/how-to-find-final-coordinatesx-y-of-the-objects-position-in-japplet-and-writ)? It seems quite similar, even though you added a bunch of stuff at the beginning – awksp Jul 20 '14 at 04:06
  • @MadProgrammer Leave about writing in to a file. Can you please tell me how to find coordinates of those components on mouse release. Thank you.t – Sai Jul 20 '14 at 04:07
  • @MadProgrammer can u please elaborate on that. Thank you. – Sai Jul 20 '14 at 04:11
  • [`Rectangle2D#getBounds`](http://docs.oracle.com/javase/7/docs/api/java/awt/geom/RectangularShape.html#getBounds()) – MadProgrammer Jul 20 '14 at 04:14

1 Answers1

0

Why do you keep changing the requirement???? See original question: How to find final coordinates(x & y) of the objects position in JApplet and write them to a text file

First you wanted to write out the location of the moved component after the drag was finished.

Then you wanted to write out the location of "all" components after the drag was finished.

Now you want to write out the location of all the components after a "button" is clicked.

I have no idea about mouseReleased method.

What does this have to do with anything? That was the suggestion for your requirement in your last question. Since your requirement has changed it is no longer needed.

My goal is to write the final coordinates(x,y) of their position on the screen for all components created in to a text file by clicking a write button.

Then you need to add an ActionListener to the button. You have already been given a link to the Swing tutorial on How to Write an ActionListener so I guess the question is what code should be included in the ActionListener?

In you last question you stated:

  1. I know how to get the location of a component
  2. I know how to write data to a file

So the only remaining problem is how to get all the components on the panel? To do this you can use the Container.getComponents() method on your panel containing all the dragged components. The basic code for your ActionListener would be something like:

for (Component component: panel.getComponents())
{
    Point location = component.getLocation();
    // write the location to the file
}

I'll let you add the code for opening/closing the file.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288