3

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).

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. The JPanels can be dragged around with the help of a MouseMotionListener.

To draw the connections, I override the drawing board paintComponent() method and call g.drawLine() (after a call to 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. 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.

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. Below is the MouseMotionListener of such a "block" JPanel.

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. It just contains some JLabels and JButtons. The buttons are used to create connections between blocks. The connection list is then used inside drawConnections mentioned above.

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

tenorsax
  • 21,123
  • 9
  • 60
  • 107
KlaasDC
  • 31
  • 3
  • hard to say anything, without see your code and what/how you are tried that – mKorbel Jun 29 '11 at 09:54
  • It does seem to work however if the window does not have the focus and another window is placed on top of it (overlap). Then the lines stay. – KlaasDC Jun 29 '11 at 10:13
  • why do you have `g.clearRect(0, 0, getWidth(), getHeight())` statement? – salman.mirghasemi Jun 29 '11 at 10:14
  • If I don't add a call to `clearRect()`, the old lines stay when moving a block. It looks like a mess then :) – KlaasDC Jun 29 '11 at 10:16
  • I guess there is some problem with your code which is not appeared here. Look at this code: http://download.oracle.com/javase/tutorial/uiswing/painting/refining.html . It does similar to what you did but it doesn't call `clearRect()`. – salman.mirghasemi Jun 29 '11 at 10:52
  • Ok I don't get it. I prepared a piece of code to put here by copy/pasting from my project. When I run it, it works fine... Rechecking everything now. – KlaasDC Jun 29 '11 at 11:29
  • @KlaasDC, You should never need to invoke getGraphics() on the Graphics object passed to your paintComponent() method. There is something else wrong with your code. – camickr Jun 29 '11 at 16:22
  • @camickr Yes, after replacing `getGraphics()` with the supplied `g` it works fine. – KlaasDC Jun 30 '11 at 06:59

2 Answers2

2

On approach might be to make the lines be JComponents that have been added to the panel and having them repaint themselves. 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.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • On second thought this is a good idea because it would make it alot easier to have complex connection shapes, not only straight lines. – KlaasDC Jun 29 '11 at 12:20
1

If JDesktopPane is an acceptable "drawing board," you could try the approach shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045