[Draw2] Drawing Vertical Text [message #520396] |
Fri, 12 March 2010 09:01 |
S 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 #717833 is a reply to message #717821] |
Mon, 22 August 2011 13:32 |
S 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
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 #726021 is a reply to message #719782] |
Fri, 16 September 2011 12:38 |
Karthikeyan Missing name 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 );
|
|
|
Powered by
FUDForum. Page generated in 0.04230 seconds