ArrayIndexOutOfBoundsException 2D游戏开发

时间:2016-05-05 01:55:47

标签: java 2d-games

我正在关注这个tutorial但是我不知道下面两个课程在解释每件事情的错误在哪里因为它只有两个课程我试图再次观看教程但仍然我没有找到错误

<!DOCTYPE html>
<html>

<head>
  <title>Tic Tac Toe Game</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    #container {
      width: 303px;
      margin: 0 auto;
      margin-top: 40px;
    }
    h1 {
      text-align: center;
      margin-bottom: 15px;
    }
    /*#wrapper {
			margin: 0 auto;
		}*/
    .col {
      float: left;
      margin: 0 auto;
    }
    .col > div:nth-child(-n+1) {
      border-bottom: 1px solid #000;
    }
    .col > div:nth-child(-n+2) {
      border-bottom: 1px solid #000;
    }
    .col > div {
      border-right: 1px solid #000;
    }
    .col:last-child {
      border-right: none;
      border: 1px solid red;
    }
    #wrapper.col * {
      border-right: none;
    }
    .reset {
      clear: both;
    }
    .box {
      width: 100px;
      height: 100px;
      background-color: grey;
      font-size: 80px;
      text-align: center;
    }
    body {
      font-size: 80%;
    }
  </style>
</head>

<body>
  <div id="container">
    <h1>Tic Tac Toe</h1>
    <div id="wrapper">
      <div class="col">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
      </div>
      <div class="col">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
      </div>
      <div class="col">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
      </div>
      <div class="reset"></div>
    </div>
  </div>
  <script>
    var turnsArray = [];

    function player() {
      alert("hello");
    }

    var el = document.getElementsByClassName('box');
    el.onclick = player;
  </script>
</body>

</html>

这是主要类,其中没有太多代码。

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gameofthrones;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author issba
 */
public class ClassOGP extends JFrame{
    boolean fse =false;
    int fsm = 0;
    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];
    public ClassOGP(String title,int width,int height){
        setTitle(title);
        setSize(width,height);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);


    }

    private void setfullscreen(){
        switch(fsm){
            case 0:
                System.out.println("No fullscreen");
                setUndecorated(false);
            break;
            case 1:
                setExtendedState(JFrame.MAXIMIZED_BOTH);
                setUndecorated(true);
            break;
            case 2:
                device.setFullScreenWindow(this);
                setUndecorated(true);
            break;
        }

    }

    public void setFullscreen(int fsnm){
           fse = true;
           if(fsm <= 2){
           this.fsm = fsnm;
           setfullscreen();
           }else{
           System.err.println("Error " + fsnm + " is not supported");
           }
        }
    }

这里的错误信息

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gameofthrones;


/**
 *
 * @author issba
 */
public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        ClassOGP frame = new ClassOGP("Game Of thrones",1280,720);
        frame.setFullscreen(1);
        frame.setVisible(true);
    }

}

2 个答案:

答案 0 :(得分:2)

这一行:

GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1]; 

抛出错误。尝试将1更改为0

这只是一个快速修复,但您应该将设备声明为实例或类成员,并在构造函数中指定它。然后,如果没有屏幕设备,您可以进行错误检查。如下所示:

public class ClassOGP extends JFrame{
    /* other code */

    public GraphicsDevice device;

    public ClassOGP(String title,int width,int height) {
        if(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length() > 0) {
            // you can also check for multiple devices here to see if you want
            // to use one other than the zero'th index
            device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
        } else {
            System.out.println("ERROR: No devices ... exiting.");
            System.exit();
        }

        /* other code */
    }

    /* rest of class */
}

答案 1 :(得分:1)

当您尝试访问具有非法数组索引的元素或位于数组范围之外的元素时,会发生错误。