如何在 JPanel 上绘制网格线作为背景

发布于 2024-08-14 15:47:40 字数 1168 浏览 3 评论 0原文

我有一个问题。我在 JFrame 中使用了 Gridbaglayout。 JPanel 就是其中之一。

我想绘制一条网格线作为我的 JPanel 的背景。例如,在下面的程序中,它应该生成 3 条垂直线和 3 条水平线,但它只显示 2 条垂直线和 2 条水平线。最后一行未显示。

另一个问题是,JPanel 的大小似乎比我设置的要大。我通过比 JPanel 白色背景短的线条长度注意到了这一点。

  public class drawLayout extends JComponent 
    {

 public Dimension getPreferredSize() { 
  return new Dimension(600, 600); 
 }

 public int getY() { 
  return 0; 
 } 

 public int getX() { 
   return 0; 
    }

    @Override public void paintComponent(Graphics g)
    {
     g.setPaint(Color.GRAY);

            for (int i = 0; i <= getSize().width; i += 300) 
            {
               g2.drawLine(i, 0, i, getSize().height);
            }

            for (int i = 0; i <= getSize().height; i += 300) 
            {
               g2.drawLine(0,i, getSize().width, i);
            }
    } 
}

编辑:

http://www.freeimagehosting.net/image.php?1af16edc28.jpg< /a>

第一个问题解决了(网格线显示在 JPanel 上)。 另一个问题让我困惑。正如您在附图中看到的,当查看网格的长度(标记为红色框)时,JPanel 的大小似乎超过 600。如何解决这个问题,使网格线背景看起来不错,而网格线外没有任何额外的空白?

I have a problem. I used Gridbaglayout in my JFrame. One of the component is JPanel.

I wanted to draw a gridline as a background for my JPanel. e.g in the program below, it supposed to produce 3 vertical and 3 horizontal lines, however it only shows 2 vertical and 2 horizontal line. the last line was not shown.

Another problem was that, it seems that the size of the JPanel was bigger that what I have set. I noticed this by the length of the line which is shorter that the JPanel white background.

  public class drawLayout extends JComponent 
    {

 public Dimension getPreferredSize() { 
  return new Dimension(600, 600); 
 }

 public int getY() { 
  return 0; 
 } 

 public int getX() { 
   return 0; 
    }

    @Override public void paintComponent(Graphics g)
    {
     g.setPaint(Color.GRAY);

            for (int i = 0; i <= getSize().width; i += 300) 
            {
               g2.drawLine(i, 0, i, getSize().height);
            }

            for (int i = 0; i <= getSize().height; i += 300) 
            {
               g2.drawLine(0,i, getSize().width, i);
            }
    } 
}

EDIT:

http://www.freeimagehosting.net/image.php?1af16edc28.jpg

The first problem solved (the gridlines were shown on JPanel).
The other problem puzzled me. As you can see on the image attached, the size of the JPanel seems to be more than 600 when look at the length of the grid (marked as red box). How can I solve this so the gridline background look nice without any extra white space outside the gridline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-08-21 15:47:40

如果您的 JPanel 大小为 600,则其可用坐标仅从 0 到 599。不会绘制 600 处的线。

此外,任何边框和/或插图都会进一步减少可用空间。

更新:我有一些时间,所以我写了你的申请。希望您会找到一些有用的提示。

public class Jessy extends JFrame {

   private static final int DRAWING_SIZE = 600;
   private static final int SUBDIVISIONS = 2;
   private static final int SUBDIVISION_SIZE = DRAWING_SIZE / SUBDIVISIONS;

   public Jessy() {
      setSize(800, 800);
      setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.gridx = 0;
      gbc.gridy = 0;
      JLabel drawingBoard = new JLabel("Drawing Board");
      gbc.anchor = GridBagConstraints.SOUTH;
      drawingBoard.setFont(new Font("Serif", Font.BOLD, 28));
      add(drawingBoard, gbc);
      JPanel panel = new JPanel() {
         @Override public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(Color.GRAY);
            for (int i = 1; i < SUBDIVISIONS; i++) {
               int x = i * SUBDIVISION_SIZE;
               g2.drawLine(x, 0, x, getSize().height);
            }
            for (int i = 1; i < SUBDIVISIONS; i++) {
               int y = i * SUBDIVISION_SIZE;
               g2.drawLine(0, y, getSize().width, y);
            }
         }          
      };
      panel.setPreferredSize(new Dimension(DRAWING_SIZE, DRAWING_SIZE));
      panel.setBackground(Color.WHITE);
      panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      gbc.gridy++;
      gbc.anchor = GridBagConstraints.CENTER;
      add(panel, gbc);
      JButton saveDrawing = new JButton("SAVE DRAWING");
      gbc.gridy++;
      gbc.anchor = GridBagConstraints.NORTH;
      add(saveDrawing, gbc);
   }
   public static void main(String[] args) {
      (new Jessy()).setVisible(true);
   }
}

