0

I want a Java swing program to draw non-overlapping n-sided polygon , circles and ellipse with the mouse.

The polygon should not add the last point and first point itself until user makes it.

Echilon
  • 10,064
  • 33
  • 131
  • 217
  • 2
    [What have you tried?](http://www.whathaveyoutried.com/) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 22 '12 at 07:40
  • Hey someone please answer my above questions. I have tried many links but it's not working – user1844273 Nov 22 '12 at 08:12
  • Sample Program to draw circle I am trying to add polygons functionality to it – user1844273 Nov 22 '12 at 08:33
  • 1
    @user1844273 `I have tried many links but it's not working`, again for better help sooner, post an [SSCCE](http://sscce.org/) – mKorbel Nov 22 '12 at 08:33
  • *"I have tried many links"* List the top 3 & explain why they failed to solve the problem. *"but it's not working"* What did you expect to happen? What happened instead? We cannot help without that information, and also the code you tried (an SSCCE). – Andrew Thompson Nov 22 '12 at 08:34
  • I want to post the code but it's saying you cannot save editsPlease help – user1844273 Nov 22 '12 at 08:48
  • *"The polygon should not add the last point and first point itself until user makes it"* Okay and just how are you expecting the user to do this?? – MadProgrammer Nov 22 '12 at 09:01
  • When the user does "single click" then a point should be added and then on the next "single click" a line should be drawn between second and fisrt point and then when it draw the third point the same thing happens. Now if the user joins the third(last) point with the first point and "double clicks it then our polygon should be completed. – user1844273 Nov 22 '12 at 09:06
  • *"it's saying you cannot save editsPlease help"* I can. Post the code at a text sharing place & link to it. If it is an SSCCE, I will edit it into the question. – Andrew Thompson Nov 22 '12 at 09:11
  • It's saying "Oops! Your edit couldn't be submitted because: Please add some context to explain the code sections (or check that you have not incorrectly formatted all of your question as code). – user1844273 Nov 22 '12 at 09:14

1 Answers1

1

This is a REALLY basic example

enter image description here

 public class DrawPolygon {

      public static void main(String[] args) {
           new DrawPolygon();
      }

      public DrawPolygon() {
           EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                     try {
                          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                     } catch (ClassNotFoundException ex) {
                     } catch (InstantiationException ex) {
                     } catch (IllegalAccessException ex) {
                     } catch (UnsupportedLookAndFeelException ex) {
                     }

                     JFrame frame = new JFrame("Test");
                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     frame.setLayout(new BorderLayout());
                     frame.add(new PolyPane());
                     frame.pack();
                     frame.setLocationRelativeTo(null);
                     frame.setVisible(true);
                }

           });
      }

      public static class PolyPane extends JPanel {

           private MouseHandler mouseHandler;
           private Path2D currentShape;
           private List<Path2D> lstPloys;
           private Point lastPoint;
           private Point currentPoint;

           public PolyPane() {
                lstPloys = new ArrayList<>();
           }

           @Override
           public Dimension getPreferredSize() {
                return new Dimension(200, 200);
           }

           @Override
           public void addNotify() {
                super.addNotify();
                addMouseListener(getMouseHandler());
                addMouseMotionListener(getMouseHandler());
           }

           @Override
           public void removeNotify() {
                removeMouseListener(getMouseHandler());
                removeMouseMotionListener(getMouseHandler());
                super.removeNotify();
           }

           public MouseHandler getMouseHandler() {
                if (mouseHandler == null) {
                     mouseHandler = new MouseHandler();
                }
                return mouseHandler;
           }

           @Override
           protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (lastPoint != null) {
                     g2d.setColor(Color.RED);
                     g2d.fillOval(lastPoint.x - 2, lastPoint.y - 2, 4, 4);
                }
                if (currentShape != null) {
                     g2d.setColor(Color.RED);
                     g2d.draw(currentShape);
                     if (lastPoint != null && currentPoint != null) {
                          System.out.println(lastPoint + " - " + currentPoint);
                          g2d.setColor(new Color(255, 0, 0, 64));
                          g2d.draw(new Line2D.Float(lastPoint, currentPoint));
                     }
                }
                g2d.setColor(Color.BLACK);
                for (Shape shape : lstPloys) {
                     g2d.draw(shape);
                }
                g2d.dispose();
           }

           public class MouseHandler extends MouseAdapter {

                @Override
                public void mouseClicked(MouseEvent e) {
                     if (e.getButton() == MouseEvent.BUTTON1) {
                          if (e.getClickCount() == 1) {
                               Point p = e.getPoint();
                               lastPoint = p;
                               if (currentShape == null) {
                                    currentShape = new Path2D.Float();
                                    currentShape.moveTo(p.x, p.y);
                               } else {
                                    currentShape.lineTo(p.x, p.y);
                               }
                               repaint();
                          } else if (e.getClickCount() == 2) {
                               currentShape.closePath();
                               lstPloys.add(currentShape);
                               currentShape = null;
                               lastPoint = null;
                               repaint();
                          }
                     }
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                     if (currentShape != null) {
                          currentPoint = e.getPoint();
                          repaint();
                     } else {
                          currentPoint = null;
                     }
                }

           }

      }

 }

You will want to the time to read through

As these will cover the basics of what you need to know to achieve what you are trying to do.

Updated

I've updated the example to include showing the line to the next point using a MosueMotionListener

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hi MadProgrammer Thankyou very much for your help.But this is not what I am looking for the lines are drawn when I click on the next point but i want lines to be start drawing as the mouse cursor moves from point a to pointb. Also please add circles drawing to iot and what about the overlapping part. Please help – user1844273 Nov 22 '12 at 10:06
  • You have the basics at hand. Trapping overlaps is a simple process of checking the shape list and using the `Shape` API for overlaps and discarding these that do overlap. As to the "real time" feedback, you will need to implementing the `mouseMoved` method of the `MouseHandler` and attaching a mouse motion listener to the `PolyPane`. Have a read of the links I supplied and try something. – MadProgrammer Nov 22 '12 at 10:25
  • Hi I have been trying from last week.I am not bale to post my code over here so that you can see what have I tried till now.So please have faith and see if you can provide some help – user1844273 Nov 22 '12 at 10:35
  • +1 The example cited [here](http://stackoverflow.com/a/11944233/230513) shows how to drag control points. – trashgod Nov 22 '12 at 12:05
  • I've updated the example to allow it to show the line for the next point in real time. A couple of hours reading through the [Java Swing Trail](http://docs.oracle.com/javase/tutorial/uiswing/) would have pointed you to what you needed to know. I'd seriously take the time to read through the [2D Graphics Trail](http://docs.oracle.com/javase/tutorial/2d/index.html), especially the [Working with Geometry](http://docs.oracle.com/javase/tutorial/2d/geometry/index.html) section as it will provide you with the information you need to meet your remaining requirements – MadProgrammer Nov 22 '12 at 19:39
  • Also MadProgrammer. Please don't mind.. There are few more points to discuss--- 1) If I double click on the last point of a polygon , it itself goes and joins the first point which I don't want. I want user to do it. 2) If I join the first and last point and don't do the double click then also I am able to drag my line but my requirement is that if I join first and last point then my polygon should be completed and then I have to draw a new one 3) A polygon must have 3 minimum points. 4) How to manage the overlapping. Please do help on these points I will be highly oblige to you. – user1844273 Nov 23 '12 at 06:20
  • Hi MadProgrammer.. Please tell me how can I share my exact code with you... I am unable to paste it over here. Please telll me – user1844273 Nov 23 '12 at 08:01