java SWT ScrolledComposite在32768像素后切断画布生成的图像
因此,我编写了一个程序,根据给定的“模型”,它会生成一个高度为50像素、长度约为84600像素的水平“时间线”条。每个像素代表一秒,因为它在24小时内以秒为单位模拟事件
问题是,在32768像素之后,该条被切断
我读过一些解决方案,比如使用ScrolledComposite只显示画布的一部分,并在滚动条通过缓冲拖动时显示新数据,但我对如何实现这一点并不熟悉
我看到的另一个解决方案是不使用ScrolledComposite,而只是使用画布。滚动,如果我的源代码正在运行(说明我的问题的测试程序)。问题很明显,滚动条无法滚动以显示整个画布,此“解决方案”的测试程序如下所示。请帮忙
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强> 只是一个问题。。。这一行: 终点时间线尺寸=新点(84600,50)
那么,这是否意味着每个x轴像素都有一个“点”,但向下50个y轴像素?例如: ++++++++++
因此,每个“+符号”是一个水平x轴像素,84600个“点”是“周期”,如图所示,向下50个y轴像素。我对这一点的理解正确吗? (顺便说一句,上面的例子说明了10点)
你认为我做错了什么?或者我执行错误了
# 1 楼答案
使用
Canvas#scroll(..)
绝对是一种方法。我修正了你的例子,画了一个从0到84600的刻度,所以它高于32k的“物理”极限