• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help extending a file to read an image from a path  RSS feed

 
Andy Farquea
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the eclipse plugin GMF if that helps. I want to write my own class that extends a class that comes with the plugin. I need the class to define a new method lets call it setImagePath. the path to where the .jpg image i want loading into my program will be entered in the properties view of eclipse not in the java class.

The class i want extending is ImageFigure and can be found below, as my java coding skills are relativly poor i was wondering if anyone could instruct me what i need to be writing in my class or even produce a qick bit of code as i dont think it should be too difficult for a competent programmer, i am even prepared to compensate anyone for their time if they feel it merits it;

package org.eclipse.draw2d;

import org.eclipse.swt.graphics.Image;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
* A Figure that simply contains an Image. Use this Figure, instead of a Label, when
* displaying Images without any accompanying text. This figure is not intended to have a
* layout mananger or children.
* <P>
* Note that it is the client's responsibility to dispose the given image. There is no
* "free" resource management in draw2d.
*
* @author Pratik Shah
*/
public class ImageFigure
extends Figure
{

private Image img;
private Dimension size = new Dimension();
private int alignment;

/**
* Constructor<br>
* The default alignment is <code>PositionConstants.CENTER</code>.
*/
public ImageFigure() {
this(null, PositionConstants.CENTER);
}

/**
* Constructor<br>
* The default alignment is <code>PositionConstants.CENTER</code>.
*
* @paramimageThe Image to be displayed
*/
public ImageFigure(Image image) {
this(image, PositionConstants.CENTER);
}

/**
* Constructor
*
* @paramimageThe Image to be displayed
* @paramalignmentA PositionConstant indicating the alignment
*
* @see ImageFigure#setImage(Image)
* @see ImageFigure#setAlignment(int)
*/
public ImageFigure(Image image, int alignment) {
setImage(image);
setAlignment(alignment);
}

/**
* @return The Image that this Figure displays
*/
public Image getImage() {
return img;
}

/**
* Calculates the necessary size to display the Image within the figure's client area.
*
* @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
*/
public Dimension getPreferredSize(int wHint, int hHint) {
if (getInsets() == NO_INSETS)
return size;
Insets i = getInsets();
return size.getExpanded(i.getWidth(), i.getHeight());
}

/**
* @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
*/
protected void paintFigure(Graphics graphics) {
super.paintFigure(graphics);

if (getImage() == null)
return;

int x, y;
Rectangle area = getClientArea();
switch (alignment & PositionConstants.NORTH_SOUTH) {
case PositionConstants.NORTH:
y = area.y;
break;
case PositionConstants.SOUTH:
y = area.y + area.height - size.height;
break;
default:
y = (area.height - size.height) / 2 + area.y;
break;
}
switch (alignment & PositionConstants.EAST_WEST) {
case PositionConstants.EAST:
x = area.x + area.width - size.width;
break;
case PositionConstants.WEST:
x = area.x;
break;
default:
x = (area.width - size.width) / 2 + area.x;
break;
}
graphics.drawImage(getImage(), x, y);
}

/**
* Sets the alignment of the Image within this Figure. The alignment comes into play
* when the ImageFigure is larger than the Image. The alignment could be any valid
* combination of the following:
*
* <UL>
* <LI>PositionConstants.NORTH</LI>
* <LI>PositionConstants.SOUTH</LI>
* <LI>PositionConstants.EAST</LI>
* <LI>PositionConstants.WEST</LI>
* <LI>PositionConstants.CENTER or PositionConstants.NONE</LI>
* </UL>
*
* @param flag A constant indicating the alignment
*/
public void setAlignment(int flag) {
alignment = flag;
}

/**
* Sets the Image that this ImageFigure displays.
* <p>
* IMPORTANT: Note that it is the client's responsibility to dispose the given image.
*
* @param imageThe Image to be displayed. It can be <code>null</code>.
*/
public void setImage(Image image) {
if (img == image)
return;
img = image;
if (img != null)
size = new Rectangle(image.getBounds()).getSize();
else
size = new Dimension();
revalidate();
repaint();
}

}
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would probably get better feedback by asking an Eclipse plugin specific mailing list or forum. This forum is geared more to the standard Java IO classes like java.io.* and java.nio.*.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!