Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 #653601] Thu, 10 February 2011 11:35 Go to next message
vijaya is currently offline vijayaFriend
Messages: 2
Registered: February 2011
Location: Banglore
Junior Member
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();
}

}

Re: SWT ScrollBar problem [message #653776 is a reply to message #653601] Fri, 11 February 2011 06:43 Go to previous messageGo to next message
aviral is currently offline aviralFriend
Messages: 4
Registered: February 2011
Location: Bangalore
Junior Member

Hi

I am not too sure if you should continue this way for zooming as this can have a lot of flicker on screen because of low rates for frame per second generated by this approach.

My suggestions would be to see draw2D and how it helps for zooming or u can even try OpenGL stuff for higher fps.
Re: SWT ScrollBar problem [message #654213 is a reply to message #653601] Mon, 14 February 2011 15:30 Go to previous message
Eric Rizzo is currently offline Eric RizzoFriend
Messages: 3070
Registered: July 2009
Senior Member
You should ask on the SWT forum group; you're more likely to find expert
advice there.

Eric


On 2/10/11 6:35 AM, vijaya wrote:
> Hi I'm attaching my code also here. Can anybody help me please
> My mail id is mailto: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:Can't access Classes in default package
Next Topic:On-the-fly compilation is non-functional
Goto Forum:
  


Current Time: Fri Apr 26 23:38:40 GMT 2024

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

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

Back to the top