Reputation: 23114
The java.awt.geom
package looks very useful in manipulation of vector graphics, I am especially interested in this function:
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)
################ ################ ################
############## ############## ################
############ ############ ################
########## ########## ################
######## ######## ################
###### ###### ###### ######
#### #### #### ####
## ## ## ##
I am very new to java, so forgive me if I am asking something stupid, but when I paste the code into netbeans, it indicates an error, saying that I should declare triangle
somewhere. The nature of this syntax is unknown to me, and after some searching, I still don't know what to do about it.
Upvotes: 0
Views: 394
Reputation: 44355
It appears that this is pseudo-code (a conceptual illustration) rather than actual code.
The code should look like:
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.
}
}
I left out the drawing part because it is nontrivial, and is in my opinion out of the scope of a "new to Java" question.
I recommend reading the Java Tutorials at http://docs.oracle.com/javase/tutorial/ (look for the section titled "Trails Covering the Basics"). They are an easy and free way to learn Java.
Upvotes: 2