打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[SWT] Canvas scroll 滚动条
scroll(int destX, int destY, int x, int y, int width, int height, boolean all)
          //Scrolls a rectangular area of the receiver by first copying the source area to the destination and then causing the area of the source which is not covered by the destination to be repainted.
以origin(0,0)作为原点滚动,否则在滚动中会变形
public static void main (String [] args) {
>     Display display = new Display ();
>     Shell shell = new Shell (display);
>     shell.setLayout(new FillLayout());
>     final Point origin = new Point (0, 0);
>     final Canvas canvas = new Canvas (shell,SWT.V_SCROLL | SWT.H_SCROLL);
>     final int width = 500;
>     final ScrollBar hBar = canvas.getHorizontalBar ();
>     final Color red = new Color(null,255,0,0);
>     hBar.setMaximum(width);
>     hBar.addListener (SWT.Selection, new Listener () {
>
>         public void handleEvent (Event e) {
>         System.out.println("HBar listener");
>             int hSelection = hBar.getSelection ();
>             int destX = -hSelection - origin.x;
>             canvas.scroll (destX, 0, 0, 0, width, 100, true);
>             origin.x = -hSelection;
>             System.out.println("HBar listener exit");
>         }
>     });
>     final ScrollBar vBar = canvas.getVerticalBar ();
>     vBar.setMaximum(100);
>     vBar.addListener (SWT.Selection, new Listener () {
>         public void handleEvent (Event e) {
>             int vSelection = vBar.getSelection ();
>             int destY = -vSelection - origin.y;
>             canvas.scroll (0, destY, 0, 0, width, 100, true);
>             origin.y = -vSelection;
>         }
>     });
>     canvas.addListener (SWT.Paint, new Listener () {
>
>     boolean init = true;
>     public void handleEvent (Event e) {
>     System.out.println("Paint listener");
>
>             GC gc = e.gc;
>             gc.setBackground(red);
>                 gc.fillRectangle(origin.x,origin.y,width,100);
>                 gc.drawText ("test
", origin.x, origin.y);
>
>
>         }
>     });
>     shell.setSize (500, 150);
>     shell.open ();
>     while (!shell.isDisposed ()) {
>         if (!display.readAndDispatch ()) display.sleep ();
>     }
>     display.dispose ();
> }
要是用GC画图的话,一定要加上origin.x和origin.y. 这样才会在新的redraw()的图形中把矩形显示在相应位置
//x,y 矩形的起点坐标
gc.fillRectangle(origin.x+x,origin.y+y,595,842);
例如 设矩形起点(50,50)和origin(0,0), 滚动后origin点的坐标变为(0,-10)。这样origin.x+x,origin.y+y 后矩形的新的起点为(50,40),然后GC在新的起点(50,40)画出图形, 这样显示出来的矩形就向上移动了10个像素。(如果对起点的x,y的值进行运算,没有必要改动origin.x和origin.y,分开考虑。 )
比如画3个上下间隔为100的矩形
gc.fillRectangle(origin.x+x,origin.y+y,595,842);
y += 100;
gc.fillRectangle(origin.x+x,origin.y+y,595,842);
y += 100;
gc.fillRectangle(origin.x+x,origin.y+y,595,842);
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SWT 图形绘画接口详解 (SWT Graphics)
swt与awt/swing互嵌
JAVA.SWT/JFace: SWT高级控件之SWT系统资源
SWT 绘图技术
Taking a look at SWT Images
SWT 的字体(Font)、颜色(Color)、图像(Image)和绘图
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服