Java Application: drawing a large number of polygons(path2d) taking too long

RockChun :

I am trying to write an application that draws a map, using polygon, from arraylist of Path2D.Double coordinates. The problem is my sample size is over 26,000 polygons and it takes ages(around 5~6 minutes) to draw the whole thing. Not to mention the fact that it repaints itself when I try to scroll. When the program is run with few thousand coordinates it takes about 5~10 seconds.

is there anyway I can make the program run faster? also is possible to draw it once and use it?, instead of re-drawing the entire map every time I try to scroll or re-size the window.

Thx.

here is the code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class WholeMap extends JPanel {

    ArrayList<PropertyInfo> propertyList;
    ArrayList<Path2D.Double> polygonList= new ArrayList<Path2D.Double>();

    double mapOffsetX = 4200;
    double mapOffsetY = 4000;

    public WholeMap(ArrayList<PropertyInfo> pl) {
        propertyList = pl;
        JFrame frame = new JFrame();
        JScrollPane scroll = new JScrollPane(this);
        frame.setVisible(true);
        frame.setBounds(10,10,400,300); //(10, 10, 1100, 900);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(scroll);
        initDraw();
    }

    public void initDraw() {
        Path2D.Double polygon = new Path2D.Double();
        for (int i = 0; i < propertyList.size(); i++) {
            Point2D[] points = propertyList.get(i).getCoordinate();
            polygon.moveTo(((points[0].getX()-mapOffsetX)/20)+2000,((-points[0].getY()+mapOffsetY)/20)+300);
            for (int j = 1; j < points.length; j++) {
                polygon.lineTo(((points[j].getX()-mapOffsetX)/20)+2000,((-points[j].getY()+mapOffsetY)/20)+300);
            }
            polygon.closePath();
            polygonList.add(polygon);
        }
    }

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

    public void paintComponent(Graphics g1) {
        super.paintComponent(g1);
        Graphics2D g = (Graphics2D) g1;
        this.setOpaque(true);
        this.setBackground(Color.black);

        g.setColor(Color.GRAY);
        for (int k = 0; k < polygonList.size(); k++) {
            g.draw(polygonList.get(k));
            System.out.println("Polygon Count: "+ k);
        }       
    }
} //EOF



Andrew Merrill :

In your initDraw function, don't you want to put the line

         Path2D.Double polygon = new Path2D.Double();

inside of the for loop?

As written, it looks like each polygon will include all of the points of all of previous polygons as well.

Also, as you point out, it would be better to do the drawing once if possible. You should trying making a BufferedImage and drawing the polygons on it. Then your paintComponent function could just redraw from the saved BufferedImage which would be very fast.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358730&siteId=1