⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 circuitpainter.java~

📁 Java遗传算法库
💻 JAVA~
字号:
/*
 * CircuitPainter.java
 *
 * Created on 26 May 2002, 10:23
 */

package jaga.pj.circuits.fpgaft;

import java.awt.*;
import java.util.Vector;
import jaga.pj.circuits.*;

/**  For now only works with LUT circuits with 2 LUTins
 *
 * @author  Michael Garvie
 * @version 
 */
public class CircuitPainter implements java.io.Serializable {

    protected final int COL_DIS = 0;
    protected final int COL_EN = 1;
    protected final int COL_ELV = 2;
    protected final int COL_ON = 3;
    protected final int COL_WIRE = 4;
    protected final int COL_TEXT = 1;
    
    protected Color[] colours = { Color.lightGray, Color.black, Color.blue, Color.red, Color.green };
    protected Dimension elDim = new Dimension( 60, 60 );
    protected int ioBlobDiameter = elDim.width / 4;
    protected int leftPadding = ioBlobDiameter * 6;
    protected int topPadding = ioBlobDiameter * 1;
    protected int frameThickness = 4;
    protected String[] outputNames = null;
    protected String[] inputNames = null;
    
    protected java.util.Random rnd = new java.util.Random();
    
    /** Creates new CircuitPainter */
    public CircuitPainter () {
    }
    /** Creates new CircuitPainter */
    public CircuitPainter ( Dimension elDim ) {
        this.elDim = elDim;
        ioBlobDiameter = elDim.width / 6;
    }
    public CircuitPainter ( Dimension elDim, String[] outputNames ) {
        this( elDim );
        this.outputNames = outputNames;
    }
    public CircuitPainter ( Dimension elDim, String[] outputNames, String[] inputNames ) {
        this( elDim, outputNames );
        this.inputNames = inputNames;
    }
    /** Creates new CircuitPainter */
    public CircuitPainter ( Dimension elDim, Color[] colours ) {
        this( elDim );
        this.colours = colours;
    }
    
    public void paint( SimulatorLogicElement[][] inoutels, Graphics g )
    {
        //g.setFont( new java.awt.Font( "Arial", 0, 8 ) );
        
        SimulatorLogicElement[] ins = inoutels[ 0 ];
        SimulatorLogicElement[] outs = inoutels[ 1 ];
        SimulatorLogicElement[] els = inoutels[ 2 ];
        
        Dimension circuitDim = getCirDim( els.length );
        
        // 1. Draw grey LUTs underneath
        for( int i = 0; i < circuitDim.width; i++ )
        {
            for( int j = 0; j < circuitDim.height; j++ )
            {
                paintLUTFrame( new Point( i, j ), colours[ COL_DIS ], g );
            }
        }
        
        // 2. Recurse through connected LUTs
        drawInputs( ins, g );
        Vector added = new Vector();
        for( int ol = 0; ol < outs.length; ol++ )
        {
            Color thisOutCol = new Color( rnd.nextInt() );
            g.setColor( thisOutCol ); // Forever?
            drawOutput( ol, inoutels, circuitDim, g );
            g.setColor( thisOutCol ); // Forever?
            paintLUTFull( added, outs[ ol ], inoutels, circuitDim, thisOutCol, g );
        }
    }
    
    protected void drawInputs( SimulatorLogicElement[] inputs, Graphics g )
    {
        for( int il = 0; il < inputs.length; il++ )
        {
            Point realTL = getRealTL( new Point( -1, il ) );
            g.setColor( colours[ COL_EN ] );
            g.drawOval( realTL.x + elDim.width - ioBlobDiameter, realTL.y + elDim.height / 2 - ioBlobDiameter / 2, 
                            ioBlobDiameter, ioBlobDiameter );
            if( inputs[ il ].getNow() )
            {
                g.setColor( colours[ COL_ON ] );
                g.fillOval( realTL.x + elDim.width - ioBlobDiameter, realTL.y + elDim.height / 2 - ioBlobDiameter / 2, 
                            ioBlobDiameter, ioBlobDiameter );
            }
            g.setColor( colours[ COL_TEXT ] );
            String inputName = "I_" + il;
            if( inputNames != null )
            {
                inputName = inputNames[ il ];
            }
            g.drawString( inputName, realTL.x + elDim.width - ioBlobDiameter * 5, realTL.y + elDim.height / 2 + ioBlobDiameter / 2 );        
            
        }
    }
    
