1

我是一个编程新手,所以如果这是一个明显的问题,请原谅我,但我正在开发一个应用程序,除其他外,构建甘特图。

我遇到的问题是其中一个面板(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();

这样做的原因是允许用户使用鼠标滚动甘特图,因此稍后将 namesPanel 和 datesPanel 的滚动条设置为与甘特图相同的 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 也不行。:-(

再次道歉,如果这太长了,但我不知道要关注代码的哪个区域。

4

0 回答 0