views:

19

answers:

1

I'm trying to use a Canvas to display an animated gif. I have the animation working, but the canvas is too small to see the entire image, and I cannot get it to resize.

I can't even get it to be the right size for the first frame, so the animation/threading code is being omitted. My code looks like:

Composite comp = new Composite(parent, SWT.None);
comp.setLayout(new GridLayout(1, false));

final ImageLoader loader = new ImageLoader();
loader.load(getClass().getClassLoader().getResourceAsStream("MYFILE.gif"));

final Canvas canvas = new Canvas(comp, SWT.None);
final Image image = new Image(Display.getCurrent(), loader.data[0]);

canvas.addPaintListener(new PaintListener() {
  public void paintControl(PaintEvent e) {
    e.gc.drawImage(image, 0, 0);
  }
});

The canvas area ends up being a tiny box of around 30x30, regardless of how big MYFILE.gif is. What the hell?

Anything I try to use to set the canvas size (setSize, setBounds, etc) does nothing.

A: 

Try using setPreferredSize. The layout manager will call setSize and/or setBounds and clobber the values you have set, but it will try to respect preferred size if it can. You can also use setMaximumSize and setMinimumSize to control how the canvas will resize if the enclosing component is resized.

EDIT: For SWT you could try this:

GridData gridData = new GridData();
gridData.widthHint = imageWidth;
gridData.heightHint = imageHeight;
canvas.setLayoutData(gridData);

I'm not an SWT expert, but the SWT documentation seems helpful.

Cameron Skinner
I'm using SWT 3.6 and I don't see any of those methods available in Canvas, Composite, or Shell.
gdm
Sorry, misread the question. This applies to Swing or AWT components.
Cameron Skinner
Argh, I didn't think to set the width/height hints in the GridData. God SWT, you can be retarded sometimes. It works now at least.
gdm