簡體   English   中英

SWT ScrolledComposite在32768像素之后切斷畫布生成的圖像

[英]SWT ScrolledComposite cuts off canvas generated images after 32768 pixels

所以我寫了一個程序,根據給定的'模型',它生成一個水平'時間軸'欄,高度為50像素,長度約為84600像素。 每個像素代表一秒,因為它在24小時內以秒為單位建模事件。

問題是,在32768像素之后,條被切斷。

我已經閱讀過諸如使用ScrolledComposite僅顯示部分畫布並在顯示新數據時進行滾動的解決方案,因為滾動條是通過緩沖拖動完成的,但我根本不熟悉如何執行此操作。

我看到的另一個解決方案是沒有使用ScrolledComposite,只是使用canvas.scroll,如果我的源代碼運行(測試程序來說明我的問題),問題顯然是滾動條不滾動以允許顯示整個畫布,這個“解決方案”的測試程序如下所示。 請幫忙!

package canvas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;

public class Test {
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.H_SCROLL;
static int canvasStyle = SWT.NO_REDRAW_RESIZE;// | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display, shellStyle);
    shell.setLayout(new FillLayout());
    shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
    shell.setText("Canvas Test");
    Image image;

    final Canvas canvas = new Canvas(shell, canvasStyle);       
    canvas.setLayout(new FillLayout());
    canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

    final Point origin = new Point(0,0);
    final ScrollBar hBar = shell.getHorizontalBar();
    Rectangle size = canvas.getBounds();
    hBar.setMaximum(size.width);
    hBar.setMinimum(0);

    // Create a paint handler for the canvas
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        // Do some drawing
          e.gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_YELLOW));
          e.gc.fillRectangle(100, 200, 100, 200);

          e.gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN));
          e.gc.fillRectangle(900, 200, 600, 200);

          e.gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
          e.gc.fillRectangle(500, 200, 300, 200);   

          e.gc.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
          e.gc.fillRectangle(1600, 200, 300, 200);  
      }

    });

 // The below event handlers allow for horizontal scrolling functionality
    hBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            int x = 0;
            int hSelection = hBar.getSelection();
            int destX = -hSelection - origin.x;
            Rectangle rect = shell.getBounds();
            canvas.scroll(destX, 0, x, 0, rect.width, rect.height, false);
            origin.x = -hSelection;     
            x = destX;
        }

    });

    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
          Rectangle rect = canvas.getClientArea();
          Rectangle client = shell.getClientArea();
          hBar.setMaximum(rect.width);
          hBar.setThumb(Math.min(rect.width, client.width));
          int hPage = rect.width - client.width;
          int hSelection = hBar.getSelection();
          if (hSelection >= hPage) {
            if (hPage <= 0)
              hSelection = 0;
            origin.x = -hSelection;
          }
          shell.redraw();
        }
      });

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

}
}

編輯:嘿謝謝p12t! 只是一個問題......這一行:final point timelineSize = new Point(84600,50);

那么這是否意味着每個x軸像素都有一個“點”但50個y軸像素下降? 如:++++++++++++++++++

因此每個“+符號”是一個水平的x軸像素,而84600'點'是'周期',如圖所示,50個y軸像素向下。 我對此的理解是否正確? (順便說一句,我上面顯示的例子說明了10分)

另外在你看來我做錯了什么? 或者我錯誤地實施了它..

使用Canvas#scroll(..)絕對是可行的方法。 我修改了你的例子來繪制從0到84600的比例,所以它超過了32k的“物理”限制。

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;

public class Test {
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL; // | SWT.V_SCROLL;

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
    shell.setText("Canvas Test");

    final Canvas canvas = new Canvas(shell, canvasStyle);       
    canvas.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
    canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

    final Point timelineSize = new Point(84600, 50);
    final Point offset = new Point(0,0);
    final ScrollBar hBar = canvas.getHorizontalBar();

    // Create a paint handler for the canvas
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        for (int x = 100; x < timelineSize.x; x += 100)
        {
          e.gc.drawLine(x + offset.x, 0, x + offset.x, 20);
          e.gc.drawText(Integer.toString(x), x + offset.x, 30, true);
        }
      }
    });

 // The below event handlers allow for horizontal scrolling functionality
    hBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            int hSelection = hBar.getSelection();
            int destX = -hSelection - offset.x;
            canvas.scroll(destX, 0, 0, 0, timelineSize.x, timelineSize.y, false);
            offset.x = -hSelection;     
        }
    });

    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
          Rectangle client = canvas.getClientArea();
          hBar.setMaximum(timelineSize.x);
          hBar.setThumb(Math.min(timelineSize.x, client.width));
          int hPage = timelineSize.y - client.width;
          int hSelection = hBar.getSelection();
          if (hSelection >= hPage) {
            if (hPage <= 0)
              hSelection = 0;
            offset.x = -hSelection;
          }
          shell.redraw();
        }
      });

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

  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM