2017-04-08 3 views
1

스윙을 사용하여 퐁 게임을 만들려고합니다. 음악, 게임 속도와 같은 환경 설정을 변경할 수있는 메뉴 화면이 있으며 멀티 플레이어 옵션 등을 선택할 수 있습니다. 그러나 선택했을 때 값은 변경되지 않습니다. 나는 메인 메뉴 클래스와 컴퓨터 클래스를 가지고있다.퐁 게임에서 선택된 변수가 변경되지 않습니다 - 스윙

예 : AI의 속도를 변경한다는 의미의 어려움을 변경하고 싶지만 그럴 수 없습니다. 나는 대답을 고대하고있다. 어쩌면 너무 단순하지만 나는 그것을 볼 수 없다.

public class MenuPanel 
{ 
    ScoreBoard sc = new ScoreBoard(); 
    private JFrame mainFrame; 
    private JFrame optionsFrame; 
    private JPanel menuPanel; 
    private JPanel optionsPanel; 
    private JFrame gameEndFrame; 
    private JPanel gameEndPanel; 
    JCheckBox twoPlayer; 

    // creating the swing components and containers, 
    // there are two frames to swap between them, 
    // we tried to use cardLayout but it didn't work for us. 

    public MenuPanel() { 
     mainFrame = new JFrame("Welcome To Pong Game"); 
     mainFrame.setSize(700,700); 
     mainFrame.setLayout(new CardLayout()); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     menuPanel = new JPanel(null); 
     menuPanel.setSize(700,700); 
     menuPanel.setVisible(true); 
     mainFrame.add(menuPanel); 

     optionsFrame = new JFrame("Settings"); 
     optionsFrame.setSize(700, 700); 
     optionsFrame.setLayout(new CardLayout()); 
     optionsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel optionsPanel = new JPanel(null) ; 
     optionsPanel.setSize(700,700); 
     optionsPanel.setVisible(true); 
     optionsFrame.add(optionsPanel); 

     // mainPanel components 
     ImageIcon ic1 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Actions-player-play-icon.png"); 
     ImageIcon ic2 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Settings-L-icon.png"); 
     ImageIcon ic3 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\cup-icon.png"); 
     ImageIcon ic4 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Button-Close-icon.png"); 
     ImageIcon ic5 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\ice-2025937_960_720.jpg"); 
     ImageIcon ic6 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\tenis1.png"); 

     JLabel mainLabel = new JLabel(); 
     Font font = new Font("Papyrus", Font.BOLD,26); 
     mainLabel.setFont(font); 
     mainLabel.setForeground(Color.RED); 
     mainLabel.setText("PONG GAME"); 

     JButton startButton = new JButton("Start Game"); 
     JButton optionsButton = new JButton("Options"); 
     JButton leaderButton = new JButton("Leaderboard"); 
     JButton exitButton = new JButton("Exit"); 

     JButton icb1 = new JButton(ic1); 
     JButton icb2 = new JButton(ic2); 
     JButton icb3 = new JButton(ic3); 
     JButton icb4 = new JButton(ic4); 
     JButton icb6 = new JButton(ic6); 

     // at first, we tried to keep our buttons and labels in panels 
     // but then we didn't add an image to label as the background 
     // so we created a JLabel to set its image as the background, we may remove it later. 

     JLabel mn = new JLabel(ic5); 
     mn.setBackground(Color.BLUE); 
     mn.setBounds(1, 1, 700, 700); 

     Font font3 = new Font("Calibri", Font.PLAIN, 20); 
     twoPlayer = new JCheckBox(); 
     twoPlayer.setFont(font3); 
     twoPlayer.setForeground(Color.DARK_GRAY); 
     twoPlayer.setText("  MultiPlayer"); 
     twoPlayer.setBounds(200, 350, 220, 40); 

     menuPanel.add(mn); 
     mn.add(mainLabel); 
     mn.add(startButton); 
     mn.add(optionsButton); 
     mn.add(leaderButton); 
     mn.add(exitButton); 
     mn.add(icb1); 
     mn.add(icb2); 
     mn.add(icb3); 
     mn.add(icb4); 
     mn.add(icb6); 
     mn.add(twoPlayer); 



     mainFrame.setVisible(true); 

     // the components added by their coordinates to make them look formal 

     mainLabel.setBounds(210, 100, 220, 40); 
     startButton.setBounds(200, 150, 220, 40); 
     optionsButton.setBounds(200, 200, 220, 40); 
     leaderButton.setBounds(200, 250, 220, 40); 
     exitButton.setBounds(200, 300, 220, 40); 

     icb1.setBounds(150, 150, 40, 40); 
     icb2.setBounds(150, 200, 40, 40); 
     icb3.setBounds(150, 250, 40, 40); 
     icb4.setBounds(150, 300, 40, 40); 
     icb6.setBounds(150,350,40,40); 

     startButton.setBorder(BorderFactory.createRaisedBevelBorder()); 
     optionsButton.setBorder(BorderFactory.createRaisedBevelBorder()); 
     leaderButton.setBorder(BorderFactory.createRaisedBevelBorder()); 
     exitButton.setBorder(BorderFactory.createRaisedBevelBorder()); 

     // optionsPanel components 
     JButton doneButton = new JButton("Done"); 
     doneButton.setBounds(150,330,220,40); 
     doneButton.setBorder(BorderFactory.createRaisedBevelBorder()); 

     Font font1 = new Font("Calibri", Font.PLAIN,24); 
     Font font2 = new Font("Calibri", Font.BOLD,19); 
     JLabel st = new JLabel(); 
     st.setFont(font1); 
     st.setForeground(Color.DARK_GRAY); 
     st.setText("-OPTIONS-"); 

     JLabel difficulty = new JLabel("Difficulty: "); 
     difficulty.setFont(font2); 
     difficulty.setForeground(Color.DARK_GRAY); 

     JLabel music = new JLabel("Sound: "); 
     music.setFont(font2); 
     music.setForeground(Color.DARK_GRAY); 

     JLabel gSpeed = new JLabel("Game Speed: "); 
     gSpeed.setFont(font2); 
     gSpeed.setForeground(Color.DARK_GRAY); 

     JLabel screen = new JLabel(ic5); 
     screen.setBackground(Color.BLUE); 
     screen.setBounds(1, 1, 700, 700); 

     JRadioButton rb1 = new JRadioButton("Easy"); 
     JRadioButton rb2 = new JRadioButton("Normal"); 
     JRadioButton rb3 = new JRadioButton("Hard"); 
     JRadioButton rb4 = new JRadioButton("On"); 
     JRadioButton rb5 = new JRadioButton("Off"); 
     JRadioButton rb6 = new JRadioButton("x"); 
     JRadioButton rb7 = new JRadioButton("2x"); 
     JRadioButton rb8 = new JRadioButton("3x"); 

     ButtonGroup bg1 = new ButtonGroup(); 
     ButtonGroup bg2 = new ButtonGroup(); 
     ButtonGroup bg3 = new ButtonGroup(); 
     bg1.add(rb1); 
     bg1.add(rb2); 
     bg1.add(rb3); 
     bg2.add(rb4); 
     bg2.add(rb5); 
     bg3.add(rb6); 
     bg3.add(rb7); 
     bg3.add(rb8); 

     optionsPanel.add(screen); 
     screen.add(difficulty); 
     screen.add(st); 
     screen.add(gSpeed); 
     screen.add(rb1); 
     screen.add(rb2); 
     screen.add(rb3); 
     screen.add(rb4); 
     screen.add(rb5); 
     screen.add(rb6); 
     screen.add(rb7); 
     screen.add(rb8); 
     screen.add(music); 
     screen.add(doneButton); 

     st.setBounds(200,100,220,40); 
     difficulty.setBounds(100,150,90,40); 
     music.setBounds(100,200,90,40); 
     gSpeed.setBounds(100, 250, 120, 40); 

     rb1.setBounds(200,150,60,40); 
     rb2.setBounds(260,150,80,40); 
     rb3.setBounds(340, 150, 60, 40); 
     rb4.setBounds(200, 200, 50, 40); 
     rb5.setBounds(250,200,50,40); 
     rb6.setBounds(220, 250, 50, 40); 
     rb7.setBounds(270, 250, 50, 40); 
     rb8.setBounds(320, 250, 50, 40); 

     startButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) {   
       Pong pong = new Pong(); 
       sound("Marimba-music"); 

       mainFrame.dispose(); 

       if(twoPlayer.isSelected()) { 
        pong.c.isTwoPlayer = true; 
       } 
       else { 
        pong.c.isTwoPlayer = false; 
       } 
      }  
     }); 

     optionsButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       mainFrame.setVisible(false); 
       optionsFrame.setVisible(true); 
      } 
     }); 

     doneButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       optionsFrame.dispose(); 
       Pong pong = new Pong(); 

       if(rb1.isSelected()) 
       { 
        pong.c.setUp(-10);; 
        pong.c.setDown(10); 
       } 
       else if(rb2.isSelected()) 
       { 
        pong.c.setUp(-11); 
        pong.c.setDown(11); 
       } 
       else if(rb3.isSelected()) 
       { 
        pong.c.setUp(-30); 
        pong.c.setDown(30); 
       } 

       if(rb4.isSelected()) 
       { 
        sound("Marimba-music"); 
       } 
       else if(rb5.isSelected()) 
       { 
        soundStop("Marimba-music"); 
       } 

       if(rb6.isSelected()) 
       { 
        GamePanel g = new GamePanel(); 
        g.x = 50; 
       } 
       else if(rb7.isSelected()) 
       { 
        GamePanel g = new GamePanel(); 
        g.x = 75; 
       } 
       else if(rb8.isSelected()) 
       { 
        GamePanel g = new GamePanel(); 
        g.x = 100; 
        System.out.println(g.x); 
       }   

      } 
     }); 

     exitButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) 
      { 
       mainFrame.dispose(); 
      } 
     }); 
    } 

    public void sound(String nm) 
    { 
     AudioStream BGM; 

     try { 
      InputStream test = new FileInputStream("./" +nm+".wav"); 
      BGM = new AudioStream(test); 

      AudioPlayer.player.start(BGM);  
     } 
     catch(IOException error) { 
      JOptionPane.showMessageDialog(null, error.getMessage()); 
     } 
    } 

    public void soundStop(String nm) 
    { 
     AudioStream BGM; 

     try { 
      InputStream test = new FileInputStream("./" +nm+".wav"); 
      BGM = new AudioStream(test); 

      AudioPlayer.player.stop(BGM); 
     } 
     catch(IOException error) { 
      JOptionPane.showMessageDialog(null, error.getMessage());   
     } 
    } 
} 

