Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » [Draw2] Drawing Vertical Text(How to draw text vertically )
[Draw2] Drawing Vertical Text [message #520396] Fri, 12 March 2010 09:01 Go to next message
S is currently offline SFriend
Messages: 8
Registered: March 2010
Junior Member
My question is quite simple :

How to draw a vertical text/label ?

When I use the graphics.rotate method , the label is lost somewhere in Wonkyland ...

Here a code try and another that both don't work :

graphics.pushState();
graphics.translate(this.bounds.x, this.bounds.y);        
graphics.rotate(-270);
graphics.drawText("TEST", 0, 0); 
graphics.popState()


new Label() {

            @Override
            protected void paintFigure(Graphics graphics) {
                if (isOpaque())
                    super.paintFigure(graphics);
                Rectangle bounds = getBounds();
                graphics.translate(bounds.x, bounds.y);

                if (!isEnabled()) {
                    graphics.translate(1, 1);
                    graphics.setForegroundColor(ColorConstants.buttonLightest);
                    graphics.drawText(getSubStringText(), getTextLocation());
                    graphics.translate(-1, -1);
                    graphics.setForegroundColor(ColorConstants.buttonDarker);
                }
                graphics.rotate(-270);
                graphics.drawText(getSubStringText(), getTextLocation());
                graphics.translate(-bounds.x, -bounds.y);
            }
        };


I read somewhere that rotate method will change coordinate but I didn't find any solution...

Any idea is welcomed ....

Thanks.
Re: [Draw2] Drawing Vertical Text [message #717821 is a reply to message #520396] Mon, 22 August 2011 13:17 Go to previous messageGo to next message
Karthikeyan Missing name is currently offline Karthikeyan Missing nameFriend
Messages: 47
Registered: July 2011
Member
Hi Sebastein,
Me too stucked with the above problem, have u found any solution for the above one. If so, kindly post the code.

Regards,
Karthikeyan
Re: [Draw2] Drawing Vertical Text [message #717833 is a reply to message #717821] Mon, 22 August 2011 13:32 Go to previous messageGo to next message
S is currently offline SFriend
Messages: 8
Registered: March 2010
Junior Member
Don't ask me How, but it works !!

I've been stuck for several hours to put the label at the right place !!

My eclipse account is unusable Sad


Good luck with that :

let me know if you solve your problem

public class MyLabel extends Label implements RotatableDecoration {

    /** The rotation value. */
    private double angle;

    /** the final location . */
    private final Point location = new Point();

    /** The reference point. */
    private final Point refPoint = new Point();

    /** The location where we want the text. */
    private Point textWishedLocation;

    public MyLabel () {
        super();
    }

    public MyLabel (Image i) {
        super(i);
    }

    public MyLabel (String s) {
        super(s);
    }

    public MyLabel (String s, Image i) {
        super(s, i);
    }

    public Point getTextWishedLocation() {
        return this.textWishedLocation;
    }

    public void setTextWishedLocation(Point newTextWishedLocation) {
        this.textWishedLocation = newTextWishedLocation;
    }

    @Override
    public Rectangle getBounds() {
        if (this.bounds == null) {
            int xDiff = this.refPoint.x - this.location.x;
            int yDiff = this.refPoint.y - this.location.y;
            int x, y, width, height;
            if (xDiff >= 0) {
                width = xDiff;
                x = this.location.x;
            } else {
                width = -xDiff;
                x = this.refPoint.x;
            }
            if (yDiff >= 0) {
                height = yDiff;
                y = this.location.y;
            } else {
                height = -yDiff;
                y = this.refPoint.y;
            }
            this.bounds = new Rectangle(x, y, width, height);
        }
        return this.bounds;
    }

    @Override
    public void setLocation(Point p) {
        this.bounds = null;
        this.location.setLocation(p);
    }

    @Override
    public void setReferencePoint(Point ref) {
        Point pt = Point.SINGLETON;
        this.refPoint.setLocation(ref);
        pt.setLocation(ref);
        pt.negate().translate(this.location);
        // setRotation(Math.atan2(pt.y, pt.x));
    }

    public void setRotation(/* double angle */int newAngle) {
        this.bounds = null;
        this.angle = newAngle;// Math.toDegrees(angle);
    }

    @Override
    protected Point getIconLocation() {
        double linkLength = Math.sqrt(this.bounds.width * this.bounds.width + this.bounds.height * this.bounds.height);
        int y = 0;// -getIconSize().height;
        int x;
        switch (getTextAlignment()) {
        case LEFT:
            x = 0;
            break;
        case RIGHT:
            x = (int) (linkLength - getIconSize().width);
            break;
        default:
            x = (int) (linkLength / 2 - getIconSize().width / 2);
        }
        if (x < 0) {
            x = 0;
        }
        return new Point(x, y);
    }

    public Dimension getTextDimension() {
        return getTextSize();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Point getTextLocation() {
        Dimension textDimension = getTextSize();
        double linkLength = Math.sqrt((this.bounds.width + getIconSize().width) * 2 + (this.bounds.height + getIconSize().height) * 2);
        int y = -textDimension.height;
        int x;
        switch (getTextAlignment()) {
        case LEFT:
            x = 0;
            break;
        case RIGHT:
            x = (int) (linkLength - textDimension.width);
            break;
        default:
            x = (int) (linkLength / 2 - textDimension.width / 2);
        }
        if (x < getIconSize().width + 5) {
            x = getIconSize().width + 5;
        }
        return new Point(x, y);
    }

    @Override
    protected void paintFigure(Graphics graphics) {

        // Draws the text
        graphics.pushState();
        graphics.setClip(graphics.getClip(new Rectangle()).expand(50, 50));
        graphics.translate(this.bounds.x, this.bounds.y);
        if (this.angle < 0) {
            graphics.translate(0, getBounds().height);
        }
        graphics.rotate((float) this.angle);

        if (isOpaque()) {
            graphics.fillRectangle(getBounds());
        }
        if (getBorder() instanceof AbstractBackground) {
            ((AbstractBackground) getBorder()).paintBackground(this, graphics, NO_INSETS);
        }
        if (getIcon() != null) {
            graphics.drawImage(getIcon(), getIconLocation());
        }
        if (!isEnabled()) {
            graphics.translate(1, 1);
            graphics.setForegroundColor(ColorConstants.buttonLightest);
            graphics.drawText(getSubStringText(), getTextLocation());
            graphics.translate(-1, -1);
            graphics.setForegroundColor(ColorConstants.buttonDarker);
        }
        graphics.drawText(getSubStringText(), getTextWishedLocation() /* getTextLocation() */);
        graphics.popState();
    }

}


[Updated on: Mon, 22 August 2011 13:40]

Report message to a moderator

Re: [Draw2] Drawing Vertical Text [message #718482 is a reply to message #717833] Wed, 24 August 2011 13:27 Go to previous messageGo to next message
Karthikeyan Missing name is currently offline Karthikeyan Missing nameFriend
Messages: 47
Registered: July 2011
Member
Hi,

Myself need the output like the below one, how to achieve this one

-------------------------------
| | |
| E | |
| M | |
| A | |
| N | |
| | |
-------------------------------

Name - each alphabets rotated to 90 degree in anticlockwise direction and read from bottom to top

Regards,
Karthikeyan

[Updated on: Thu, 25 August 2011 04:09]

Report message to a moderator

Re: [Draw2] Drawing Vertical Text [message #718485 is a reply to message #718482] Wed, 24 August 2011 13:43 Go to previous messageGo to next message
S is currently offline SFriend
Messages: 8
Registered: March 2010
Junior Member
Damned your boss is crueler than mine.

I really don't know how to do that !

I don't have an rcp development environement at the moment so I can't test anything.

Perhaps you should make a figure in which you override the paint method to draw single caracter text each one below the previous.

It might be possible like that


protcode
String s = "NAME";
for(int i = 0 ; i <= s.length();i++){
    graphics.drawText(s.substring(i,i+1));
    graphics.translate(0, -15);//get the right font size height
}


Give us a feedback Very Happy

Good Luck

Re: [Draw2] Drawing Vertical Text [message #719782 is a reply to message #718485] Mon, 29 August 2011 01:40 Go to previous messageGo to next message
karthick9686@gmail.com Missing nameFriend
Messages: 41
Registered: July 2011
Member
hi the problem for vertical label is solved , now i am not able position it properly,
i am using an toolbarlayout with alignment center. the label is not positioning itself in the center vertically. the problem is we have to give the textwishedlocation value . its statically fixed in one location . i would like it to be centered as the container for the label resizes. thanks in advance
Re: [Draw2] Drawing Vertical Text [message #726021 is a reply to message #719782] Fri, 16 September 2011 12:38 Go to previous message
Karthikeyan Missing name is currently offline Karthikeyan Missing nameFriend
Messages: 47
Registered: July 2011
Member
Hi,

the problem for vertical label has been solved in gef code, by extending ImageUtility Class, just refer the following code

private final ImageFigure image;

final Image icon = ImageUtility.createRotatedImageOfString( "Label Name",
new Font( Display.getCurrent(), "Times New Roman", 12, SWT.BOLD ),
ColorConstants.black, ColorConstants.white );
Previous Topic:How to get objects selected under a region
Next Topic:adding association connection to artifacts
Goto Forum:
  


Current Time: Sat Dec 20 13:45:13 GMT 2014

Powered by FUDForum. Page generated in 0.05018 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software