Menu

Dynamic background coloring

2005-01-27
2013-05-29
  • Till Wimmer

    Till Wimmer - 2005-01-27

    Hi
    I'd like to do some dynamic background coloring in SpringLayout.
    So I override paint() from GraphDraw and do a call to super.paint() after coloring the background.

    But unfortunately GraphDraw.paint() seems to reset the background color every time to default gray, so i have to choose between no nodes or no background coloring :)

    Any hint?
    Thanx in advance
    Till

     
    • D. Fisher

      D. Fisher - 2005-01-28

      In Java -- not just GraphDraw -- paint() starts by painting the background, and then working forwrad. (This, btw, is why we override only paintComponent: it assures that we don't have to take responsibility for the other things on screen.)

      Fortunately, paint() respects JComponent settings. And setBackground is the method to set the background color.

      so call gd.setBackground( color )
      and enjoy.

      Danyel

       
    • Bjoern

      Bjoern - 2005-01-28

      I'm using the new graphdraw component, but this will also working for the old one.

      I implemented a custom background (I'm actually using a picture for background) by overriding the paintComponent-method. As this method resets the background, too, I just took the code from the original new graphdraw and put it into my paintComponent method. yes, it's awkward and I will have to update the class if JUNG changes but it works very well.

       
      • Till Wimmer

        Till Wimmer - 2005-01-29

        Hi
        OK, seems like this is the only way. As I said, I overwrote paint(), but this didn't do the job. I tried this with paintComponent, too, but only with calling super() before or after doing my backgrounding - no way :( Now I see that I obviously have to _replace_ the whole function as you did... Thank you for the hint!

         
        • D. Fisher

          D. Fisher - 2005-01-31

          Hm. So would it work if we did something like this? Add one more call to PluggableRenderer:

          setPrePaintFunction( PrePaintFunction ppf );

          where

          public interface PrePaintFunction {
             public void prePaint( Graphics g );
          }

          and paintComponent has a few lines added up front:

          public void paintComponent( Graphics g ) {
             if prePaintFunction != null
                prePaintFunction.prepaint( g );
            ...
          }

           
          • Till Wimmer

            Till Wimmer - 2005-01-31

            PLEASE HELP ME! :/

            I extended the interface and added the prepaint() at the very start of paintComponent()... But the vertices and edges are still painted a the gray default background! So my own background gets overwritten every time a node changes.
            I can never see nodes and my background together, only a flashing graph - looks awful :(

            bg
            Till

             
        • Till Wimmer

          Till Wimmer - 2005-01-31

          OK you can't find paintComponent in GraphDraw. but in VisualizationViewer, so I had to hack the whole library :(

          @offkey: I'd like to update the background everytime something's changing. There's user data for each vertex (temperature) which modulates the background color, and which is updated very often.

          bg
          Till

           
          • D. Fisher

            D. Fisher - 2005-01-31

            First, I obviously wasn't thinking much. This would actually take an incompatible break of the Renderer interface to put a setBackground into Renderer.

            Ick.

            So, yes, the place to get to work here is VisualizationViewer.

            What would you think of a VisualizationViewer.setBackgroundFunction() that worked pretty much the same way as the one before?

            Your background paint function would paint as a result of state

            ("setColor(theColorOf(MY_NUM_VERTICES))"

            and then, when your state changed, you'd just call GraphDraw.repaint().

            Let me play with this tonight and see what hacks I can work out. TWimmer, if you can email me the changes you've made ( offkey@sourceforge.net), I'll use them as a starting point.

            Danyel

             

Log in to post a comment.