0

我正在尝试绘制一个带有程式化边缘的矩形,但不幸的是边缘仅覆盖左侧和顶部。

public Graphics2D Draw(Graphics2D b, long cameraX, long cameraY) {
    //let's do the drawing stuff, yay
    renderImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = renderImage.createGraphics();
    int[] xPs= new int[pointsarray.length];
    int[] yPs= new int[pointsarray.length];
    int i=0;
    for (Pair p:pointsarray){
        xPs[i]=p.pair[0];
        yPs[i]=p.pair[1];
        i++;
    }
    g.setColor(color);
    g.fillPolygon(xPs, yPs, xPs.length);
    g.setColor(color.darker());
    g.drawPolygon(xPs, yPs, xPs.length);
    b = super.Draw(b, cameraX, cameraY);
    return b;
}

是的,它是一个多边形。我必须保持这个可扩展的。

编辑:这就是 pointsarray 现在的样子。

    pointsarray = new Pair[4];

    pointsarray[0] = new Pair(0,0);
    pointsarray[1] = new Pair(0,height);
    pointsarray[2] = new Pair(width,height);
    pointsarray[3] = new Pair(width,0);

渲染方法:(这被第一种方法覆盖)(我也承认 -renderImage.getWidth()/2 部分是错误的)

public Graphics2D Draw(Graphics2D b ,long cameraX, long cameraY) {
    AffineTransform t = AffineTransform.getRotateInstance(r);
    AffineTransformOp op = new AffineTransformOp(t,AffineTransformOp.TYPE_BILINEAR);
    b.drawImage(renderImage, op, (int)(cameraX-x), (int)(cameraY-y));
    return b;
}

JFrame 最终渲染:

public void paintComponent(Graphics g) {
    Graphics2D r = (Graphics2D) g;
    Graphics2D b = (Graphics2D) r.create(); // i herd you liek buffers
    b.clearRect(0, 0, getWidth(), getHeight()); // clear the buffer
    // let's draw some... stuff
    long newCameraX = cameraX;
    long newCameraY = cameraY;
    // background
    b.setColor(Color.WHITE);
    b.fillRect(0, 0, getWidth(), getHeight());
    b.setColor(Color.BLACK);
    for (Entity entity : entities) {
        entity.Draw((Graphics2D) b, cameraX + (this.getSize().width / 2),
                cameraY + (this.getSize().height / 2));
    }

    // we're done drawing. let's put the stuff back in the first thing
    r = b;
    cameraX = newCameraX;
    cameraY = newCameraY;
}

对类:

public class Pair {
    public int[] pair = {0,0}; 

    public Pair(int num1, int num2) {
        pair[0] = num1;
        pair[1] = num2;
    }
    //you mad?
}
4

1 回答 1

1

基本上,您正在绘制超出图像的多边形的底部和右侧部分。 Java Graphics.fillPolygon:如何同时渲染右边缘和下边缘?

于 2012-06-12T16:47:16.660 回答