读取.drw文件中的点对象和面板上的绘图线

时间:2014-09-15 05:33:35

标签: java arrays user-interface file-io point

我正在尝试读取来自.drw文件的点对象,其中包含线段的端点,并在我运行程序时在面板上绘制正方形。这是我到目前为止所得到的。你能否就如何使用数组绘制点来提供一些想法?

//在方法中读取.drw文件中的点对象

try
{
    fos = new FileInputStream("square.drw");
    oos = new ObjectInputStream(fos);

    // creating an  object of Point class
    endPoints = new Point[sampleSize];
    // Creating the Points to put in the array (Deserialize the objects)
    for(int i = 0; i < endPoints.length+1; i++)
    {
        endPoints[i] = (Point) oos.readObject(); 
        endPoints[sampleSize-i+1] = new Point(); // initializing the value before accessing it
        // now setting the values , since the point's aren't null
        endPoints[0].x = x1; // sample points
        endPoints[0].y = y1;
        endPoints[1].x = x2;
        endPoints[1].y = y2;

        // how to initialize every point in the array using for??
    }
}

//实际绘图命令

public void paintComponent(Graphics g)
{
    // draw parts of the drawing using lineColour
    super.paintComponent(g);
    //Point p = new Point[endPoints];
    int numPoints = endPoints.length;
    for(int i = 0; i < numPoints; i++)
    {
        g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
    }
}

0 个答案:

没有答案