Search in sources :

Example 6 with TruthTable

use of com.cburch.logisim.analyze.model.TruthTable in project logisim-evolution by reds-heig.

the class KarnaughMapPanel method computePreferredSize.

private void computePreferredSize() {
    Graphics g = getGraphics();
    TruthTable table = model.getTruthTable();
    String message = null;
    if (output == null) {
        message = Strings.get("karnaughNoOutputError");
    } else if (table.getInputColumnCount() > MAX_VARS) {
        message = Strings.get("karnaughTooManyInputsError");
    }
    if (message != null) {
        if (g == null) {
            tableHeight = AppPreferences.getScaled(AppPreferences.BoxSize);
            tableWidth = AppPreferences.getScaled(100);
        } else {
            FontMetrics fm = g.getFontMetrics(EntryFont);
            tableHeight = fm.getHeight();
            tableWidth = fm.stringWidth(message);
        }
        setPreferredSize(new Dimension(tableWidth, tableHeight));
        repaint();
        return;
    }
    if (g == null) {
        headHeight = 16;
        cellHeight = 16;
        cellWidth = 24;
    } else {
        FontMetrics headFm = g.getFontMetrics(HeaderFont);
        headHeight = headFm.getHeight();
        FontMetrics fm = g.getFontMetrics(EntryFont);
        cellHeight = fm.getAscent() + CELL_VERT_SEP;
        cellWidth = fm.stringWidth("00") + CELL_HORZ_SEP;
    }
    int rows = 1 << ROW_VARS[table.getInputColumnCount()];
    int cols = 1 << COL_VARS[table.getInputColumnCount()];
    tableWidth = headHeight + cellWidth * (cols) + 15;
    tableHeight = headHeight + cellHeight * (rows) + 15;
    if ((cols >= 4) && (rows >= 4)) {
        tableWidth += headHeight + 11;
    }
    if (cols >= 4) {
        tableHeight += headHeight + 11;
    }
    if (cols > 4) {
        tableHeight += headHeight + 11;
    }
    setPreferredSize(new Dimension(tableWidth, tableHeight));
    invalidate();
    repaint();
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) TruthTable(com.cburch.logisim.analyze.model.TruthTable) Dimension(java.awt.Dimension)

Example 7 with TruthTable

use of com.cburch.logisim.analyze.model.TruthTable in project logisim-evolution by reds-heig.

the class KarnaughMapPanel method paintComponent.

