浏览代码

used better color function

Kolja Strohm 6 年之前
父节点
当前提交
da983bea7e

+ 1 - 6
src/bk/BlockCalc.java

@@ -1,6 +1,5 @@
 package bk;
 
-import java.awt.Color;
 import java.util.ArrayList;
 import java.util.Collections;
 
@@ -155,12 +154,10 @@ public class BlockCalc implements AlgorithmStage {
                         e.setConflicted( true, layout );
                     }
                     LayeredGraphNode oldAlignU = u.getAlign( layout );
-                    Color oldColorCurrent = current.getColor( layout );
                     LayeredGraphNode oldRootCurrent = current.getRoot( layout );
                     LayeredGraphNode oldAlignCurrent = current.getAlign( layout );
                     int oldR = r;
                     u.setAlign( current, layout );
-                    current.setColor( u.getRoot( layout ).getColor( layout ), layout );
                     current.setRoot( u.getRoot( layout ), layout );
                     current.setAlign( current.getRoot( layout ), layout );
                     r = calcBeforeLayerNodeIndex( graph.getContainedLayers().get( calcBeforeLayerIndex() ).indexOf( u ) ) + 1;
@@ -171,7 +168,6 @@ public class BlockCalc implements AlgorithmStage {
                         for( int i = 0; i < conflicts.size(); i++ )
                             conflicts.get( i ).setConflicted( oldConflicts.get( i ), layout );
                         uf.setAlign( oldAlignU, layout );
-                        current.setColor( oldColorCurrent, layout );
                         current.setRoot( oldRootCurrent, layout );
                         current.setAlign( oldAlignCurrent, layout );
                         r = oldR;
@@ -230,7 +226,7 @@ public class BlockCalc implements AlgorithmStage {
             case BREAKPOINT:
                 return StageStatus.BREAKPOINT;
             case UNFINISHED:
-                LayeredGraphNode current = graph.getContainedLayers().get( calcLayerIndex() ).get( nodeIndex );
+                LayeredGraphNode current = graph.getContainedLayers().get( calcLayerIndex() ).get( calcNodeIndex(  nodeIndex ) );
                 current.setSelected( layout );
                 if( breakpoint )
                     return StageStatus.BREAKPOINT;
@@ -246,7 +242,6 @@ public class BlockCalc implements AlgorithmStage {
             return status;
         LayeredGraphNode current = graph.getContainedLayers().get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) );
         current.setSelected( layout );
-        //current.update();
         if( !backwards.isEmpty() )
         {
             backwards.get( 0 ).reverse();

+ 1 - 1
src/bk/Combine.java

@@ -95,7 +95,7 @@ public class Combine implements AlgorithmStage {
             tblOffset = minX - calcMinX( LayoutType.TOP_BOTTOM_LEFT );
             btrOffset = minWidth - btrw;
             tbrOffset = minWidth - tbrw;
-            Color oldColor = graph.getColor( LayoutType.TOP_BOTTOM_LEFT );
+            Color oldColor = graph.getColor( LayoutType.COMBINED );
             if( oldColor == null )
                 graph.setColor( Color.BLACK, null );
                 actions.add( 0, () -> {

+ 5 - 1
src/graph/InitializeNodePositions.java

@@ -18,9 +18,13 @@ public class InitializeNodePositions {
      * @param graph the graph where the node positions are to be set.
      */
     public static void placeNodes( LayeredGraphNode graph ) {
+        float hue = 0; // color hue
+        float saturation = 1; // color saturation
+        float brightness = 1; // color brightness
         for( LayeredGraphNode n : graph.getContainedNodes() )
         {
-            n.setColor( new Color((int)(Math.random() * 0x1000000)), null );
+            n.setColor( Color.getHSBColor( hue, saturation, brightness ), null );
+            hue += 0.29f;
             placeNodes( n );
         }
         int curY = 0;

+ 1 - 1
src/graph/LayeredGraphNode.java

@@ -137,7 +137,7 @@ public interface LayeredGraphNode {
   public void setColor(Color c, LayoutType layout);
 
   public Color getColor(LayoutType layout);
-
+  public Color getClassColor( LayoutType layout );
   public void setSelected(LayoutType layoutType);
 
   public boolean isSelected(LayoutType layout);

+ 61 - 0
src/graph/LayeredNode.java

@@ -414,15 +414,76 @@ public class LayeredNode implements LayeredGraphNode {
             combined.color = c;
     }
     
+    private int calcClassSize( LayeredGraphNode sink, LayoutType layout )
+    {
+        if( parent == null )
+            return 1;
+        int ret = 0;
+        for( LayeredGraphNode n : parent.getContainedNodes() )
+        {
+            if( n.getRoot( layout ).getSink( layout ) == sink )
+                ret++;
+        }
+        return ret;
+    }
+    
+    @Override
+    public Color getClassColor( LayoutType layout )
+    {
+        if( layout == LayoutType.TOP_BOTTOM_LEFT && this.layouts[ 0 ].sink == this && calcClassSize( this, layout ) == 1 )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.TOP_BOTTOM_LEFT && this.layouts[ 0 ].sink == this )
+            return this.layouts[ 0 ].color;
+        else if( layout == LayoutType.TOP_BOTTOM_LEFT )
+            return this.layouts[ 0 ].sink.getClassColor( layout );
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT && this.layouts[ 1 ].sink == this && calcClassSize( this, layout ) == 1 )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT && this.layouts[ 1 ].sink == this )
+            return this.layouts[ 1 ].color;
+        else if( layout == LayoutType.TOP_BOTTOM_RIGHT )
+            return this.layouts[ 1 ].sink.getClassColor( layout );
+        if( layout == LayoutType.BOTTOM_TOP_LEFT && this.layouts[ 2 ].sink == this && calcClassSize( this, layout ) == 1 )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.BOTTOM_TOP_LEFT && this.layouts[ 2 ].sink == this )
+            return this.layouts[ 2 ].color;
+        else if( layout == LayoutType.BOTTOM_TOP_LEFT )
+            return this.layouts[ 2 ].sink.getClassColor( layout );
+        if( layout == LayoutType.BOTTOM_TOP_RIGHT && this.layouts[ 3 ].sink == this && calcClassSize( this, layout ) == 1 )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.BOTTOM_TOP_RIGHT && this.layouts[ 3 ].sink == this )
+            return this.layouts[ 3 ].color;
+        else if( layout == LayoutType.BOTTOM_TOP_RIGHT )
+            return this.layouts[ 3 ].sink.getClassColor( layout );
+        if( layout == LayoutType.COMBINED )
+            return combined.color;
+        return null;
+    }
+    
     @Override
     public Color getColor( LayoutType layout )
     {
+        if( layout == LayoutType.TOP_BOTTOM_LEFT && this.layouts[ 0 ].root == this && this.layouts[ 0 ].align == this )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.TOP_BOTTOM_LEFT && this.layouts[ 0 ].root != this )
+            return this.layouts[ 0 ].root.getColor( layout );
         if( layout == LayoutType.TOP_BOTTOM_LEFT )
             return this.layouts[ 0 ].color;
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT && this.layouts[ 1 ].root == this && this.layouts[ 1 ].align == this )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT && this.layouts[ 1 ].root != this )
+            return this.layouts[ 1 ].root.getColor( layout );
         if( layout == LayoutType.TOP_BOTTOM_RIGHT )
             return this.layouts[ 1 ].color;
+        if( layout == LayoutType.BOTTOM_TOP_LEFT && this.layouts[ 2 ].root == this && this.layouts[ 2 ].align == this )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.BOTTOM_TOP_LEFT && this.layouts[ 2 ].root != this )
+            return this.layouts[ 2 ].root.getColor( layout );
         if( layout == LayoutType.BOTTOM_TOP_LEFT )
             return this.layouts[ 2 ].color;
