adding background image in compartment [message #665940] |
Mon, 18 April 2011 09:26 |
fenrir Messages: 15 Registered: April 2011 Location: France |
Junior Member |
|
|
Hi,
Here is my problem :
I want to add a background in a compartment. I found several subjects about it, but it doesn't work.
I used this code :
in a FreeFormLayerWithImageBGR class :
//********************************************************** *****************
public class FreeFormLayerWithImageBGR extends FreeformLayer {
Image image;
public FreeFormLayerWithImageBGR(String path) {
super();
try {
FileInputStream fis = new FileInputStream(path);
setImage(new Image(PlatformUI.getWorkbench().getDisplay(),fis));
System.out.println(fis.toString());
} catch (Exception e) {
System.out.println(e);
setImage(null);
}
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
@Override
protected void paintFigure(Graphics graphics) {
if (getImage() != null) {
Rectangle targetRect = getBounds().getCopy();
org.eclipse.swt.graphics.Rectangle imgBox = getImage().getBounds();
graphics.drawImage(getImage(), 0, 0, imgBox.width, imgBox.height,targetRect.x, targetRect.y, targetRect.width, targetRect.height);
//System.out.println("il est passé par ici");
}else {
System.out.println("error : no image");
}
super.paintFigure(graphics);
}
}
//********************************************************** *****************
and in the createFigure method of my ***compartmentEditPart :
//********************************************************** *****************
public IFigure createFigure() {
ResizableCompartmentFigure result = (ResizableCompartmentFigure) super.createFigure();
result.setTitleVisibility(false);
IFigure figure = new FreeFormLayerWithImageBGR("C:\\images\\background.jpeg");
result.repaint();
return result;
}
//********************************************************** *****************
but nothing happends ='(
(I tried to return figure but it returns an exception "FreeFormLayerWithImageBGR cannot be cast to ResizableCompartmentFigure")
Can you help me please?
Thanks,
Fenrir
[Updated on: Mon, 18 April 2011 09:27] Report message to a moderator
|
|
|
|
Re: adding background image in compartment [message #714621 is a reply to message #665940] |
Thu, 11 August 2011 06:07 |
vinay Messages: 4 Registered: August 2011 |
Junior Member |
|
|
hi fenrin..
try this... still having trouble but it works..
public IFigure createFigure() {
final ResizableCompartmentFigure result = (ResizableCompartmentFigure) super
.createFigure();
result.setTitleVisibility(false);
final Figure f = new FreeformLayer() {
@Override
protected void paintFigure(Graphics graphics) {
Image img = null;
try {
img = new org.eclipse.swt.graphics.Image(PlatformUI
.getWorkbench().getDisplay(), new FileInputStream(
new File("d:/images.jpg")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (img != null) {
Rectangle targetRect = result.getBounds();
org.eclipse.swt.graphics.Rectangle imgBox = img.getBounds();
graphics.drawImage(img, 0, 0, imgBox.width, imgBox.height,
targetRect.x, targetRect.y, targetRect.width,
targetRect.height);
} else {
System.out.println("error : no image");
}
super.paintFigure(graphics);
}
};
f.addFigureListener(new FigureListener() {
@Override
public void figureMoved(IFigure source) {
f.setBorder(new MarginBorder(source.getBounds().x));
}
});
f.setLayoutManager(new FreeformLayout());
result.repaint();
result.add(f, 0);
return result;
}
|
|
|
Powered by
FUDForum. Page generated in 0.02812 seconds