    protected void drawOutput( int ol, SimulatorLogicElement[][] inoutels, Dimension circuitDim, Graphics g )
    {
        SimulatorLogicElement currOut = inoutels[ 1 ][ ol ];
        Point thisOutVirPos = getVirPos( currOut, inoutels, circuitDim );
        Point thisOutRealTLPos = getRealTL( thisOutVirPos );
        Point realTL = getRealTL( new Point( circuitDim.width - 1, ol ) );
        g.drawLine( realTL.x + elDim.width + ioBlobDiameter, realTL.y + elDim.height / 2, 
                    thisOutRealTLPos.x + elDim.width, thisOutRealTLPos.y + elDim.height / 2 );
        
        g.setColor( colours[ COL_EN ] ); 
        g.drawOval( realTL.x + elDim.width + ioBlobDiameter, realTL.y + elDim.height / 2 - ioBlobDiameter / 2, 
                        ioBlobDiameter, ioBlobDiameter );
        if( currOut.getNow() )
        {
            g.setColor( colours[ COL_ON ] );
            g.fillOval( realTL.x + elDim.width + ioBlobDiameter, realTL.y + elDim.height / 2 - ioBlobDiameter / 2, 
                        ioBlobDiameter, ioBlobDiameter );
        }
        g.setColor( colours[ COL_TEXT ] );
        String outputName = "Q_" + ol;
        if( outputNames != null )
        {
            outputName = outputNames[ ol ];
        }
        g.drawString( outputName, realTL.x + elDim.width + ioBlobDiameter * 3, realTL.y + elDim.height / 2 + ioBlobDiameter / 2 );        
    }
        
    protected Point getVirPos( SimulatorLogicElement el, SimulatorLogicElement[][] inoutels, Dimension cirDim )
    {
        SimulatorLogicElement[] ins = inoutels[ 0 ];
        SimulatorLogicElement[] outs = inoutels[ 1 ];
        SimulatorLogicElement[] els = inoutels[ 2 ];
        int elpos = jaga.ESLib.indexOf( el, els );
        //System.out.println("elpos=" + elpos);
        Point rv;
        if( elpos > -1 )
        {
            rv = xlate( elpos, cirDim );
        }else
        {
            // Its an input
            elpos = jaga.ESLib.indexOf( el, ins );
            rv = new Point( -1, elpos );
            /*if( rnd.nextBoolean() )
            {
                rv = new Point( elpos, -1 ); // inputs along top
            }else
            {
                rv = new Point( elpos, cirDim.height );
            }*/
        }
        return rv;
    }
    
    protected void paintLUTFull( Vector added, SimulatorLogicElement el, SimulatorLogicElement[][] inoutels, Dimension cirDim, Color c, Graphics g )
    {
        //System.out.println("plf"+added.size());
        if( !added.contains( el ) )
        {
            added.add( el );
            SimulatorLogicElement[] ins = el.getInputs();
            if( ins != null )
            {
                Point virPos = getVirPos( el, inoutels, cirDim );
                //System.out.println("vp" + virPos );
                
                Point tlPos = getRealTL( virPos );
                boolean elVal = el.getNow();
                String elStr = generateLabel( el );
                Color frameColor = colours[ COL_EN ];
                if( elVal )
                {
                    frameColor = colours[ COL_ON ];
                }
                paintLUTFrame( virPos, frameColor, g );
                g.setColor( colours[ COL_ELV ] );
                g.drawString( elStr, tlPos.x + 10, tlPos.y + elDim.height / 2 );
                
                //Check if failing
                if( el instanceof SimulatorFaultyDelayLE )
                {
                    if( ( ( SimulatorFaultyDelayLE ) el).SSA )
                    {
                        // Draw Cross
                        paintCross( virPos, colours[ COL_EN ], g );
                    }
                }
                
                for( int il = 0; il < ins.length; il++ )
                {
                    paintConnect( el, ins[ il ], inoutels, il, cirDim, c, g );
                    paintLUTFull( added, ins[ il ], inoutels, cirDim, c, g );
                }
            }else
            {
                added.remove( el );
            }
        }
    }
    
