Guest User

Untitled

a guest
Jun 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. _shell.addListener (SWT.Resize, new Listener () {
  2. public void handleEvent (Event e)
  3. {
  4. int toolBarOffset = _toolBar.getSize().y;
  5.  
  6. Rectangle clientArea = _shell.getClientArea();
  7.  
  8. _scrollComposite.setBounds(0, toolBarOffset, clientArea.width, clientArea.height - toolBarOffset);
  9.  
  10. Point canvasSize = _canvas.getSize();
  11.  
  12. int canvasX = Math.max(0, (clientArea.width / 2) - (canvasSize.x / 2));
  13. int canvasY = Math.max(0, ((clientArea.height - toolBarOffset) / 2) - (canvasSize.y / 2));
  14.  
  15. _canvas.setLocation(canvasX, canvasY);
  16. }
  17. });
  18.  
  19. package test;
  20.  
  21. import org.eclipse.swt.SWT;
  22. import org.eclipse.swt.custom.ScrolledComposite;
  23. import org.eclipse.swt.events.ControlAdapter;
  24. import org.eclipse.swt.events.ControlEvent;
  25. import org.eclipse.swt.events.PaintEvent;
  26. import org.eclipse.swt.events.PaintListener;
  27. import org.eclipse.swt.graphics.Point;
  28. import org.eclipse.swt.graphics.Rectangle;
  29. import org.eclipse.swt.layout.FillLayout;
  30. import org.eclipse.swt.widgets.Canvas;
  31. import org.eclipse.swt.widgets.Composite;
  32.  
  33. import org.eclipse.swt.widgets.Display;
  34. import org.eclipse.swt.widgets.Shell;
  35.  
  36. public class Test {
  37.  
  38. public static void main(final String[] args) {
  39. final Display display = new Display();
  40. final Shell shell = new Shell(display);
  41.  
  42. shell.setLayout( new FillLayout() );
  43.  
  44. final ScrolledComposite sComp = new ScrolledComposite( shell, SWT.H_SCROLL | SWT.V_SCROLL );
  45. final Composite fillComp = new Composite( sComp, SWT.NONE );
  46.  
  47. sComp.setLayout( new FillLayout() );
  48.  
  49. final Canvas c = new Canvas( fillComp, SWT.DOUBLE_BUFFERED );
  50. c.addPaintListener( new PaintListener() {
  51. public void paintControl(PaintEvent e) {
  52. Point p = c.getSize();
  53. e.gc.setBackground( display.getSystemColor( SWT.COLOR_RED ));
  54. e.gc.fillRectangle( 0, 0, p.x, p.y );
  55.  
  56. e.gc.setBackground( display.getSystemColor( SWT.COLOR_BLUE ));
  57. e.gc.fillRectangle( p.x / 2 - 10, p.y / 2 - 10, 20, 20 );
  58. }
  59. });
  60.  
  61. c.setSize( 400, 400 );
  62. sComp.setContent( fillComp );
  63.  
  64. sComp.addControlListener( new ControlAdapter() {
  65. @Override
  66. public void controlResized(ControlEvent e) {
  67. Rectangle clientArea = sComp.getClientArea();
  68.  
  69. Point cSize = c.getSize();
  70. int fillX = Math.max( clientArea.width, cSize.x );
  71. int fillY = Math.max( clientArea.height, cSize.y );
  72. fillComp.setSize( fillX, fillY );
  73.  
  74. int cX = ( clientArea.width - cSize.x ) / 2;
  75. int cY = ( clientArea.height - cSize.y ) / 2;
  76.  
  77. sComp.setOrigin( -cX, -cY );
  78.  
  79. int locX = Math.max( cX, 0 );
  80. int locY = Math.max( cY, 0 );
  81. c.setLocation( locX, locY );
  82. }
  83. });
  84.  
  85. shell.open();
  86. shell.pack();
  87. while (!shell.isDisposed()) {
  88. if (!display.readAndDispatch()) {
  89. display.sleep();
  90. }
  91. }
  92. }
  93. }
Add Comment
Please, Sign In to add comment