0

java.awt.geom包看起来在处理矢量图形方面非常有用,我对这个功能特别感兴趣:

public void add(Area rhs)

Adds the shape of the specified Area to the shape of this Area. The resulting shape of this Area will include the union of both shapes, or all areas that were contained in either this or the specified Area. 

 // Example:
 Area a1 = new Area([triangle 0,0 => 8,0 => 0,8]);
 Area a2 = new Area([triangle 0,0 => 8,0 => 8,8]);
 a1.add(a2);

    a1(before)     +         a2         =     a1(after)

 ################     ################     ################
 ##############         ##############     ################
 ############             ############     ################
 ##########                 ##########     ################
 ########                     ########     ################
 ######                         ######     ######    ######
 ####                             ####     ####        ####
 ##                                 ##     ##            ##

我对java很陌生,所以如果我问一些愚蠢的问题,请原谅我,但是当我将代码粘贴到netbeans时,它表明一个错误,说我应该在triangle某个地方声明。我不知道这种语法的性质,经过一番搜索,我仍然不知道该怎么办。

4

1 回答 1

2

看起来这是伪代码(概念说明)而不是实际代码。

代码应如下所示:

import java.awt.Polygon;
import java.awt.geom.Area;

public class AreaAddition {

    public void addTriangles() {
        Polygon triangle1 = new Polygon();
        triangle1.addPoint(0, 0);
        triangle1.addPoint(8, 0);
        triangle1.addPoint(0, 8);

        Polygon triangle2 = new Polygon();
        triangle2.addPoint(0, 0);
        triangle2.addPoint(8, 0);
        triangle2.addPoint(8, 8);

        Area a1 = new Area(triangle1);
        Area a2 = new Area(triangle2);
        a1.add(a2);

        // Code that draws the Area belongs here.
    }

}

我省略了绘图部分,因为它很重要,并且在我看来超出了“Java 新手”问题的范围。

我建议阅读http://docs.oracle.com/javase/tutorial/上的 Java 教程(查找标题为“Trails Covering the Basics”的部分)。它们是学习 Java 的一种简单且免费的方式。

于 2013-10-30T22:58:57.963 回答