Computer.class : 나는 당신의 모든 프로그램이 어떻게 구성되어 있는지 모르는

public class Computer 
{ 
    private GamePanel field; 

    private int y = Pong.WINDOW_HEIGHT/2; 
    private int yCh = 0; 

    private int up = -10; 
    private int down = 10; 

    private int width = 20; 
    private int height = 90; 
    boolean isTwoPlayer = false; 

    public Computer() { 

    } 

    public void update(Ball b) { 
     if(y + height > Pong.WINDOW_HEIGHT) 
     { 
      y = Pong.WINDOW_HEIGHT - height; 
     } 
     else if(y < 0) 
     { 
      y = 0; 
     } 

     if(!isTwoPlayer) { 
      if(b.getY() < y+height && y >= 0) 
      { 
       yCh = up; 
      } 
      else if(b.getY() > y && y + height <= Pong.WINDOW_HEIGHT) 
      { 
       yCh = down; 
      } 
      y = y + yCh; 
     } 
     else { 
      y = y + yCh; 
     } 
    } 

    public void setYPosition(int speed) { 
     yCh = speed; 
    } 

    public void paint(Graphics g) { 
     g.setColor(Color.BLACK); 
     g.fillRoundRect(450, y, width, height, 10, 10); 
    } 

    public int getX() { 
     return 450; 
    } 

    public int getY() { 
     return y; 
    } 

    public int getWidth() { 
     return width; 
    } 

    public int getHeight() { 
     return height; 
    } 

    public int getUp() { 
     return up; 
    } 

    public int getDown() { 
     return down; 
    } 

    public void setUp(int up) { 
     this.up = up; 
    } 

    public void setDown(int down) { 
     this.down = down; 
    } 
+0

안녕하십니까. TL; DR; [MCVE]를 게시하십시오. – c0der

답변

1

이 내 코드입니다. 그러나이 코드 조각 :

doneButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     optionsFrame.dispose(); 
     Pong pong = new Pong(); 

당신은 Pong 클래스의 새로운 인스턴스를 생성하고 새로운 생성 된 인스턴스의 값을 설정합니다.

이 인스턴스는 다른 곳에서는 사용되지 않습니다. 이미 사용중인 Pong의 인스턴스를 사용해야하거나 다른 클래스에서 새 인스턴스를 사용할 수 있습니다.

관련 문제