2

I am trying to create a graph implementation where the graph is displayed on a JPanel.

I am adding and removing vertices without a problem (I am using more panels for these). The problem I am having is adding lines to represent the edges. I know I can use

contentPane.getGraphics().drawLine(x1, y1, x2, y2)

to add a line, but it leaves no way for me to delete that line later on. Any ideas?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
KristenApril
  • 263
  • 1
  • 3
  • 6
  • You Probably don't want to use `getGraphics()`, as noted [here](http://stackoverflow.com/a/8185020/230513) and [here](http://stackoverflow.com/a/7081362/230513). – trashgod Apr 23 '12 at 21:10

2 Answers2

4

GraphPanel uses a List<Edge> to model edges connecting the nodes of a graph.

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

I don't think that can be done, really... If you just paint the same line in the background color that will of course mess up any other things you have drawn that crosses that line...

The easiest solution would be to redraw the entire control without the line

Only thing I can think of would be if it was possible to create a transparent panel you put inside of the root panel in your control, where you draw that line, like using Layers in Photoshop or GIMP, but not sure about how/if this is possible in Swing.

If you are just trying to get a graph there are several good graph libraries out there...

Tor P
  • 1,351
  • 11
  • 9
  • The graph implementation really isn't the problem, it is just displaying it. But thanks ill give it a shot – KristenApril Apr 22 '12 at 23:05
  • This might be of interest: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html , you want to look at the Glass Pane section, only thing is though, this will show on top of any panel in your application – Tor P Apr 22 '12 at 23:12
  • ah, reading further in the link above there is something called JLayeredPane that can do the job when combined with using the setOpaque() method of JPanel (read more: http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html) – Tor P Apr 22 '12 at 23:17