1

Following my previous post here , I changed the code to :

PolygonnerJframe.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 * 
 * @author X2
 *
 */
public class PolygonnerJframe
{
    public static void main (String[] args)
    {
     JFrame frame = new JFrame("Draw polygons");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setContentPane(new DrawingPanel());
     frame.pack();
     frame.setVisible(true);
 }
}

Now , this code results in : this

And I can't understand what causes this .

The changes that I've made are : when we're done with a polygon , its coordinates are saved in the arrayList of class Polygon , and each time that I create a new polygon , I take the previous polygons and draw them , while drawing a new polygon .

As you can see above , something went wrong with the drawing and I can't seem to find the problem .

I'd appreciate any help .

Thanks

EDIT:

After taking into consideration what @StanislavL said , I moved those lines to mouseClicked() , but this time I get a new screen each time that a new polygon is created without the "old" polygons .

Just a new polygon ... without the old ones

enter image description here

Community
  • 1
  • 1
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 1
    You didn't post the most important part - drawing code. BTW: Why can't you use standard Polygon class? You can get source from here http://java-sl.com/shapes.html where regular Polygons are drawn. – StanislavL Mar 27 '13 at 10:52
  • @StanislavL: Of course I did , in the first file (`PolygonnerJframe.java` ) , under the function `private void draw(Graphics g, Point p1, Point p2)` . This is HW , hence I can't use the Polygon class :) – JAN Mar 27 '13 at 10:53

2 Answers2

2

On each public void paintComponent(Graphics g) call you create a new polygon add add it to the polygons list.

        Polygon poly = new Polygon(this.edges);

        // add the polygon to the polygons array 
        this.polygons.add(poly);

Guess that should happen just one e.g. in the mouseClicked() processing

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • @StanislavL: It still doesn't work : I moved those lines into `mouseClicked()` overridden method , but this time the screen doesn't show anything except for the NEW polygon . Please see the edited post . – JAN Mar 27 '13 at 13:05
  • simple to disable, super.paintComponent(g);, (but be sure that not proper of ways), did you read comment by @trashgod – mKorbel Mar 27 '13 at 13:17
2

@Stas code works for me witout any add_ons (added basic stuff cried in IDE), did you meaning final result could be ???

enter image description here

.

EDIT

.

dirty hack is disable super.paintComponent(g);, but proper way should be only

  • to add all Objects to the array (see quite clear comment by @ trashgod)

  • create an BurreferImage as Backgroung Image (after Mouse Double_Click)

output by disable super.paintComponent(g);

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • No, I need to create multiple polygons , where the final Point is attached to the first Point. – JAN Mar 27 '13 at 11:08
  • is there some logics, how much points are valid for one Polygon, – mKorbel Mar 27 '13 at 11:11
  • Take for example a triangle , it has 3 points and 3 edges .So I take each edge (where it has 2 points , one for each end) and draw it with `drawline` . That the way I'm trying to work with ,but as you can see ,I got something else . – JAN Mar 27 '13 at 11:13
  • 1
    +1 In `GraphPanel` each node could be a `Polygon` or `List` could be the vertices of an editable `Polygon`. – trashgod Mar 27 '13 at 11:51
  • @trashgod: This is very nice ,but it still does not explain where did I go wrong with my code . – JAN Mar 27 '13 at 13:34
  • @ron: Not sure; you might look at [`LinePanel`](http://stackoverflow.com/a/5797965/230513) as an example of animating just the _last_ segment added. – trashgod Mar 27 '13 at 13:49
  • @trashgod: The thing is , that the locations where the mouse "was" are also counted , not just the locations where I hit "one click" with the mouse ... that's what I'm trying to solve . – JAN Mar 27 '13 at 19:34