    protected void paintConnect( SimulatorLogicElement src, SimulatorLogicElement dest, SimulatorLogicElement[][] inoutels, int inpix, Dimension cirDim, Color c, Graphics g )
    {
        Point sp = getVirPos( src, inoutels, cirDim );
        Point dp = getVirPos( dest, inoutels, cirDim );
        Point gsp = getRealTL( sp );
        Point gdp = getRealTL( dp );
        
        int inDivs = src.getInputs().length + 1;
        int inSlotSize = elDim.height / inDivs;
        
        int gsx = gsp.x; //**!! Change here if more lutins
        int gsy = gsp.y + inSlotSize + inpix * inSlotSize;
        int gdx = gdp.x;
        int gdy = gdp.y + elDim.height / 2;
        
        gdx += elDim.width;
        //gsx += elDim.width; // Before when was leftwards did this and not previous line
        //g.setColor( colours[ COL_WIRE ] );
        //g.setColor( new Color( rnd.nextInt() ) ) ;
        g.setColor( c );
        g.drawLine( gsx, gsy, gdx, gdy );
    }
    
    protected void paintLUTFrame( Point virPos, Color c, Graphics g )
    {
        Point realPos = getRealTL( virPos );
        g.setColor( c );
        for( int tl = 0; tl < frameThickness; tl++ )
        {
            g.drawRect( realPos.x + tl, realPos.y + tl, elDim.width - tl * 2, elDim.height - tl * 2 );
        }
    }

    protected void paintCross( Point virPos, Color c, Graphics g )
    {
        Point realPos = getRealTL( virPos );
        g.setColor( c );
        for( int tl = 0; tl < frameThickness; tl++ )
        {
            g.drawLine( realPos.x + tl, realPos.y, realPos.x + elDim.width, realPos.y + elDim.height - tl );
            g.drawLine( realPos.x + elDim.width, realPos.y + tl, realPos.x + tl, realPos.y + elDim.height );
        }
    }
    
    
    protected Point xlate( int ix, Dimension cirDim )
    {
        Point rv = new Point( cirDim.width - ix / cirDim.height - 1, ix % cirDim.height );
        return rv;
    }
    
    protected Point getRealTL( Point virPos )
    {
        return new Point( leftPadding + virPos.x * ( elDim.width + elDim.width/4 ) + elDim.width/4,
                          topPadding + virPos.y * ( elDim.height + elDim.height/4 ) + elDim.height/4 );
    }        

    protected Dimension getCirDim( int numEls )
    {
        final double root34 = 0.86602540378443864676372317075294;
        final double root43 = 1.1547005383792515290182975610039;
        double rootNumEls = Math.sqrt( numEls );
        int width = ( int )( root43 * rootNumEls );
        int height = ( int )( root34 * rootNumEls );
        while( width * height < numEls )
        {
            height++;
        }
        Dimension rv = new Dimension( width , height  );
        return rv;
    }
    
    protected int calculateLUTIns( SimulatorLogicElement el )
    {
        int rv = -1;
        if( el instanceof SimulatorLUT )
        {
            boolean[] table = ( ( SimulatorLUT ) el ).table;
            rv = jaga.ESLib.log2( table.length );
        }
        return rv;
    }
    
    protected String generateLabel( SimulatorLogicElement el )
    {
        String rv = "";
        if( el instanceof SimulatorLUT )
        {
            SimulatorLUT.mode = SimulatorLUT.PRINT_DESCRIPTION;
        }
        rv = el.toString();
        if( rv.length() > 10 )
        {
            rv = rv.substring( 0, 8 ) + "..";
        }
        return rv;
    }
    
    public SimulatorLogicElement getElementFromClick( SimulatorLogicElement[][] inoutels, int x, int y )
    {
        SimulatorLogicElement rv = null;
        y -= topPadding;
        int gridHeight = elDim.height + elDim.height / 4;
        int gridWidth = elDim.width + elDim.width / 4;
        
        if( y >= 0 )
        {
            y /= gridHeight;
            x -= leftPadding;
            if( x < 0 )
            {
                rv = inoutels[ 0 ][ y ];
            }else
            {
                Dimension circuitDim = getCirDim( inoutels[ 2 ].length );
                x /= gridWidth;
                x = circuitDim.width - x - 1;
                rv = inoutels[ 2 ][ x * circuitDim.height + y ];
            }
        }
        return rv;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -