Repainting an applet from a swingworker used to calculate triangles and circles

I am trying to reproduce the applet found here as part of an exercise. The applet uses the Fortune algorithm to generate both; Voronoi diagram and Delaunay triangulation. I'm just interested in generating the Delaunay triangulation in the plane and, therefore, will use incremental algorithms, that is, add 1 point at a time. I intend to show that triangles are generated at every stage when a sample point is added.

I use the SwingWorker class to create an instance of the Triangulate class that contains the algorithm. I call the triangulation method inside the for loop, which iterates through a set of sample points when the start button in the GUI is pressed.

Here is the code for this:

JButton startButton = new JButton("Start");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                SwingWorker<List<Triangle>, Triangle> worker = new SwingWorker<List<Triangle>, Triangle>() {
                    @Override
                    protected List<Triangle> doInBackground() throws Exception {
                        Triangulate dt = new Triangulate(drawingPanel.pointsList());
                        dt.preTriangulate(); //Set-up a bounding triangle and obtain a random permutation of the points
                        List<PlanarPoint> pointsList = dt.pointsList();
                        for (int i = 0; i < pointsList.size(); i++) {
                            PlanarPoint sample = pointsList.get(i);
                            dt.triangulate(sample); 
                            List<Triangle> list = dt.trianglesList(); //Obtaining the list of triangles at every stage. Good Idea??
                            for (int j = 0; j < list.size(); j++) {
                                publish(list.get(j));
                            }
                            Thread.sleep(500);
                        }
                        dt.removeTriangles(dt.trianglesList()); // Remove all the triangles containing bounding-triangle vertices
                        return dt.trianglesList();
                    }

                    protected void process(List<Triangle> triangles) {
                        for (Triangle triangle : triangles) {
                            g = drawingPanel.getGraphics();
                            PlanarPoint p1 = triangle.getVertex1();
                            PlanarPoint p2 = triangle.getVertex2();
                            PlanarPoint p3 = triangle.getVertex3();
                            g.drawLine((int) Math.ceil(p1.x), (int) Math.ceil(p1.y),
                                    (int) Math.ceil(p2.x), (int) Math.ceil(p2.y));
                            g.drawLine((int) Math.ceil(p2.x),(int) Math.ceil(p2.y),
                                    (int) Math.ceil(p3.x),(int) Math.ceil(p3.y));
                            g.drawLine((int) Math.ceil(p3.x),(int) Math.ceil(p3.y),
                                    (int) Math.ceil(p1.x),(int) Math.ceil(p1.y));
                        }
                    }
                };
                worker.execute();
            }
        });

Here is a triangulation class that computes a Delanuay triangular chain of multiple points:

public class Triangulate {

    private List<PlanarPoint> pointsList;
    private List<Triangle> triangleList;
    private Triangle boundingTriangle;
    private List<Edge> edgeList;

    public Triangulate(List<PlanarPoint> pointsList) {
        this.pointsList = pointsList;
        this.triangleList = new ArrayList<Triangle>();
        this.edgeList = new ArrayList<Edge>();
    }

    public List<Triangle> trianglesList() {
        return triangleList;
    }

    public List<PlanarPoint> pointsList() {
        return pointsList;
    }

    public void preTriangulate() {
        boundingTriangle = getBoundingTriangle(pointsList);
        triangleList.add(boundingTriangle);
        randomPermutation(pointsList);
    }

    public void triangulate(PlanarPoint samplePoint) {
        // A procedure implementing the Bowyer - Watson algorithm
        // to calculate the DT of a set of points in a plane.
    }

    public void removeTriangles(List<Triangle> trianglesList) {
        // A procedure to remove all triangles from the list sharing
        // edges with the bounding-triangle
    }

    private Triangle getBoundingTriangle(List<PlanarPoint> pointsList) {
        //Obtains a bounding-triangle for a set of points
    }

    public void randomPermutation(List<PlanarPoint> pointsList) {
        //Obtains a random permutation of a set of points
    }
}

I have 3 more classes

  • PlanarPoint is a subclass of the Point2D.Double class that implements Comparable to provide y-coordinate sorting
  • A triangle is a class that defines a circle and radius radius for a triangle and determines whether a point is inside the triangle's circle
  • Edge is a class that represents Edge as one that has 2 PlanarPoints as its endpoints.
  • DrawingPanel - A class that acts as a surface on which points are added in click events and drawn on the screen.

    Now, here are a few problems that I have

    • , , , , ,
    • DrawingPanel, , JApplet/JFrame, , , , , ? , ?
    • SwingWorker , , , DT ?

- ,

, Chaitanya

+3
1

:

  • getGraphics() Graphics, Graphics , - (- ). BufferedImage JPanel JComponent BufferedImage paintComponent - paintComponent , .
  • , JFrame JApplet, , JComponent, JComponent, JPanel.
  • Swing, .
  • SwingWorker , , Swing, Swing - , SwingWorkers.
+5

All Articles