Resize SWT画布位置

Resize SWT画布位置,resize,canvas,swt,Resize,Canvas,Swt,我使用的是SWT的最新稳定版本 我有一个包含工具栏、ScrolledComposite和画布的Shell。画布被设置为ScrolledComposite的内容(例如ScrolledComposite.setContent(画布))。画布是用一个永远不会改变的特定大小创建的(例如,400 x 400)。而ScrolledComposite不断增长或收缩,以填充可用父shell的客户端区域 我有一个调整大小的监听器连接到父Shell,它尝试执行以下操作:a)如上所述增长ScrolledComposi

我使用的是SWT的最新稳定版本

我有一个包含工具栏、ScrolledComposite和画布的Shell。画布被设置为ScrolledComposite的内容(例如ScrolledComposite.setContent(画布))。画布是用一个永远不会改变的特定大小创建的(例如,400 x 400)。而ScrolledComposite不断增长或收缩,以填充可用父shell的客户端区域

我有一个调整大小的监听器连接到父Shell,它尝试执行以下操作:a)如上所述增长ScrolledComposite,b)在ScrolledComposite中水平和垂直居中画布(参见下面的示例代码)

这与Mac OS X上的工作原理完全相同,但在Windows上会触发调整大小事件,并正确计算新位置,但最终画布会恢复为0,0。另一个小信息是,如果不断调整窗口的大小,可以看到画布在闪烁,看起来就像在正确的位置短暂绘制一样

_shell.addListener (SWT.Resize, new Listener () {
    public void handleEvent (Event e)
    {
        int toolBarOffset = _toolBar.getSize().y;

        Rectangle clientArea = _shell.getClientArea();

        _scrollComposite.setBounds(0, toolBarOffset, clientArea.width, clientArea.height - toolBarOffset);

        Point canvasSize = _canvas.getSize();

        int canvasX = Math.max(0, (clientArea.width / 2) - (canvasSize.x / 2));
        int canvasY = Math.max(0, ((clientArea.height - toolBarOffset) / 2) - (canvasSize.y / 2));

        _canvas.setLocation(canvasX, canvasY);
    }
});

不确定您是否已经解决了这个问题,但问题是ScrolledComposite在调整内容大小时总是将内容设置为0/0。我不知道为什么你的方法可以在OSX上运行,我也没有测试我的示例,因为我这里没有Mac

解决方案是使用至少与ScrolledComposite中的客户端区域一样大的填充组合。在该填充中,您可以正确地将画布居中

我举了一个小例子,作为一个额外的好处,如果SC的客户区比画布小,它也会使画布居中(因为我首先想到的是你的问题:)

您可能需要做一些小的调整,我想OSX上的代码可能有一些小问题

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();
            }
        }   
    }
}

不确定您是否已经解决了这个问题,但问题是ScrolledComposite在调整内容大小时总是将内容设置为0/0。我不知道为什么你的方法可以在OSX上运行,我也没有测试我的示例,因为我这里没有Mac

解决方案是使用至少与ScrolledComposite中的客户端区域一样大的填充组合。在该填充中,您可以正确地将画布居中

我举了一个小例子,作为一个额外的好处,如果SC的客户区比画布小,它也会使画布居中(因为我首先想到的是你的问题:)

您可能需要做一些小的调整,我想OSX上的代码可能有一些小问题

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();
            }
        }   
    }
}