1

我正在使用滚动窗格,其中有一个 JPanel,它绘制了一个正方形网格,这些正方形网格是 [][] 数组中的对象。

如果数组是 [83][81] 大小为 18^2 的矩形,它看起来像:http: //i.imgur.com/fwJ9LV6.png (注意网格边缘的白色边框)

但是,[84][82] 处的相同矩形:http: //i.imgur.com/PZ2rNY3.png(最后一行被切片)。

现在,我已经:

  • 将滚动窗格和 Jpanel 的首选大小和最大大小增加到远高于所需的大小。
  • 检查 JPanel 是可滚动对象的视口。

它并没有改变任何东西。

我的 Jpanel 课程是:

package BlastRadius;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

/**
 * Canvas Class Stuart Bradley 25-1-2013 Contains the paint component
 */
public class BlastRadiusCanvas extends JPanel {

    GridOfNodes grid = new GridOfNodes();

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints rh = g2d.getRenderingHints();
        rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(rh);

        //Draw space
        int pixelsAcross = 32;
        int pixelsDown = 0;
        int size = 18;
        for (int i = 0; i < grid.getRows(); i++) {
            for (int j = 0; j < grid.getColumns(); j++) {
                g2d.setColor(grid.getNodeGrid()[i][j].getColour());
                g2d.fillRect(pixelsAcross, pixelsDown, size, size);
                g2d.setColor(new Color(0, 0, 0));
                g2d.drawRect(pixelsAcross, pixelsDown, size, size);
                //Better Ovals maybe needed, try Ellipise2D class

                if (grid.getNodeGrid()[i][j].getHasOval() == true) {
                    g2d.setColor(new Color(255, 255, 255));
                    g2d.fillOval((pixelsAcross + (size / 2) - 1), (pixelsDown + (size / 2) - 1), size / 4, size / 4);
                }
                pixelsAcross += 18;
            }
            pixelsDown += 18;
            pixelsAcross = 32;
            //Draws gene string for first object in each row
            g2d.setColor(new Color(0, 0, 0));
            g2d.drawString(grid.getNodeGrid()[i][0].getGeneString(), 5, pixelsDown - 5);
        }
    }
}

滚动窗格和相关的,如果需要可以发布整个 GUI 类:

jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane1.setPreferredSize(new java.awt.Dimension(10000, 10000));
jScrollPane1.setViewportView(drawingJPanel);

//Grouping 

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

虽然我之前通过 RAD 环境和手动完成了 GUI,但可滚动界面对我来说有点新奇!

4

3 回答 3

5

BlastRadiusCanvas需要向滚动窗格提供有关它想要多大的信息。

在此处输入图像描述

根据我对您尝试做的事情的解释,尝试将以下内容添加到您的BlastRadiusCanvas

@Override
public Dimension getPreferredSize() {
    return new Dimension((grid.getColumns() * 18) + 64, (grid.getRows() * 18) + 1);
}

现在真的,你不应该依赖“魔术”数字,最好使用类似......

protected static final int GRID_SIZE = 18;
protected static final int H_GAP = 32;

@Override
public Dimension getPreferredSize() {
    return new Dimension((grid.getColumns() * GRID_SIZE) + (H_GAP * 2), (grid.getRows() * GRID_SIZE) + 1);
}

这将使您的paintComponent方法看起来更像...

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    RenderingHints rh = g2d.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHints(rh);

    //Draw space
    int pixelsAcross = H_GAP;
    int pixelsDown = 0;
    int size = GRID_SIZE;
    for (int i = 0; i < grid.getRows(); i++) {
        for (int j = 0; j < grid.getColumns(); j++) {
            g2d.setColor(grid.getNodeGrid()[i][j].getColour());
            g2d.fillRect(pixelsAcross, pixelsDown, size, size);
            g2d.setColor(new Color(0, 0, 0));
            g2d.drawRect(pixelsAcross, pixelsDown, size, size);
            //Better Ovals maybe needed, try Ellipise2D class

            if (grid.getNodeGrid()[i][j].getHasOval() == true) {
                g2d.setColor(new Color(255, 255, 255));
                g2d.fillOval((pixelsAcross + (size / 2) - 1), (pixelsDown + (size / 2) - 1), size / 4, size / 4);
            }
            pixelsAcross += size;
        }
        pixelsDown += size;
        pixelsAcross = H_GAP;
        //Draws gene string for first object in each row
        g2d.setColor(new Color(0, 0, 0));
        g2d.drawString(grid.getNodeGrid()[i][0].getGeneString(), 5, pixelsDown - 5);
    }
}
于 2013-01-29T03:13:31.360 回答
5

因为BlastRadiusCanvas不包含自己的组件,您可以覆盖getPreferredSize()以返回最佳大小以显示网格。有关更多详细信息,请参阅此相关答案。还可以考虑实现Scrollable接口以返回合适Dimension的 from getPreferredScrollableViewportSize()。图像似乎具有自然的单位和块大小。

于 2013-01-29T03:15:07.477 回答
0

万一其他人也有同样的问题-因为谷歌搜索数小时并没有为我解决这个问题。

检查您的 JFrame 首选尺寸。如果它太低,它会最大化您拥有的网格数量 - 或任何绘图空间。

于 2013-01-29T02:46:05.697 回答