0

I'm trying to draw a rectangle with a stylized edge on it, but unfortunately the edge only covers the left and top sides.

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

Yes, it's a polygon. I have to keep this extendable.

EDIT: This is what pointsarray looks like for now.

    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);

Render method: (this is overridden by the first method) (also I admit that the -renderImage.getWidth()/2 part was wrong)

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

Pair class:

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

    public Pair(int num1, int num2) {
        pair[0] = num1;
        pair[1] = num2;
    }
    //you mad?
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
striking
  • 655
  • 1
  • 8
  • 25
  • What sort of reference do we have? Is this an applet, JFrame, can we see the paint method? – Michael Jun 12 '12 at 15:19
  • Where is pointsarray ? The error may be in its definition (like missing two sides). – Denys Séguret Jun 12 '12 at 15:20
  • 2
    for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Jun 12 '12 at 15:21
  • Is it possible that what you are experiencing is due to the fact that you subtract `renderImage.getWidth()/2` from both the x and y parts of the camera in `Draw()`? – Attila Jun 12 '12 at 15:26
  • 1
    What's the Pair class ? The call to its content is strange. Does each instance contain an array called `pair` of just two items ? Ok, seen the edit, this is indeed a strange class... – Denys Séguret Jun 12 '12 at 15:29
  • That `r = b` (and following assignments) in `paintComponent()` doesn't really do anything, unless there's more in that method. I also don't see anything that would cause _any_ offsets onscreen between the two rectangles being drawn... I'm guessing you may be only seeing an offset 'up-and-left' may have to do with drawing order. – Clockwork-Muse Jun 12 '12 at 15:47
  • 1) *"JFrame final render: `public void paintComponent(`"* Add @Override notation to discover that is a false statement. 2) But to repeat good advice from earlier. *For better help sooner, post an [SSCCE](http://sscce.org/).* – Andrew Thompson Jun 12 '12 at 16:10

1 Answers1

1

Basically you are drawing the bottom and right portions of your polygon outsize of the image. Java Graphics.fillPolygon: How to also render right and bottom edges?

Community
  • 1
  • 1
Clint
  • 8,988
  • 1
  • 26
  • 40