为什么我的Java建筑没有正确绘图?

时间:2012-06-12 15:16:24

标签: java drawing awt polygon graphics2d

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

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 = 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 final render:

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?
}

1 个答案:

答案 0 :(得分:1)

基本上,您正在绘制多边形的底部和右部,超出图像范围。 Java Graphics.fillPolygon: How to also render right and bottom edges?