@Override
public void paintComponent(Graphics g) {
    /* Anti-aliasing changes from https://github.com/hausen/logisim-evolution */
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    super.paintComponent(g);
    TruthTable table = model.getTruthTable();
    int inputCount = table.getInputColumnCount();
    Dimension sz = getSize();
    String message = null;
    if (output == null) {
        message = Strings.get("karnaughNoOutputError");
    } else if (inputCount > MAX_VARS) {
        message = Strings.get("karnaughTooManyInputsError");
    }
    if (message != null) {
        g.setFont(HeaderFont);
        GraphicsUtil.drawCenteredText(g, message, sz.width / 2, sz.height / 2);
        return;
    }
    int left = computeMargin(sz.width, tableWidth);
    int top = computeMargin(sz.height, tableHeight);
    int x = left;
    int y = top;
    int rowVars = ROW_VARS[inputCount];
    int colVars = COL_VARS[inputCount];
    int rows = 1 << rowVars;
    int cols = 1 << colVars;
    g.setFont(HeaderFont);
    FontMetrics headFm = g.getFontMetrics(HeaderFont);
    for (int i = 0; i < inputCount; i++) {
        String header = model.getInputs().get(i);
        Boolean rotated = false;
        int middleOffset = (headFm.stringWidth(header) >> 1);
        int xoffset = headHeight + 11;
        int yoffset = headHeight + 11;
        switch(i) {
            case 0:
                if (inputCount == 1) {
                    rotated = false;
                    xoffset += cellWidth + cellWidth / 2;
                    yoffset = headFm.getAscent();
                } else {
                    rotated = true;
                    yoffset += (rows - 1) * cellHeight;
                    if (inputCount < 4)
                        yoffset += cellHeight / 2;
                    xoffset = headFm.getAscent();
                }
                break;
            case 1:
                if (inputCount == 2) {
                    rotated = false;
                    xoffset += cellWidth + cellWidth / 2;
                    yoffset = headFm.getAscent();
                } else if (inputCount == 3) {
                    rotated = false;
                    xoffset += 3 * cellWidth;
                    yoffset = headFm.getAscent();
                } else {
                    rotated = true;
                    xoffset += 4 * cellWidth + 11 + headFm.getAscent();
                    yoffset += 2 * cellHeight;
                    if (inputCount > 4) {
                        xoffset += 4 * cellWidth;
                    }
                }
                break;
            case 2:
                rotated = false;
                if (inputCount == 3) {
                    xoffset += 2 * cellWidth;
                    yoffset += 11 + 2 * cellHeight + headFm.getAscent();
                } else if (inputCount == 4) {
                    xoffset += 3 * cellWidth;
                    yoffset = headFm.getAscent();
                } else {
                    xoffset += 6 * cellWidth;
                    yoffset += 11 + 4 * cellHeight + headFm.getAscent();
                }
                break;
            case 3:
                rotated = false;
                if (inputCount == 4) {
                    xoffset += 2 * cellWidth;
                    yoffset += 11 + 4 * cellHeight + headFm.getAscent();
                } else {
                    xoffset += 4 * cellWidth;
                    yoffset = headFm.getAscent();
                }
                break;
            case 4:
                rotated = false;
                xoffset += 2 * cellWidth;
                yoffset += 11 + 4 * cellHeight + headFm.getAscent() + headHeight;
                break;
            default:
                break;
        }
        if (g instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2D) g.create();
            if (rotated) {
                g2.translate(xoffset + x, yoffset + y);
                g2.rotate(-Math.PI / 2.0);
                g2.drawString(header, -middleOffset, 0);
            } else
                g2.drawString(header, xoffset + x - middleOffset, yoffset + y);
            if (i == 4)
                g2.drawString(header, 4 * cellWidth + xoffset + x - middleOffset, yoffset + y);
        }
    }
    x += headHeight;
    y += headHeight;
    g.setFont(EntryFont);
    FontMetrics fm = g.getFontMetrics();
    int dy = (cellHeight + fm.getAscent()) / 2;
    x += 11;
    y += 11;
    /* Here the 0/1 labels are placed */
    switch(cols) {
        case 2:
            g.drawLine(x + cellWidth, y - 8, x + 2 * cellWidth, y - 8);
            g.drawLine(x + cellWidth, y - 9, x + 2 * cellWidth, y - 9);
            break;
        case 4:
            g.drawLine(x + 2 * cellWidth, y - 8, x + 4 * cellWidth, y - 8);
            g.drawLine(x + 2 * cellWidth, y - 9, x + 4 * cellWidth, y - 9);
            g.drawLine(x + cellWidth, y + 8 + rows * cellHeight, x + 3 * cellWidth, y + 8 + rows * cellHeight);
            g.drawLine(x + cellWidth, y + 9 + rows * cellHeight, x + 3 * cellWidth, y + 9 + rows * cellHeight);
            break;
        case 8:
            g.drawLine(x + cellWidth, y + 8 + rows * cellHeight + headHeight, x + 3 * cellWidth, y + 8 + rows * cellHeight + headHeight);
            g.drawLine(x + cellWidth, y + 9 + rows * cellHeight + headHeight, x + 3 * cellWidth, y + 9 + rows * cellHeight + headHeight);
            g.drawLine(x + 5 * cellWidth, y + 8 + rows * cellHeight + headHeight, x + 7 * cellWidth, y + 8 + rows * cellHeight + headHeight);
            g.drawLine(x + 5 * cellWidth, y + 9 + rows * cellHeight + headHeight, x + 7 * cellWidth, y + 9 + rows * cellHeight + headHeight);
            g.drawLine(x + 2 * cellWidth, y - 8, x + 6 * cellWidth, y - 8);
            g.drawLine(x + 2 * cellWidth, y - 9, x + 6 * cellWidth, y - 9);
            g.drawLine(x + 4 * cellWidth, y + 8 + rows * cellHeight, x + 8 * cellWidth, y + 8 + rows * cellHeight);
            g.drawLine(x + 4 * cellWidth, y + 9 + rows * cellHeight, x + 8 * cellWidth, y + 9 + rows * cellHeight);
            break;
    }
    switch(rows) {
        case 2:
            g.drawLine(x - 8, y + cellHeight, x - 8, y + 2 * cellHeight);
            g.drawLine(x - 9, y + cellHeight, x - 9, y + 2 * cellHeight);
            break;
        case 4:
            g.drawLine(x - 8, y + 2 * cellHeight, x - 8, y + 4 * cellHeight);
            g.drawLine(x - 9, y + 2 * cellHeight, x - 9, y + 4 * cellHeight);
            g.drawLine(x + cols * cellWidth + 8, y + cellHeight, x + cols * cellWidth + 8, y + 3 * cellHeight);
            g.drawLine(x + cols * cellWidth + 9, y + cellHeight, x + cols * cellWidth + 9, y + 3 * cellHeight);
            break;
    }
    int outputColumn = table.getOutputIndex(output);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int row = getTableRow(i, j, rows, cols);
            Entry entry = table.getOutputEntry(row, outputColumn);
            if (provisionalValue != null && row == provisionalY && outputColumn == provisionalX)
                entry = provisionalValue;
            if (entry.isError()) {
                g.setColor(ERROR_COLOR);
                g.fillRect(x + j * cellWidth, y + i * cellHeight, cellWidth, cellHeight);
                g.setColor(Color.BLACK);
            }
            g.drawRect(x + j * cellWidth, y + i * cellHeight, cellWidth, cellHeight);
            g.drawRect(x + j * cellWidth + 1, y + i * cellHeight + 1, cellWidth - 2, cellHeight - 2);
        }
    }
    g.drawRect(x, y, cols * cellWidth, rows * cellHeight);
    g.drawRect(x - 1, y - 1, cols * cellWidth + 2, rows * cellHeight + 2);
    List<Implicant> implicants = model.getOutputExpressions().getMinimalImplicants(output);
    if (implicants != null) {
        int index = 0;
        for (Implicant imp : implicants) {
            g.setColor(IMP_COLORS[index % IMP_COLORS.length]);
            paintImplicant(g, imp, x, y, rows, cols);
            index++;
        }
    }
    if (outputColumn < 0)
        return;
    g.setColor(Color.BLUE);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int row = getTableRow(i, j, rows, cols);
            if (provisionalValue != null && row == provisionalY && outputColumn == provisionalX) {
                String text = provisionalValue.getDescription();
                g.setColor(Color.ORANGE);
                g.drawString(text, x + j * cellWidth + (cellWidth - fm.stringWidth(text)) / 2, y + i * cellHeight + dy);
                g.setColor(Color.BLUE);
            } else {
                Entry entry = table.getOutputEntry(row, outputColumn);
                String text = entry.getDescription();
                g.drawString(text, x + j * cellWidth + (cellWidth - fm.stringWidth(text)) / 2, y + i * cellHeight + dy);
            }
        }
    }
}
Also used : Entry(com.cburch.logisim.analyze.model.Entry) Implicant(com.cburch.logisim.analyze.model.Implicant) FontMetrics(java.awt.FontMetrics) TruthTable(com.cburch.logisim.analyze.model.TruthTable) Dimension(java.awt.Dimension) Graphics2D(java.awt.Graphics2D)

