我是编程新手,所以如果这是一个显而易见的问题,请原谅我,但我正在开发一个应用程序,除其他外,构建一个甘特图。
我遇到的问题是其中一个面板(namesPanel)显示相关图像,但顶部缺少大约10个像素。我可以通过在绘制时向y变量的起点添加10个像素来使图像看起来正确,但这感觉不对,如果有错误我现在想要压缩它。
图表实际上由嵌套在JPanel中的三个不同的JScrollPanes组成,并使用GridBagLayout排列:
datesPanel = new JScrollPane();
datesPane = new GanttPanel();
panelList.put(datesPanel, datesPane);
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.1;
c.weighty = 0.1;
add(datesPanel, c);
namesPanel = new JScrollPane();
namesPane = new GanttPanel();
panelList.put(namesPanel, namesPane);
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.18;
c.weighty = 1;
add(namesPanel, c);
ganttPanel = new JScrollPane();
ganttPane = new GanttPanel();
panelList.put(ganttPanel, ganttPane);
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
add(ganttPanel, c);
for (JScrollPane j : panelList.keySet()) {
GanttPanel g = panelList.get(j);
j.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
j.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
j.addMouseListener(listener);
LineBorder b = (LineBorder) BorderFactory.createLineBorder(DARK);
j.setBorder(b);
j.setViewportView(g);
}
namesImage = (BufferedImage) namesPane.createImage(canvasWidth, canvasDepth);
namesPane.setImage(namesImage);
namesGraphics = (Graphics2D) namesImage.getGraphics();
datesImage = (BufferedImage) datesPane.createImage(canvasWidth, canvasDepth);
datesPane.setImage(datesImage);
datesGraphics = (Graphics2D) datesImage.getGraphics();
ganttImage = (BufferedImage) ganttPane.createImage(canvasWidth, canvasDepth);
ganttPane.setImage(ganttImage);
graphics = (Graphics2D) ganttImage.getGraphics();
这样做的原因是允许用户使用鼠标滚动ganttChart,因此稍后在namesPanel和datesPanel的滚动条上设置为与ganttChart相同的JScrollBar实例。
然后程序继续调整所有图像的大小以节省内存 - 首先将它们设置为相同大小的原因是需要某些图形计算来设置最终图像的界限意味着创建一个图像来获取图形对象。另外,我不知道如何使图像更大:
/**
* Sets the boundaries of all the panels and moves them into position
*/
private void setBounds()
{
int xStart = canvasWidth - xOffset;
int yStart = canvasDepth - yOffset;
Container dad = getParent();
Rectangle dadRect = dad.getBounds();
Dimension ganttSize = new Dimension(dadRect.width - xOffset, dadRect.height - yOffset);
ganttImage = ganttImage.getSubimage(0, 0, xStart, yStart);
ganttPane.setPreferredSize(new Dimension(xStart, yStart));
ganttPanel.setPreferredSize(ganttSize);
datesImage = datesImage.getSubimage(0, 0, xStart, (yOffset + yUnit));
datesPane.setPreferredSize(new Dimension(xStart, yOffset));
datesPanel.setPreferredSize(new Dimension(xStart, yOffset));
namesImage = namesImage.getSubimage(0, 0, xOffset, yOffset);
namesPane.setPreferredSize(new Dimension(xOffset, yStart));
namesPanel.setPreferredSize(new Dimension(xOffset, yStart));
namesPanel.setBackground(DARK);
}
最后,程序实际上绘制了图像 - 我知道这个区域需要对子方法进行一些重构,但这里是:
/**
* Draw the bars of the Gantt Chart
*/
private void drawBars()
{
Calendar from = model.getFrom();
int i = 0;
for (int y = 0; y < canvasDepth && i < elements.size(); y += yUnit) {
GanttElement ge = elements.get(i);
//TODO refactor this section into multiple methods
int startPoint = 0;
int endPoint = 0;
try {
startPoint = (model.calculateRange(from, ge.getFrom()) * xUnit);
if (startPoint % 10 != 0) {
startPoint -= startPoint % 10;
}
endPoint = (model.calculateRange(from, ge.getTo()) * xUnit);
}
catch (IllegalArgumentException | BadDateException e) {
/*TODO add a badDateResponse method to the GanttElement interface
*so that the program can handle a bad date request appropriately
*/
System.out.println(e + " " + ge.getName());
}
for (int x = 0; x <= endPoint; x += xUnit) {
if (x == 0) {
namesGraphics.setColor(Color.WHITE);
namesGraphics.drawString(ge.getName(), (2 * getSeparatorX()), y);
}
if (i % 2 == 0) {
graphics.setColor(lightBlock);
}
else {
graphics.setColor(darkBlock);
}
if (x >= startPoint) {
GanttBlock block = new GanttBlock(x + (separatorX / 2), y, blockWidth, rowHeight, i);
blocks.add(block);
graphics.fill(block);
}
}
i++;
}
}
我很难过 - 我找不到导致问题的那条线,我看不出我处理namesPanel的方式与处理其他方式的方式有什么不同。更改影响图像放置和尺寸的字段也无济于事。也没有操纵JViewport。 :-(
再次道歉,如果这个太长,但我不知道要关注的代码区域。