views:

40

answers:

2

I created a bufferedimage which i applied to a Rectangle to use as filling pattern to shape S. If i change S's position, the filling pattern changes with it instead of remaining "fixed". What could it be?

Image: (the pattern is a 3 stripes, all with the same aspect ratio) :alt text

    if (bannerPatternCreated == false) {

        banner = new BufferedImage(size * 3, size * 3, BufferedImage.TYPE_INT_RGB);
        Graphics2D gc = banner.createGraphics();


        System.out.println("Creating banner...");

        gc.setColor(Color.black);
        gc.fillRect(0, 0, size, size * 3);

        gc.setColor(Color.BLUE);
        gc.fillRect(size, 0, size, size * 3);

        gc.setColor(Color.WHITE);
        gc.fillRect(size * 2, 0, size, size * 3);
        gc.dispose();
        bannerPatternCreated = true;

    }

    Rectangle patternPencil = new Rectangle(size, size);
    g2.setPaint(new TexturePaint(banner, patternPencil));

    Rectangle recto = new Rectangle(presentX-size, presentY-size, size, size);
    g2.fill(recto);
A: 

I think the TexturePaint docs indicate why the problem:

...the texture is anchored to the upper left corner of a Rectangle2D that is specified in user space. Texture is computed for locations in the device space by conceptually replicating the specified Rectangle2D infinitely in all directions

It's as if you're rectangle is drawn from 0,0 and replicated over and over, and the visible parts are small "windows" that are opened with the call to g2.fill.

If you're drawing to a canvas-type component, could you just use one of the Graphics.drawImage methods at the appropriate x,y?

Ash
+1  A: 

It looks like the texture position is fixed, and so when you move recto around you're just getting a different view of the underlying infinitely-repeating texture.

If you change the patternPencil rect to be the same size/position as recto, I think it should get sorted:

Rectangle patternPencil = new Rectangle(presentX-size, presentY-size, size, size);
tzaman