簡體   English   中英

如何在Java中使用點畫一條線?

[英]How do I draw a line in java using points?

我想知道如何在Java中使用不同的點畫一條線。 這是我到目前為止的代碼。 我寧願不獲取每個點的x和y,而只是讓程序自己使用x,y中的點。 我對編程還是很陌生,目前還不太擅長使用javadoc。 以下是我現在擁有的代碼:

 //Create a Polygon class. A polygon is a closed shape with lines joining the corner points. 
 //You will keep the points in an array list. Use object of java.awt.Point for the point.

 //Polygon will have as an instance variable an ArrayList of Points to hold the points
 //The constructor takes no parameters but initializes the instance variable.  The
 //Polygon class also has the following methods:
 //    add: adds a Point to the polygon
 //    perimeter: returns the perimeter of the polygon
 //    draw: draws the polygon by connecting consecutive points and then
 //          connecting the last point to the first.
 //
 //No methods headers or javadoc is provided this time. You get to try your hand at writing
 //a class almost from scratch

 // Need help starting this question? In the lesson titled
 // "Starting points: Problem Set Questions", go to the
 // problem titled "Problem Set 6 - Question 3" for some tips on 
 // how to begin.

 import java.util.ArrayList; import java.awt.Point;

 public class Polygon {
     ArrayList<Point> points;

     /**
     **  a Polygon represents a shape with flat lines and several points.
     */ 
     public Polygon(){
     /**
     * Constructs empty array of points.
     */
     points = new ArrayList<Point>();
     }    /**    ** Adds points to the polygon points Array.    *@param = points x and y coordinates to add to class    */
     public void addPoints(Point point){
         points.add(point);
     }
     /**
     **gets distance between points in ArrayList
     */
     public double perimeter(){
         double perimeter = 0;
         int i = 0;
         while(i<points.size()){
             if(i<points.size()-1){
              perimeter = perimeter + points.get(i).distance(points.get(i+1));   
             }
             else{
              perimeter = perimeter + points.get(i).distance(points.get(0));   
             }
         }
         return perimeter;
     }
      /**    * Draws Polygon using points from points ArrayList    */
     public void drawPolygon(){
      int i = 0;
         while(i < points.size()-1){
           Line2D line = Line2D.Float(points.get(i), points.get(i+1));
             line.draw();
         }
         Line2D lastLine = Line2D.Float(points.get(0), points.get(points.size()-1));
         lastLine.draw();
     } }

讓我正確理解這一點,您想實現draw()方法嗎?

如果是這樣,您需要做的就是搜索布雷森納姆算法 ,該算法根據兩個給定點畫一條線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM