すでに理解しているかどうかはわかりませんが、問題は、サイズ変更時に ScrolledComposite がコンテンツを常に 0/0 に設定することです。あなたのアプローチが OS X で機能する理由がわかりません。また、ここに Mac がないため、私の例をテストしていません。
解決策は、少なくとも ScrolledComposite のクライアント領域と常に同じ大きさのフィラー コンポジットを使用することです。そのフィラーで、キャンバスを正しく中央に配置できます。
SCのクライアント領域がキャンバスよりも小さい場合、追加のボーナスとして、キャンバスを中央に配置する小さな例を作成しました(最初にそれがあなたの質問だと思ったからです:))
いくつかの微調整が必要になる場合があります。そのコードには OS X にいくつかの不具合があると思います...
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test {
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout( new FillLayout() );
final ScrolledComposite sComp = new ScrolledComposite( shell, SWT.H_SCROLL | SWT.V_SCROLL );
final Composite fillComp = new Composite( sComp, SWT.NONE );
sComp.setLayout( new FillLayout() );
final Canvas c = new Canvas( fillComp, SWT.DOUBLE_BUFFERED );
c.addPaintListener( new PaintListener() {
public void paintControl(PaintEvent e) {
Point p = c.getSize();
e.gc.setBackground( display.getSystemColor( SWT.COLOR_RED ));
e.gc.fillRectangle( 0, 0, p.x, p.y );
e.gc.setBackground( display.getSystemColor( SWT.COLOR_BLUE ));
e.gc.fillRectangle( p.x / 2 - 10, p.y / 2 - 10, 20, 20 );
}
});
c.setSize( 400, 400 );
sComp.setContent( fillComp );
sComp.addControlListener( new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
Rectangle clientArea = sComp.getClientArea();
Point cSize = c.getSize();
int fillX = Math.max( clientArea.width, cSize.x );
int fillY = Math.max( clientArea.height, cSize.y );
fillComp.setSize( fillX, fillY );
int cX = ( clientArea.width - cSize.x ) / 2;
int cY = ( clientArea.height - cSize.y ) / 2;
sComp.setOrigin( -cX, -cY );
int locX = Math.max( cX, 0 );
int locY = Math.max( cY, 0 );
c.setLocation( locX, locY );
}
});
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}