简体   繁体   English

JPanel 之间的绘图

[英]Drawing between JPanels

I have to following situation:我必须以下情况:

A JPanel is used a "drawing board" where the user can add blocks that have specific connection points which can be used to interconnect to other blocks (think Simulink or labView). JPanel用作“绘图板”,用户可以在其中添加具有特定连接点的模块,这些连接点可用于与其他模块互连(想想 Simulink 或 labView)。

The blocks themselves are JPanel objects with buttons on them, that are added to the drawing board by the add() method after setting a null layout.块本身是带有按钮的JPanel对象,在设置 null 布局后通过 add() 方法添加到绘图板上。 The JPanels can be dragged around with the help of a MouseMotionListener . JPanels可以在MouseMotionListener的帮助下拖动。

To draw the connections, I override the drawing board paintComponent() method and call g.drawLine() (after a call to super.paintComponent ).为了绘制连接,我重写了绘图板的paintComponent()方法并调用 g.drawLine()(在调用super.paintComponent之后)。 This works, but as soon as you move a block, the lines overlap each other and it turns into a mess.这行得通,但是一旦你移动一个块,线条就会相互重叠,就会变得一团糟。 Therefore I call drawingBoard.repaint() during the time a user moves a block.因此,我在用户移动块期间调用drawingBoard.repaint() This has the effect that the lines are flickering visible during dragging and then disappear immediately.这样做的效果是线条在拖动过程中闪烁可见,然后立即消失。

Clearly, the drawing of the JPanels in the parent JPanel interferes with each other.显然,父JPanel中的JPanels的绘制会相互干扰。

How can I solve this?我该如何解决这个问题?

edit: Some snippets of the code:编辑:一些代码片段:

The drawing board:绘图板:

public void paintComponent(Graphics g){
    g.clearRect(0, 0, getWidth(), getHeight());
    super.paintComponent(g);
    drawConnections(g);//Contains g.drawLine calls
}

The blocks are added to the drawing board with the JPanel.add() method.使用 JPanel.add() 方法将块添加到绘图板上。 Below is the MouseMotionListener of such a "block" JPanel.下面是这样一个“块”JPanel 的 MouseMotionListener。

public void mouseDragged(MouseEvent e)
{
    pt = SwingUtilities.convertPoint(movingPanel, e.getX(), e.getY(), movingPanel.getParent());
    movingPanel.setBounds(pt.x - clickX, pt.y - clickY, movingPanel.getWidth(), movingPanel.getHeight());
    e.consume();

    movingPanel.getParent().repaint();
}

The block JPanel does not override paintComponent because no special drawing is necessary in it.块 JPanel 不会覆盖paintComponent,因为其中不需要特殊的绘图。 It just contains some JLabels and JButtons.它只包含一些 JLabels 和 JButton。 The buttons are used to create connections between blocks.这些按钮用于在块之间创建连接。 The connection list is then used inside drawConnections mentioned above.然后在上面提到的 drawConnections 中使用连接列表。

There's really not much more than this.真的没有比这更多的了。

SOLVED:解决了:

Ok, as expected this was a very small detail.好的,正如预期的那样,这是一个非常小的细节。

In the line drawing code I used在我使用的线条图代码中

Graphics2D g2 = (Graphics2D) this.getGraphics();

instead of代替

Graphics2D g2 = (Graphics2D) g;

I just noticed the references are not the same.我只是注意到参考文献不一样。 D'oh德欧

On approach might be to make the lines be JComponents that have been added to the panel and having them repaint themselves.一种方法可能是使这些行成为已添加到面板的 JComponents,并让它们自己重新绘制。 This might also have the nice effect of isolating the line logic and paint calculation in a line class instead of having it on your drawing board.这也可能具有在 class 行中隔离行逻辑和绘制计算的良好效果,而不是将其放在绘图板上。

If JDesktopPane is an acceptable "drawing board," you could try the approach shown here .如果JDesktopPane是可接受的“绘图板”,您可以尝试此处显示的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM