piccolo2d / piccolo2d.java

Structured 2D Graphics Framework
http://piccolo2d.org
Other
51 stars 14 forks source link

PSWTText not respecting bounds.x and bounds.y in painting #219

Closed mro closed 10 months ago

mro commented 9 years ago

Originally reported on Google Code with ID 219

What steps will reproduce the problem?
1. create a composite PNode containing a child of type PSWTText
2. in layoutChildren() method write the following code

        PBounds bounds = getBounds();
        bounds.x = 5;
        bounds.y = 5;
        bounds.width -= 10;
        bounds.height -= 10;
        this.textNode.setBounds(bounds);

3. run the program

What is the expected output? What do you see instead?

this code should produce a text with a xmargin and a ymargin of 5 from its parent .

Instead x and y left and top margin are ignored. bottom and right margin are ok.

It work if i use translate method, but it should also work with plain x and y set.

//see in code replacement to fix it

    /**
     * Paints this object normally (show it's text). Note that the entire text
     * gets rendered so that it's upper left corner appears at the origin of
     * this local object.
     *
     * @param ppc The graphics context to paint into.
     */
    @Override
    public void paintAsText(final PPaintContext ppc) {
        final SWTGraphics2D sg2 = (SWTGraphics2D) ppc.getGraphics();

        if (!isTransparent()) {
            if (getPaint() == null) {
                sg2.setBackground(Color.WHITE);
            }
            else {
                sg2.setBackground((Color) getPaint());
            }

            //bad sg2.fillRect(0, 0, (int) getWidth(), (int) getHeight());
            sg2.fillRect(getX(), getY(), (int) getWidth(), (int) getHeight()); //fixed
        }

        sg2.translate(this.padding, this.padding);

        sg2.setColor(this.penColor);
        sg2.setFont(this.font);

        String line;
        //bad double y = 0;
        double y = getY(); //fixed

        final FontMetrics fontMetrics = sg2.getSWTFontMetrics();

        final Iterator lineIterator = this.lines.iterator();
        while (lineIterator.hasNext()) {
            line = (String) lineIterator.next();
            if (line.length() != 0) {
                //bad sg2.drawString(line, 0, y, true);
                sg2.drawString(line, getX(), y, true); //fixed
            }

            y += fontMetrics.getHeight();
        }

        sg2.translate(-this.padding, -this.padding);
    }

Reported by steeve.stlaurent on 2011-06-08 03:49:49

mro commented 9 years ago
my bad, i didn't read paint method comment.
but why limiting use of direct x, y set ?

Reported by steeve.stlaurent on 2011-06-08 14:01:58

mro commented 9 years ago

Reported by heuermh on 2012-08-31 20:29:30