填充颜​​色自定义形状Java

时间:2015-12-08 07:25:47

标签: java graphics2d

我想填充自定义形状内的颜色

这是我的梯形形状代码

Trapezium Class

public class Trapezium implements Shape {
private GeneralPath trapezium = new GeneralPath();

public Trapezium (Point2D A, double height, double width)
{

    double Ax = A.getX();
    double Ay = A.getY();

    double Bx = Ax + (width/6);
    double By = Ay;

    double Cx = Bx + (width/3);
    double Cy = By + height;

    double Dx = Cx - width;
    double Dy = Cy;

    double Ex = Dx + (width/3);
    double Ey = Dy - height;


    trapezium.moveTo(Ax, Ay);
    trapezium.lineTo(Bx, By);
    trapezium.lineTo(Cx, Cy);
    trapezium.lineTo(Dx, Dy);
    trapezium.lineTo(Ex, Ey);
    trapezium.closePath();

}

@Override
public java.awt.Rectangle getBounds() {
    return trapezium.getBounds();
}

@Override
public Rectangle2D getBounds2D() {
    return trapezium.getBounds2D();
}

@Override
public boolean contains(double x, double y) {
    return trapezium.contains(x, y);
}

@Override
public boolean contains(Point2D p) {
    return trapezium.contains(p);
}

@Override
public boolean intersects(double x, double y, double w, double h) {
    return trapezium.intersects(x, y, w, h);
}

@Override
public boolean intersects(Rectangle2D r) {
        return trapezium.intersects(r);
}

@Override
public boolean contains(double x, double y, double w, double h) {
    return trapezium.contains(x, y,w ,h);
}

@Override
public boolean contains(Rectangle2D r) {
    return trapezium.contains(r);
}

@Override
public PathIterator getPathIterator(AffineTransform at) {
    return trapezium.getPathIterator(at);
}

@Override
public PathIterator getPathIterator(AffineTransform at, double flatness) {
    return trapezium.getPathIterator(at, flatness);
}
}

这是我调用梯形类时的代码

DrawPanel类

else if(type.get(a).equals("Trapezium"))
{
    int[]coordinates=allShapes.get(a);
    Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);

    Trapezium trapezium = new Trapezium(b,coordinates[3], coordinates[2]);
    g2.setStroke(new BasicStroke(coordinates[4]));
    g2.setPaint(dragColor);
    g2.draw(trapezium);
}

setPaint只为我的形状的笔划着色,我如何为形状的内部着色?

Image of my current code when executed

修改

我有2个JColorChooser,1个用于形状的笔触,2个用于形状内的颜色

如果我使用

g2.setPaint(COLOR2) g2.fill(梯形)

如何让color1改变中风的颜色?

1 个答案:

答案 0 :(得分:2)

您必须填充形状^ _ ^

$.mobile.defaultPageTransition = 'none';