如何在JPanel上绘制网格线作为背景 [英] How to paint a gridline as a background on JPanel

查看:81
本文介绍了如何在JPanel上绘制网格线作为背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我在JFrame中使用了Gridbaglayout.组件之一是JPanel.

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

我想画一条网格线作为JPanel的背景.例如,在下面的程序中,它应该产生3条垂直线和3条水平线,但是只显示2条垂直线和2条水平线.最后一行没有显示.

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.

另一个问题是,JPanel的大小似乎比我设置的要大.我注意到这是因为行的长度比JPanel白色背景短.

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);
            }
    } 
}

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

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

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?

推荐答案

如果JPanel的大小为600,则其可用坐标仅从0到599.将不会绘制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);
   }
}

一些细节:

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

这篇关于如何在JPanel上绘制网格线作为背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