一些细节:

  • 我使用了边框作为外线,这为我们节省了一些“599”的麻烦。
  • 我假设你想要在内部有一个细分的网格,所以我添加了一些管道以使其灵活且可配置。
  • 我注意到您的 paintComponents() 没有调用 super.paintComponents()。它应该!
  • 我使用了我认为所需的最低编码来指定网格袋约束。更少的编码 = 更少的 bug :)
  • 我对 JPanel 进行了子类化(根据您编写的内容),而不是 JComponent(根据您代码中的内容)。区别似乎在于 JComponent 无法绘制其背景,因此最终变成灰色。
  • 也许最重要的是,我用 kruler 测量了我的 GUI:绘图组件的大小正好是 600! :)

If your JPanel's size is 600, then its available coordinates only go from 0 to 599. The line at 600 isn't going to be drawn.

Also, any borders and/or insets would further reduce the available space.

Update: I had some time, so I wrote your application. Hopefully you will find some useful hints.

public class Jessy extends JFrame {

   private static final int DRAWING_SIZE = 600;
   private static final int SUBDIVISIONS = 2;
   private static final int SUBDIVISION_SIZE = DRAWING_SIZE / SUBDIVISIONS;

   public Jessy() {
      setSize(800, 800);
      setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.gridx = 0;
      gbc.gridy = 0;
      JLabel drawingBoard = new JLabel("Drawing Board");
      gbc.anchor = GridBagConstraints.SOUTH;
      drawingBoard.setFont(new Font("Serif", Font.BOLD, 28));
      add(drawingBoard, gbc);
      JPanel panel = new JPanel() {
         @Override public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(Color.GRAY);
            for (int i = 1; i < SUBDIVISIONS; i++) {
               int x = i * SUBDIVISION_SIZE;
               g2.drawLine(x, 0, x, getSize().height);
            }
            for (int i = 1; i < SUBDIVISIONS; i++) {
               int y = i * SUBDIVISION_SIZE;
               g2.drawLine(0, y, getSize().width, y);
            }
         }          
      };
      panel.setPreferredSize(new Dimension(DRAWING_SIZE, DRAWING_SIZE));
      panel.setBackground(Color.WHITE);
      panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      gbc.gridy++;
      gbc.anchor = GridBagConstraints.CENTER;
      add(panel, gbc);
      JButton saveDrawing = new JButton("SAVE DRAWING");
      gbc.gridy++;
      gbc.anchor = GridBagConstraints.NORTH;
      add(saveDrawing, gbc);
   }
   public static void main(String[] args) {
      (new Jessy()).setVisible(true);
   }
}

Some details:

  • I used a Border for the outside lines, this saves us a bit of trouble with "599".
  • I assumed you wanted a subdivided grid on the inside so I added some plumbing for making that flexible and configurable.
  • I noticed your paintComponents() doesn't call super.paintComponents(). It should!
  • I used what I think to be the minimum required coding to specify the gridbag constraints. Less coding = less bugs :)
  • I subclassed a JPanel (in accordance with what you wrote) rather than a JComponent (in accordance with what's in your code). The difference seems to be that JComponent isn't able to draw its background, so that ended up gray.
  • Perhaps most importantly, I measured my GUI with kruler: The drawing component is exactly 600 in size both ways! :)
痕至 2024-08-21 15:47:40

您将 i 增加 300。两次迭代后,i 为 600,然后退出 for 循环。
尝试将尺寸设置为 601 或增加 299。您还可以在比较中使用宽度+1 和高度+1。

You are incrementing i by 300. After two itterations, i is 600, and you exit the for loop.
Try setting your dimensions to 601 or increment by 299. You can also use width+1 and Height+1 in your comparison.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文