[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] Re: Scrolling in a basic custom widget that extends Canvas

Chandresh,

I'm not really clear on what you're trying to do, but here's some
code I posted a while back for a similar sounding question. It 
paints a set of random rectangles in a larger "virtual" canvas 
and lets you scroll around the visible area. The key is the use
of the ScrolledComposite class from the custom package.

If this isn't what you needed then post some code to show us 
what you're trying to do.

Cheers,

Alun

  public static void main(String[] args)
  {
    Display display = new Display ();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Shell shell = new Shell (display);
    shell.setLayout(new FillLayout());

    int canvasWidth = 400;
    int canvasHeight = 400;

    ScrolledComposite sc1 = new ScrolledComposite(shell, 
                      SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    Canvas c1 = new Canvas(sc1, SWT.NONE);
    sc1.setContent(c1);

    c1.setBackground(red);
    c1.setSize(canvasWidth, canvasHeight);

    // Create some random rectangles
    java.util.Random rand = new java.util.Random();
    final Rectangle[] rects = new Rectangle[10];
    for (int i=0; i<rects.length; i++)
    {
      int x = rand.nextInt(canvasWidth/2);
      int y = rand.nextInt(canvasHeight/2);
      int w = canvasWidth/2 + rand.nextInt(canvasWidth/2) - x;
      int h = canvasHeight/2 + rand.nextInt(canvasHeight/2) - y;
      rects[i] = new Rectangle(x, y, w, h);
    }

    c1.addPaintListener(new PaintListener ()
    {
      public void paintControl(PaintEvent event)
      {
        GC gc = event.gc;
        gc.setForeground(blue);
        for (int i=0; i<rects.length; i++)
        {
          gc.drawRectangle(rects[i]);
        }
      }
    });

    shell.setSize(canvasWidth/2, canvasHeight/2);
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
   }


Chandresh Prakash wrote:

> I am trying to create a custom widget by extending Canvas.
> I am painting on the canvas and have both a paintlistener
> for the purpose and I have also overridden computeSize.
> But I am not able to get Scrollbars properly for the widget
> on setting a V_SCROLL | H_SCROLL style to it.
> The scrollbars do appear but on scrolling they do not show
> the painted area hidden from view if the display area is not
> enough.
> Any ideas ???

> Thanx in advance,
>  Chandresh