叠加的JPanels没有排队

时间:2018-04-28 15:46:57

标签: java swing jpanel

我正在尝试制作一个可绘制的JPanel,带有可选的网格线显示。为此,我有一个自定义JPanel,它使另一个JPanel只保存网格线。这样,我可以显示/隐藏网格线而无需移除画布上的内容。

所有似乎都在起作用,除了一些奇怪的对齐问题,我似乎可以确定。当我开始绘制一些正方形时,它们只比网格线高几个像素。我该如何解决这个问题,以便两个面板完全相互显示?

以下是此问题的示例: An Example of the grid

package com.carvethsolutions.guilib.customcomp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * A custom component for displaying a grid
 */
public class GridCanvas extends JPanel implements MouseListener {

/**
 * Width and height of the canvas, in pixels
 */
private int width, height;

/**
 * How many pixels represent one square on the grid
 */
private int gridScale;

/**
 * The separate panel that holds the grid lines
 */
private JPanel gridPanel;

private boolean gridLinesVisible = true;

/**
 * Holds color selections
 */
private Paintbrush paintbrush;

public GridCanvas() {
    super();
    width = 500;
    height = 500;

    setupComponent();
}

public GridCanvas(int width, int height) {
    super();
    this.width = width;
    this.height = height;

    setupComponent();
}

/**
 * Private function to prepare the component.
 */
private void setupComponent() {
    gridScale = 50;
    this.setPreferredSize(new Dimension(width,height));
    this.setBackground(Color.WHITE);

    this.addMouseListener(this);

    gridPanel = new JPanel() {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Vertical Lines
            for (int x = gridScale; x <= width; x += gridScale) {
                g.drawLine(x, 0, x, height);
            }

            for (int y = gridScale; y <= height; y += gridScale) {
                g.drawLine(0, y, width, y);
            }
        }
    };

    gridPanel.setVisible(gridLinesVisible);
    gridPanel.setPreferredSize(this.getPreferredSize());
    this.add(gridPanel);

    this.setSize(gridPanel.getSize());
    paintbrush = new Paintbrush(Color.black);
}

/**
 * Enable or disable grid lines from appearing on the Canvas
 */
public void toggleGridlines() {
    gridLinesVisible = !gridLinesVisible;
    gridPanel.setVisible(gridLinesVisible);
}


/**
 * Invoked when the mouse button has been clicked (pressed
 * and released) on a component.
 *
 * @param e
 */
@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX() / gridScale;
    int y = e.getY() / gridScale;

    System.out.println("mouseClicked Event : (" + x + ", " + y + ")");

    this.getGraphics().setColor(paintbrush.getColor());
    this.getGraphics().fillRect(x * gridScale, y * gridScale, gridScale, gridScale);
}

/**
 * Invoked when a mouse button has been pressed on a component.
 *
 * @param e
 */
@Override
public void mousePressed(MouseEvent e) {

}

/**
 * Invoked when a mouse button has been released on a component.
 *
 * @param e
 */
@Override
public void mouseReleased(MouseEvent e) {

}

/**
 * Invoked when the mouse enters a component.
 *
 * @param e
 */
@Override
public void mouseEntered(MouseEvent e) {

}

/**
 * Invoked when the mouse exits a component.
 *
 * @param e
 */
@Override
public void mouseExited(MouseEvent e) {

}
}

1 个答案:

答案 0 :(得分:2)

  

要做到这一点,我有一个自定义JPanel,它使另一个JPanel只保存网格线。这样,我可以显示/隐藏网格线而无需移除画布上的内容。

嗯,这是错误的做法。您的面板应具有属性:

  1. 是否绘制网格线
  2. 用于控制应填充哪个方块的2D数组。
  3. 您的绘画逻辑错误:

    this.getGraphics().setColor(paintbrush.getColor());
    this.getGraphics().fillRect(x * gridScale, y * gridScale, gridScale, gridScale);
    

    你不应该在组件上使用getGraphics(...)来进行自定义绘画。 Swing第一次确定需要重新绘制组件时,您将丢失所有绘画。例如,如果您调整框架的大小。

    相反。自定义绘画必须在组件的paintComponent()中完成。然后你:

    1. 首先根据需要绘制网格线
    2. 遍历2D数组并根据需要绘制任何方块。
    3. 阅读Custom Painting上Swing教程中的部分,了解更多信息和工作示例。

      或者使用ArrayList来包含要绘制的对象,而不是2D数组。该对象可以简单地是Point对象来控制要绘制的正方形的x / y位置,或者可以是包含关于要绘制的对象的完整信息的对象,例如位置/颜色/形状。查看Custom Painting Approaches以获取此方法的工作示例。