+        if( layout == LayoutType.BOTTOM_TOP_RIGHT && this.layouts[ 3 ].root == this && this.layouts[ 3 ].align == this )
+            return Color.LIGHT_GRAY;
+        if( layout == LayoutType.BOTTOM_TOP_RIGHT && this.layouts[ 3 ].root != this )
+            return this.layouts[ 3 ].root.getColor( layout );
         if( layout == LayoutType.BOTTOM_TOP_RIGHT )
             return this.layouts[ 3 ].color;
         if( layout == LayoutType.COMBINED )

+ 2 - 2
src/view/NodeView.java

@@ -116,9 +116,9 @@ public class NodeView extends JPanel {
         Border linebor = BorderFactory.createLineBorder(model.getColor( layout ), 5);
         if( model.getRoot( layout ) != model || model.getContainedNodes().size() != 0  )
             linebor.paintBorder( this, g2, 0, 0, (int)model.getWidth( layout )-1, (int)model.getHeight( layout )-1 );
-        if( layout != LayoutType.COMBINED && model.getRoot( layout ).getSink( layout ).getColor( layout ) != model.getColor( layout ))
+        if( layout != LayoutType.COMBINED )
         {
-            g.setColor( model.getRoot( layout ).getSink( layout ).getColor( layout ) );
+            g.setColor( model.getRoot( layout ).getClassColor( layout ) );
             if( model.getContainedNodes().size() == 0 )
             {
                 if( selected )