魔方模拟器烦恼



对于我的科学博览会项目,我正在制作一个魔方模拟器。我的代码出现逻辑错误,我无法弄清楚。我和爸爸已经仔细研究了几个小时,没有发现任何错误。我检查过,每次单击按钮时,操作侦听器仅调用一次图形类/方法。但是图形方法每次点击都会运行三次,我不知道为什么。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Simulator implements ActionListener
{
    int[] sideValues = new int[54];
    int pieceValue = 0;
    JFrame window = new JFrame();
    Container arrangement;
JPanel title = new JPanel();
JPanel leftButton = new JPanel();
JPanel rightButton = new JPanel();
JPanel cubeSpace = new JPanel();
JPanel info = new JPanel();
JPanel middleArea = new JPanel();
JPanel drawAndButtons = new JPanel();
JButton method1;
JButton method2;
JButton method3;
JButton Start;
JButton Stop;
JButton Reset;
int side = 2;
int worknow = 0;
int woyrAxis = 0;
int bogrAxis = 1;
int wbygAxis = 2;
int lastAxis = 1;
int count = 0;
int work = 0;
int amount = 0;
int loopNumDet = 0;
int rectX = 190, rectY = 100;
boolean firstRun = true;
@SuppressWarnings("serial")
public class graphics extends JComponent
{
    public void paint(Graphics page)
    {
        if(firstRun==true)
        {
            page.setColor(Color.blue);
            page.fillRect(190, 100, 50, 50);
            page.fillRect(250, 100, 50, 50);
            page.fillRect(310, 100, 50, 50);
            page.fillRect(190, 160, 50, 50);
            page.fillRect(250, 160, 50, 50);
            page.fillRect(310, 160, 50, 50);
            page.fillRect(190, 220, 50, 50);
            page.fillRect(250, 220, 50, 50);
            page.fillRect(310, 220, 50, 50);
            firstRun = false;
        }
        else
        {
            loopNumDet = side * 9;
            for(int i = loopNumDet - 9; i < loopNumDet; i++)
            {
                super.paint(page);
                work++;
                Stop.setText("" + work);
                pieceValue = sideValues[i];
                method3.setText("" + pieceValue);
                switch(pieceValue)
                {
                    case 1:
                        page.setColor(Color.yellow);
                        break;
                    case 2:
                        page.setColor(Color.blue);
                        break;
                    case 3:
                        page.setColor(Color.orange);
                        break;
                    case 4:
                        page.setColor(Color.green);
                        break;
                    case 5:
                        page.setColor(Color.red);
                        break;
                    case 6:
                        page.setColor(Color.white);
                        break;
                    default:
                        method2.setText("Color: " + pieceValue);
                }
                page.fillRect(rectX, rectY, 50, 50);
                rectX += 60;
                if(rectX == 370)
                {
                    rectX = 190;
                    rectY += 60;
                    if(rectY == 280)
                    {
                        rectX = 190;
                        rectY = 100;
                    }
                }
            }
            amount++;
            method1.setText("" + amount);
            if(amount > 1)
            {
                Start.setText("loop run" + amount);
            }
        }
    }
}
public class sideDet
{
    public void determineSide()
    {
        if(wbygAxis == 3)
            side = 1;
        else if(wbygAxis == 2)
            side = 2;
        else if(wbygAxis == 0 && woyrAxis == 2)
            side = 3;
        else if(wbygAxis == 4)
            side = 4;
        else if(wbygAxis == 0 && woyrAxis == 4)
            side = 5;
        else if(wbygAxis == 1)
            side = 6;
        else
            method3.setText("Failed sideDet");
    }
}
public class setAxis
{
    public void setAxes()
    {
        if(side == 1)
        {
            wbygAxis = 3;
            woyrAxis = 3;
            bogrAxis = 0;
        }
        else if(side == 2)
        {
            wbygAxis = 2;
            woyrAxis = 0;
            bogrAxis = 1;
        }
        else if(side == 3)
        {
            wbygAxis = 0;
            woyrAxis = 2;
            bogrAxis = 2;
        }
        else if(side == 4)
        {
            wbygAxis = 4;
            woyrAxis = 0;
            bogrAxis = 3;
        }
        else if(side == 5)
        {
            wbygAxis = 0;
            woyrAxis = 4;
            bogrAxis = 4;
        }
        else if(side == 6)
        {
            wbygAxis = 1;
            woyrAxis = 1;
            bogrAxis = 0;
        }
        else
            method3.setText("Failed setAxis");
    }
}
public Simulator()
{
    window.setSize(800,600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setTitle("Rubik's Cube Simulator");
    arrangement = window.getContentPane();
    arrangement.setLayout(new BorderLayout());
    title.setSize(800, 200);
    title.setBackground(Color.black);
    middleArea.setSize(800, 400);
    middleArea.setBackground(Color.black);
    info.setSize(800, 200);
    info.setBackground(Color.black);
    arrangement.add(title, BorderLayout.NORTH);
    arrangement.add(middleArea, BorderLayout.CENTER);
    arrangement.add(info, BorderLayout.SOUTH);
    JLabel name = new JLabel("Rubik's Cube Simulator by Erik Laucks", JLabel.CENTER);
    name.setFont(new Font("Times New Roman", Font.PLAIN, 28));
    name.setForeground(Color.white);
    title.add(name);
    JPanel b1 = new JPanel();
    JPanel b2 = new JPanel();
    JPanel b3 = new JPanel();
    JPanel b4 = new JPanel();
    JPanel b5 = new JPanel();
    info.setLayout(new GridLayout(1,5));
    info.add(b1);
    b1.setBackground(Color.black);
    info.add(b2);
    b2.setBackground(Color.black);
    info.add(b3);
    b3.setBackground(Color.black);
    info.add(b4);
    b4.setBackground(Color.black);
    info.add(b5);
    b5.setBackground(Color.black);
    //Bottom Row, Left Button
    b1.setLayout(new BorderLayout());
    JLabel fillb11 = new JLabel("     ");
    b1.add(fillb11, BorderLayout.WEST);
    JButton results = new JButton("Results");
    results.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    results.addActionListener(this);
    b1.add(results, BorderLayout.CENTER);
    JLabel fillb12 = new JLabel("     ");
    b1.add(fillb12, BorderLayout.EAST);
    //Bottom Row, Left Middle Button
    b2.setLayout(new BorderLayout());
    JLabel fillb21 = new JLabel("     ");
    b2.add(fillb21, BorderLayout.WEST);
    method1 = new JButton("Tri-Layer");
    method1.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    method1.addActionListener(this);
    b2.add(method1, BorderLayout.CENTER);
    JLabel fillb22 = new JLabel("     ");
    b2.add(fillb22, BorderLayout.EAST);
    //Bottom Row, Middle Button
    b3.setLayout(new BorderLayout());
    JLabel fillb31 = new JLabel("     ");
    b3.add(fillb31, BorderLayout.WEST);
    method2 = new JButton("Corner-Edge");
    method2.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    method2.addActionListener(this);
    b3.add(method2, BorderLayout.CENTER);
    JLabel fillb32 = new JLabel("     ");
    b3.add(fillb32, BorderLayout.EAST);
    //Bottom Row, Right Middle Button
    b4.setLayout(new BorderLayout());
    JLabel fillb41 = new JLabel("     ");
    b4.add(fillb41, BorderLayout.WEST);
    method3 = new JButton("Minh Thai");
    method3.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    method3.addActionListener(this);
    b4.add(method3, BorderLayout.CENTER);
    JLabel fillb42 = new JLabel("     ");
    b4.add(fillb42, BorderLayout.EAST);
    //Bottom Row, Right Label(Time)
    JLabel time = new JLabel("00:00", JLabel.CENTER);
    time.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    time.setForeground(Color.white);
    b5.add(time);

    middleArea.setLayout(new BorderLayout());
    JPanel leftButton = new JPanel();
    JPanel lbm = new JPanel();
    JLabel ltopfil = new JLabel(" ");
    ltopfil.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    leftButton.setLayout(new GridLayout(3,1));
    JPanel ltfil1 = new JPanel();
    JPanel ltfil2 = new JPanel();
    JLabel lmfil1 = new JLabel("     "); //Left Middle Filler 1(To center button)
    JLabel lmfil2 = new JLabel("     "); //Left Middle Filler 2
    JButton lview = new JButton("<");
    lview.setFont(new Font("Times New Roman", Font.PLAIN, 24));
    lview.addActionListener(this);
    lbm.add(lmfil1, BorderLayout.WEST);
    lbm.add(lmfil2, BorderLayout.EAST);
    lbm.add(lview, BorderLayout.CENTER);
    lbm.add(ltopfil, BorderLayout.NORTH);
    leftButton.add(ltfil1);
    leftButton.add(lbm);
    leftButton.add(ltfil2);
    leftButton.setBackground(Color.black);
    ltopfil.setBackground(Color.black);
    ltfil1.setBackground(Color.black);
    ltfil2.setBackground(Color.black);
    lmfil1.setBackground(Color.black);
    lmfil2.setBackground(Color.black);
    lbm.setBackground(Color.black);
    middleArea.add(leftButton, BorderLayout.WEST);

    JPanel rightButton = new JPanel();
    JPanel rbm = new JPanel();
    JLabel rtopfil = new JLabel(" ");
    rtopfil.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    rightButton.setLayout(new GridLayout(3,1));
    JPanel rtfil1 = new JPanel();
    JPanel rtfil2 = new JPanel();
    JLabel rmfil1 = new JLabel("     "); //Left Middle Filler 1(To center button)
    JLabel rmfil2 = new JLabel("     "); //Left Middle Filler 2
    JButton rview = new JButton(">");
    rview.setFont(new Font("Times New Roman", Font.PLAIN, 24));
    rview.addActionListener(this);
    rbm.add(rmfil1, BorderLayout.WEST);
    rbm.add(rmfil2, BorderLayout.EAST);
    rbm.add(rview, BorderLayout.CENTER);
    rbm.add(rtopfil, BorderLayout.NORTH);
    rightButton.add(rtfil1);
    rightButton.add(rbm);
    rightButton.add(rtfil2);
    rightButton.setBackground(Color.black);
    rtopfil.setBackground(Color.black);
    rtfil1.setBackground(Color.black);
    rtfil2.setBackground(Color.black);
    rmfil1.setBackground(Color.black);
    rmfil2.setBackground(Color.black);
    rbm.setBackground(Color.black);
    middleArea.add(rightButton, BorderLayout.EAST);

    cubeSpace.setLayout(new BorderLayout());
    JPanel topButton = new JPanel();
    topButton.setLayout(new GridLayout(1,3));
    JPanel tfil1 = new JPanel();
    JPanel tfil2 = new JPanel();
    JPanel tbutton = new JPanel();
    JPanel tlfil1 = new JPanel();
    JPanel tlfil2 = new JPanel();
    JButton tview = new JButton("^");
    tview.setFont(new Font("Times New Roman", Font.PLAIN, 24));
    tview.addActionListener(this);
    tbutton.setLayout(new GridLayout(1,3));
    tbutton.add(tlfil1);
    tbutton.add(tview);
    tbutton.add(tlfil2);
    topButton.add(tfil1);
    topButton.add(tbutton);
    topButton.add(tfil2);
    tfil1.setBackground(Color.black);
    tfil2.setBackground(Color.black);
    tlfil1.setBackground(Color.black);
    tlfil2.setBackground(Color.black);
    topButton.setBackground(Color.black);
    cubeSpace.add(topButton, BorderLayout.NORTH);
    middleArea.add(cubeSpace);

    JPanel PPS = new JPanel();
    PPS.setLayout(new GridLayout(1,3));
    Start = new JButton("Start");
    Stop = new JButton("Stop");
    Reset = new JButton("Reset");
    Start.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    Stop.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    Reset.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    Start.addActionListener(this);
    Stop.addActionListener(this);
    Reset.addActionListener(this);
    PPS.add(Start);
    PPS.add(Stop);
    PPS.add(Reset);
    PPS.setBackground(Color.black);
    cubeSpace.setBackground(Color.black);
    cubeSpace.add(PPS, BorderLayout.SOUTH);
    middleArea.setBackground(Color.black);
    drawAndButtons.setLayout(new BorderLayout());
    JPanel drawArea = new JPanel();
    JPanel bottomButton = new JPanel();
    bottomButton.setLayout(new GridLayout(1,3));
    JPanel bfil1 = new JPanel();
    JPanel bfil2 = new JPanel();
    JPanel bbutton = new JPanel();
    JPanel blfil1 = new JPanel();
    JPanel blfil2 = new JPanel();
    JButton bview = new JButton("\/");
    bview.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    bview.addActionListener(this);
    bbutton.setLayout(new GridLayout(1,3));
    bbutton.add(blfil1);
    bbutton.add(bview);
    bbutton.add(blfil2);
    bottomButton.add(bfil1);
    bottomButton.add(bbutton);
    bottomButton.add(bfil2);
    bfil1.setBackground(Color.black);
    bfil2.setBackground(Color.black);
    blfil1.setBackground(Color.black);
    blfil2.setBackground(Color.black);
    bottomButton.setBackground(Color.black);
    drawArea.setBackground(Color.black);
    drawAndButtons.setBackground(Color.black);
    drawAndButtons.add(drawArea, BorderLayout.NORTH);
    drawAndButtons.add(bottomButton, BorderLayout.SOUTH);
    cubeSpace.add(drawAndButtons, BorderLayout.CENTER);
    for(count = 0; count < 54; count++)
    {
        if(count < 9)
        {
            sideValues[count] = 1;
        }
        else if(count >= 9 && count < 18)
        {
            sideValues[count] = 2;
        }
        else if(count >= 18 && count < 27)
        {
            sideValues[count] = 3;
        }
        else if(count >= 27 && count < 36)
        {
            sideValues[count] = 4;
        }
        else if(count >= 36 && count < 45)
        {
            sideValues[count] = 5;
        }
        else
        {
            sideValues[count] = 6;
        }
    }
    worknow++;
    Reset.setText("" + worknow);
    drawAndButtons.add(new graphics());
    window.setVisible(true);
}
public static void main(String [] args)
{
    new Simulator();
}   
public static void L()
{
}
@Override
public void actionPerformed(ActionEvent arg0)
{
    if(((JButton) arg0.getSource()).getText().equals("^"))
    {
        if(side == 2 || side == 3 || side == 4 || side == 5)
        {
            side = 6;
            new setAxis();
        }
        else if(side == 1 || side == 6)
        {
            if(lastAxis == 1)
            {
                wbygAxis -= 1;
                if(wbygAxis == 0)
                    wbygAxis = 4;
                //method1.setText("" + wbygAxis);
            }
            else if(lastAxis == 2)
            {
                woyrAxis -= 1;
                if(woyrAxis == 0)
                    woyrAxis = 4;
            }
            new sideDet();
            new setAxis();
        }
        drawAndButtons.add(new graphics());
        cubeSpace.repaint();
    }
    else if(((JButton) arg0.getSource()).getText().equals("\/"))
    {
        if(side == 2 || side == 3 || side == 4 || side == 5)
        {
            side = 1;
            new setAxis();
        }
        else if(side == 1 || side == 6)
        {
            if(lastAxis == 1)
            {
                wbygAxis += 1;
                if(wbygAxis == 5)
                    wbygAxis = 1;
            }
            if(lastAxis == 2)
            {
                woyrAxis += 1;
                if(woyrAxis == 5)
                    woyrAxis = 1;
            }
            new sideDet();
            new setAxis();
        }
        drawAndButtons.add(new graphics());
        cubeSpace.repaint();
    }
    /*else if(((JButton) arg0.getSource()).getText().equals("<"))
    {
        if(side == 2 || side == 3 || side == 4 || side == 5)
        {
            side -= 1;
            if(side == 1)
                side = 5;
            new setAxis();
        }
        else if(side == 1 || side == 6)
        {
            if(lastAxis == 1)
            {
                if(side == 2)
                wbygAxis = 0;
            }
        }
    }
    else if(((JButton) arg0.getSource()).getText().equals(">"))
    {
    }*/
    else if(((JButton) arg0.getSource()).getText().equals("Tri-Layer"))
    {
        method1.setForeground(Color.red);
        method2.setForeground(Color.black);
        method3.setForeground(Color.black);
        firstRun = true;
        drawAndButtons.add(new graphics());
        cubeSpace.repaint();
    }
    else if(((JButton) arg0.getSource()).getText().equals("Corner-Edge"))
    {
        method1.setForeground(Color.black);
        method2.setForeground(Color.red);
        method3.setForeground(Color.black);
        firstRun = true;
        drawAndButtons.add(new graphics());
        cubeSpace.repaint();
    }
    else if(((JButton) arg0.getSource()).getText().equals("Minh Thai"))
    {
        method1.setForeground(Color.black);
        method2.setForeground(Color.black);
        method3.setForeground(Color.red);
        firstRun = true;
        drawAndButtons.add(new graphics());
        cubeSpace.repaint();
    }
}

}

旁注:你的图形类应该被调用Graphics以符合Java风格的准则,或者更好的是有意义的东西,如CubeDisplay

每当系统需要重新绘制部分或全部组件时,系统都可以调用 paint() 方法任意次数。除了重绘所需的逻辑之外,您不应该有任何逻辑。例如,如果您调整窗口大小、在其上移动另一个窗口等,则会调用paint()

解决方法不是担心为什么它被调用三次,而是确保它无关紧要!

顺便说一下,你最好使用 paintComponent() 而不是 paint()。paint 的默认实现为您处理边框、子元素等的绘制。

相关内容

  • 没有找到相关文章

最新更新