在JAVA中使用“createbufferstrategy()”时出错

时间:2015-11-15 16:41:22

标签: java swing

错误:

Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3998)
at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3972)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4495)
at java.awt.Component.createBufferStrategy(Component.java:3849)
at java.awt.Canvas.createBufferStrategy(Canvas.java:194)
at java.awt.Component.createBufferStrategy(Component.java:3773)
at java.awt.Canvas.createBufferStrategy(Canvas.java:169)
at KMap.draw(KMap.java:276)
at KMap.run(KMap.java:246)
at KMap.<init>(KMap.java:33)
at Driver.main(Driver.java:6)

我读了一些其他帖子说我必须将Canvas添加到我的JFrame中,但问题仍然存在。现在我只想显示黑屏。如果有人可以帮助我,我会很感激。感谢

完整代码:(滚动到问题的底部)

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;

public class KMap extends Canvas
{
    private ArrayList<String> variableNameList = new ArrayList<String>();
    private String [] characters = {"A","B","C","D","E","F","G","H","I","J"};
    private ArrayList<int[]> values = new ArrayList<int[]>();
    private int[][] cell, leftValues, topValues;
    private int leftValuesSize, topValuesSize;
    private Canvas c = new Canvas();
    private JFrame f;
    public KMap(int i) 
    {
        f = new JFrame();
        f.setTitle("Karnaugh Map for COMP 228");
        f.setLayout(new BorderLayout());
        updateVariables(i);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.add(c);
        run(i);
    }
    public void updateVariables(int numV)
    {


        /*
         * The characters[] String array
         * holds the first ten letters of the alphabet(Since
         * the maximum number of variables can be 10). This loop
         * adds the names of the variables that need to be stored
         * inside 'variableNameList'. Example: if number of variables
         * (numV) = 5, then variable name list consists of the first
         * five Strings of characters[], "A", "B", "C", "D", "E".
         */

        variableNameList.clear();
        for(int i = 0; i < numV; i++)
            variableNameList.add(characters[i]);
        System.out.println();


        /*
         * The below for loop creates a list of all possible combinations between
         * the variables. For example, if numV(number of variables
         * is 5, the number of combinations will be 25). It will
         * start from 0, ending at 24, and convert those integers
         * to their binary values to store in the array list
         * called 'values'. These binary values will be used
         * as our truth table to help out with the K-Map (although
         * the truth table will not be displayed).*/

        int combinations = (int) Math.pow(2,numV);
        System.out.println(combinations);
        System.out.println("New variables: " + numV);
        values.clear();

        for(int i = 0; i < combinations; i++)
        {
            int binary[] = new int[numV];
            if(i == 0)
                for(int j = 0; j < numV; j++)
                    binary[j] = 0;

            else
            {

                for(int z = 0; z < values.get(i-1).length; z++)
                    binary[z] = values.get(i-1)[z];

                for(int a = numV-1; a >= 0; a--)
                {
                    if(binary[a]==0)
                    {
                        binary[a]++;
                        break;
                    }
                    else
                        binary[a]=0;
                }
            }
            values.add(binary);

            for(int j = 0; j < values.get(i).length; j++)
                System.out.print(values.get(i)[j]);
            System.out.println();

        }

        createMapValues(numV);
    }

    public void createMapValues(int numV)
    {
        /*From the 'values' array list consisting of binary numbers
         * (stored as int[] types), and a list of names for variables
         * (within the 'variableNameList' array, topString will be the
         * variable names on the top, and leftString will the names to the left
         * Example: i have 5 variables. Top string = "ABC", left = "DE"
         * 
         * For the top values "ABC" can be, it will pull a list of combinations
         * from 'values'. In this case, since there are 3 values, it will pull out
         * 2^3. So: 000,001,010,011,100,101,110,111. Then assign those values
         * to the 2-D array for the top values, 'topValues[][]'. Same thing
         * for the left values.
         * 
         */
        String[] characterSet = new String[variableNameList.size()];
        for(int i = 0; i < variableNameList.size(); i++)
            characterSet[i] = variableNameList.get(i);

        JButton emptyButton = new JButton();

        String topString = "";
        String leftString = "";
        System.out.println(numV);
        int topCount = numV/2;
        int leftCount = numV-topCount;

        System.out.println(topCount);
        System.out.println(leftCount);
        for(int i = 0; i < topCount; i++)
        {
            topString+=characterSet[i];
        }
        for(int i = topCount; i < numV; i++)
        {
            leftString+=characterSet[i];
        }


        topValuesSize = topString.length();
         topValues = new int[(int) Math.pow(2, topString.length())][topValuesSize];
        leftValuesSize = leftString.length();
        leftValues = new int[(int) Math.pow(2, leftString.length())][leftValuesSize];

        for(int i = 0; i < topValues.length; i++)
            for(int j = 0; j < topValuesSize; j++)
                topValues[i][j] = values.get(i)[numV-topValuesSize+j];



        for(int i = 0; i < leftValues.length; i++)
            for(int j = 0; j < leftValuesSize; j++)
                leftValues[i][j] = values.get(i)[numV-leftValuesSize+j];



        System.out.println(topString + "=");
        for(int i = 0; i < topValues.length; i++)
        {
            for(int j = 0; j < topValuesSize; j++)
                System.out.print(topValues[i][j]);
            System.out.println();
        }

        System.out.println(leftString + "=");

        for(int i = 0; i < leftValues.length; i++)
        {
            for(int j = 0; j < leftValuesSize; j++)
                System.out.print(leftValues[i][j]);
            System.out.println();
        }
        cell = new int[topValues.length][leftValues.length];
        for(int i = 0; i < topValues.length; i++)
        {
            for(int j = 0; j < leftValues.length; j++)
                cell[0][j] = 0;
        }
    }

    private boolean running = false;
    private long second = 1000000000;
    private long fps = 60;
    private long frame = second/fps;
    private long frames = 0;
    private long seconds = 0;

    public void run(int numV) 
    {
        long now = System.nanoTime();
        long lastSecond = now;
        long lastFrame = now;
        long deltaSecond = now-lastSecond;
        long deltaFrame = now-lastFrame;
        running = true;
        while(true)
        {
            while(running)
            {

                now = System.nanoTime();
                deltaSecond = now-lastSecond;
                deltaFrame = now-lastFrame;
                if(deltaFrame>=frame)
                {
                    draw(numV);
                    f.pack();
                    lastFrame = now;
                    frames++;
                }
                if(deltaSecond>=second)
                {

                    lastSecond = now;
                    seconds++;
                    frames = 0;
                }
            }
        }
    }
    public void draw(int numV)
    {
        /*
         * 
         */
        int w = (leftValues.length + 2)*50;
        int topH = 50;
        int midH = (topValues.length + 2)*50;
        int botH = 50;
        int h = topH + midH + botH;

        setSize(w, h);
        BufferStrategy bs = getBufferStrategy();
        if(bs==null)
        {
            createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, w, h);
        g.dispose();
        bs.show();

    }
}

1 个答案:

答案 0 :(得分:1)

在KMap构造函数中,不添加否则未使用的Canvas,添加此项并在运行之前将帧设置为可见。

public KMap(int i) 
{
    f = new JFrame();
    f.setTitle("Karnaugh Map for COMP 228");
    f.setLayout(new BorderLayout());
    updateVariables(i);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.add(this);
    f.setVisible(true);
    run(i);
}