Skip to main content



      Home
Home » Newcomers » Newcomers » SWT ScrollBar problem(I'm creating a display,shell and canvas, drawing a rectangle and zooming it. After that I'm not able see the complete rectangle when scrolling down the scrollbar)
SWT ScrollBar problem [message #653602] Thu, 10 February 2011 06:36
Eclipse UserFriend
Hi I'm attaching my code also here. Can anybody help me please
My mail id is vijaya.pramila@gmail.com

My code is



import org.eclipse.swt.*;

import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class Snippet9 {
static double zoom = 1.0;

public static void main(String[] args) {
Display display = new Display();

final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Point origin = new Point(0, 0);
final Canvas canvas = new Canvas(shell, SWT.BORDER | SWT.H_SCROLL
| SWT.V_SCROLL);
canvas.setBackground(new Color(display, 255, 255, 255));
canvas.setSize(600, 500);
final Color cyan = display.getSystemColor(SWT.COLOR_CYAN);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.setBackground(cyan);
e.gc.drawRectangle(20, 20, (int) (200 * zoom),
(int) (200 * zoom));
e.gc.fillRectangle(20, 20, (int) (200 * zoom),
(int) (200 * zoom));
}
});
final ScrollBar hBar = canvas.getHorizontalBar();
hBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
int hSelection = hBar.getSelection();
int destX = -hSelection - origin.x;
Rectangle rect = canvas.getBounds();
/*
* canvas.getAlwaysShowScrollBars();
* canvas.setExpandHorizontal(true);
* canvas.setExpandVertical(true);
*/

canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);

origin.x = -hSelection;

}
});
final ScrollBar vBar = canvas.getVerticalBar();
vBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {

int vSelection = vBar.getSelection();
int destY = -vSelection - origin.y;
Rectangle rect = canvas.getBounds();

canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);

/*
* canvas.getAlwaysShowScrollBars();
* canvas.setExpandHorizontal(true);
* canvas.setExpandVertical(true);
*/
origin.y = -vSelection;
}
});
// treeCanvas.redraw();
canvas.addListener(SWT.Resize | SWT.Show, new Listener() {

public void handleEvent(Event e) {
Rectangle rect = canvas.getBounds();
Rectangle client = canvas.getClientArea();

hBar.setMaximum((int) ((rect.width) * zoom));
vBar.setMaximum((int) ((rect.height) * zoom));
hBar.setThumb(Math.min(rect.width, client.width));
vBar.setThumb(Math.min(rect.height, client.height));
int hPage = rect.width - client.width;
int vPage = rect.height - client.height;
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
if (hSelection >= hPage) {
if (hPage <= 0)
hSelection = 0;
shell.setVisible(true);
canvas.setVisible(true);
origin.x = -hSelection;
}
if (vSelection >= vPage) {
if (vPage <= 0)
vSelection = 0;
shell.setVisible(true);
canvas.setVisible(true);
canvas.dispose();
origin.y = -vSelection;
}
canvas.redraw();

}
});

Slider slider = new Slider(canvas, SWT.HORIZONTAL);
slider.setBounds(canvas.getBounds().width / 8, (int) (canvas
.getBounds().height / 1.1), 150, 20);
slider.setMinimum(10);
slider.setMaximum(110);
slider.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent e) {
Slider source = (Slider) e.getSource();
int value = source.getSelection();

zoom = ((value) / 46.0);

canvas.redraw();

shell.layout();

}

public void widgetDefaultSelected(SelectionEvent arg0) {

}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

}

Previous Topic:Helios configuration problem
Next Topic:working sets: projects appearing n times?
Goto Forum:
  


Current Time: Sat Nov 08 05:40:28 EST 2025

Powered by FUDForum. Page generated in 0.06054 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top