I am trying to create a drawing of a triangle with the path2D class and when I try to create my triangle, it says that "Cannot instantiate the type Path2D", I'm not really sure what I did wrong here if anyone can help.
package houseGoesVroom;
import java.awt.Graphics2D;
import java.awt.geom.*;
public class HousePlans
{
//instance variables
private int x;
private int y;
//constructor
public HousePlans(int cornerX, int cornerY)
{
x = cornerX;
y = cornerY;
}
//draw method
public void draw(Graphics2D g2)
{
int r = (int) (256 * Math.random());
int g = (int) (256 * Math.random());
int b = (int) (256 * Math.random());
g2.fillRect(0, 40, 80, 120);
Path2D path = new Path2D();
path.moveTo(0,40);
path.lineTo(40,0);
path.lineTo(80, 40);
path.closePath();
g2.draw(path);
}
}
Path2D
is an abstract class. You can not create object of an abstract class or interface. There are two concrete implementations of Path2D
inside Path2D
class. Path2D.Double and Path2D.Float. You can use any of them based on your precision level of your coordinates.
Path2D path = new Path2D.Double();