Example 8 with TruthTable

use of com.cburch.logisim.analyze.model.TruthTable in project logisim-evolution by reds-heig.

the class TableTabCaret method selectAll.

void selectAll() {
    table.requestFocus();
    TruthTable model = table.getTruthTable();
    setCursor(model.getRowCount(), model.getInputColumnCount() + model.getOutputColumnCount(), false);
    setCursor(0, 0, true);
}
Also used : TruthTable(com.cburch.logisim.analyze.model.TruthTable)

Example 9 with TruthTable

use of com.cburch.logisim.analyze.model.TruthTable in project logisim-evolution by reds-heig.

the class TableTabClip method paste.

public void paste() {
    Clipboard clip = table.getToolkit().getSystemClipboard();
    Transferable xfer;
    try {
        xfer = clip.getContents(this);
    } catch (IllegalStateException | ArrayIndexOutOfBoundsException t) {
        // I don't know - the above was observed to throw an odd
        // ArrayIndexOutOfBounds
        // exception on a Linux computer using Sun's Java 5 JVM
        JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteSupportedError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    Entry[][] entries;
    if (xfer.isDataFlavorSupported(binaryFlavor)) {
        try {
            Data data = (Data) xfer.getTransferData(binaryFlavor);
            entries = new Entry[data.contents.length][];
            for (int i = 0; i < entries.length; i++) {
                Entry[] row = new Entry[data.contents[i].length];
                for (int j = 0; j < row.length; j++) {
                    row[j] = Entry.parse(data.contents[i][j]);
                }
                entries[i] = row;
            }
        } catch (UnsupportedFlavorException e) {
            return;
        } catch (IOException e) {
            return;
        }
    } else if (xfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String buf = (String) xfer.getTransferData(DataFlavor.stringFlavor);
            StringTokenizer lines = new StringTokenizer(buf, "\r\n");
            String first;
            if (!lines.hasMoreTokens())
                return;
            first = lines.nextToken();
            StringTokenizer toks = new StringTokenizer(first, "\t,");
            String[] headers = new String[toks.countTokens()];
            Entry[] firstEntries = new Entry[headers.length];
            boolean allParsed = true;
            for (int i = 0; toks.hasMoreTokens(); i++) {
                headers[i] = toks.nextToken();
                firstEntries[i] = Entry.parse(headers[i]);
                allParsed = allParsed && firstEntries[i] != null;
            }
            int rows = lines.countTokens();
            if (allParsed)
                rows++;
            entries = new Entry[rows][];
            int cur = 0;
            if (allParsed) {
                entries[0] = firstEntries;
                cur++;
            }
            while (lines.hasMoreTokens()) {
                toks = new StringTokenizer(lines.nextToken(), "\t");
                Entry[] ents = new Entry[toks.countTokens()];
                for (int i = 0; toks.hasMoreTokens(); i++) {
                    ents[i] = Entry.parse(toks.nextToken());
                }
                entries[cur] = ents;
                cur++;
            }
        } catch (UnsupportedFlavorException e) {
            return;
        } catch (IOException e) {
            return;
        }
    } else {
        JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteSupportedError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    TableTabCaret caret = table.getCaret();
    int c0 = caret.getCursorCol();
    int c1 = caret.getMarkCol();
    int r0 = caret.getCursorRow();
    int r1 = caret.getMarkRow();
    if (r0 < 0 || r1 < 0 || c0 < 0 || c1 < 0)
        return;
    TruthTable model = table.getTruthTable();
    int rows = model.getRowCount();
    int inputs = model.getInputColumnCount();
    int outputs = model.getOutputColumnCount();
    if (c0 == c1 && r0 == r1) {
        if (r0 + entries.length > rows || c0 + entries[0].length > inputs + outputs) {
            JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteEndError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
            return;
        }
    } else {
        if (r0 > r1) {
            int t = r0;
            r0 = r1;
            r1 = t;
        }
        if (c0 > c1) {
            int t = c0;
            c0 = c1;
            c1 = t;
        }
        if (r1 - r0 + 1 != entries.length || c1 - c0 + 1 != entries[0].length) {
            JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteSizeError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    for (int r = 0; r < entries.length; r++) {
        for (int c = 0; c < entries[0].length; c++) {
            if (c0 + c >= inputs) {
                model.setOutputEntry(r0 + r, c0 + c - inputs, entries[r][c]);
            }
        }
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Entry(com.cburch.logisim.analyze.model.Entry) StringTokenizer(java.util.StringTokenizer) TruthTable(com.cburch.logisim.analyze.model.TruthTable) Clipboard(java.awt.datatransfer.Clipboard)

Example 10 with TruthTable

use of com.cburch.logisim.analyze.model.TruthTable in project logisim-evolution by reds-heig.

the class TruthTableMouseListener method mousePressed.

public void mousePressed(MouseEvent event) {
    TruthTablePanel source = (TruthTablePanel) event.getSource();
    TruthTable model = source.getTruthTable();
    int cols = model.getInputColumnCount() + model.getOutputColumnCount();
    int rows = model.getRowCount();
    cellX = source.getOutputColumn(event);
    cellY = source.getRow(event);
    if (cellX < 0 || cellY < 0 || cellX >= cols || cellY >= rows)
        return;
    oldValue = source.getTruthTable().getOutputEntry(cellY, cellX);
    if (oldValue == Entry.ZERO)
        newValue = Entry.ONE;
    else if (oldValue == Entry.ONE)
        newValue = Entry.DONT_CARE;
    else
        newValue = Entry.ZERO;
    source.setEntryProvisional(cellY, cellX, newValue);
}
Also used : TruthTable(com.cburch.logisim.analyze.model.TruthTable)

Aggregations

TruthTable (com.cburch.logisim.analyze.model.TruthTable)10 Entry (com.cburch.logisim.analyze.model.Entry)3 Dimension (java.awt.Dimension)2 FontMetrics (java.awt.FontMetrics)2 Clipboard (java.awt.datatransfer.Clipboard)2 Implicant (com.cburch.logisim.analyze.model.Implicant)1 Graphics (java.awt.Graphics)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 StringTokenizer (java.util.StringTokenizer)1