2015-12-07 35 views
0

我在一個班級中有一個工作程序,但我認爲將班級分開會更好,因此我將程序分成三個.java文件,現在程序不再運行。我沒有一個單一的錯誤在程序的任何地方,但是當我嘗試運行該程序我得到這個錯誤信息如何將功能返回到我的主要方法?

"Exception in thread "main" java.lang.NoSuchMethodException: Tetris.main([Ljava.lang.String;) 
    at java.lang.Class.getMethod(Class.java:1786) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)" 

這是什麼問題,如何解決?

這是我的三個文件:

Tetris.java

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*; 
import java.util.Random; 
import static java.awt.Color.*; 


public class Tetris extends Applet { 

    // 
    // STATIC MEMBERS 
    // 

    private final static int INITIAL_DELAY = 1000; 

    public final static byte ROWS = 20; 
    public final static byte COLUMNS = 10; 
    public final static int EMPTY = -1; 

    static Color[] colors = 
      {black, green, blue, red, 
        yellow, magenta, pink, cyan}; 




    private final static Color BACKGROUND_COLOR = Color.black; 


    public final static boolean PIECE_BITS[][][] = { 

      { 
        {false, true, false, false}, 
        {false, true, false, false}, 
        {false, true, false, false}, 
        {false, true, false, false}, 
      }, 
      { 
        {false, false, false, false}, 
        {false, true, true, false}, 
        {false, true, false, false}, 
        {false, true, false, false}, 
      }, 
      { 
        {false, false, false, false}, 
        {false, true, false, false}, 
        {false, true, false, false}, 
        {false, true, true, false}, 
      }, 
      { 
        {false, false, false, false}, 
        {false, true, false, false}, 
        {false, true, true, false}, 
        {false, false, true, false}, 
      }, 
      { 
        {false, false, false, false}, 
        {false, false, true, false}, 
        {false, true, true, false}, 
        {false, true, false, false}, 
      }, 
      { 
        {false, false, false, false}, 
        {false, true, false, false}, 
        {false, true, true, false}, 
        {false, true, false, false}, 
      }, 
      { 
        {false, false, false, false}, 
        {false, false, false, false}, 
        {false, true, true, false}, 
        {false, true, true, false}, 
      }, 
    }; 

    private static Random random = new Random(); 

    // 
    // INSTANCE DATA 
    // 

    public static int grid[][] = new int[ROWS][COLUMNS]; 
    public static Timer timer; 

    private int next_piece_grid[][] = new int[4][4]; 
    private GridCanvas game_grid = new GridCanvas(grid, true); 
    private GridCanvas next_piece_canvas = new GridCanvas(next_piece_grid, false); 
    private TetrisPiece cur_piece; 
    private TetrisPiece next_piece = randomPiece(); 


    // 
    // INNER CLASSES 
    // 


    private class Timer extends Thread { 
     private long m_delay; 
     private boolean m_paused = true; 
     private boolean m_fast = false; 
     private ActionListener m_cb; 

     public Timer(long delay, ActionListener cb) { 
      setDelay(delay); 
      m_cb = cb; 
     } 

     public void setPaused(boolean pause) { 
      m_paused = pause; 
      if (m_paused) { 

      } else { 

       synchronized (this) { 
        this.notify(); 
       } 
      } 
     } 

     public void setDelay(long delay) { 
      m_delay = delay; 
     } 

     public void setFast(boolean fast) { 
      m_fast = fast; 
      if (m_fast) { 
       try { 
        this.checkAccess(); 
        this.interrupt(); // no exception, so OK to interrupt 
       } catch (SecurityException se) { 
       } 
      } 
     } 


     public void run() { 
      while (true) { 
       try { 
        sleep(m_fast ? 30 : m_delay); 
       } catch (Exception e) { 
       } 
       if (m_paused) { 
        try { 
         synchronized (this) { 
          this.wait(); 
         } 
        } catch (InterruptedException ie) { 
        } 
       } 
       synchronized (this) { 
        m_cb.actionPerformed(null); 
       } 
      } 
     } 
    } // end class Timer 

    private class GridCanvas extends DoubleBufferedCanvas { 
     private int grid[][]; 

     public GridCanvas(int[][] grid, boolean do_background) { 
      this.grid = grid; 
      clear(); 
     } 

     private void clear() { 
      for (int i = 0; i < grid.length; i++) 
       for (int j = 0; j < grid[0].length; j++) 
        grid[i][j] = EMPTY; 
     } 

     public Dimension getPreferredSize() { 
      return new Dimension(grid[0].length * 30, grid.length * 30); 
     } 

     public void paint(Graphics g) { 
      g = this.startPaint(g); // returned g paints into offscreen image 
      int width = this.getSize().width; 
      int height = this.getSize().height; 
      g.clearRect(0, 0, width, height); 
      int cell_size, xstart, ystart; 
      double panel_aspect_ratio = (double) width/height; 
      double grid_aspect_ratio = (double) grid[0].length/grid.length; 
      if (panel_aspect_ratio > grid_aspect_ratio) { 
       // extra space on sides 
       cell_size = (int) ((double) height/grid.length + 0.5); 
       xstart = (int) (width/2 - (grid[0].length/2.0 * cell_size + 0.5)); 
       ystart = 0; 
      } else { 
       // extra vertical space 
       cell_size = (int) ((double) width/grid[0].length + 0.5); 
       xstart = 0; 
       ystart = (int) (height/2 - (grid.length/2.0 * cell_size + 0.5)); 
      } 

      for (int i = 0; i < grid.length; i++) { 
       for (int j = 0; j < grid[0].length; j++) { 
        if (grid[i][j] != EMPTY) { 
         g.setColor(colors[grid[i][j]]); 
         int x = xstart + j * cell_size; 
         int y = ystart + i * cell_size; 
         g.fill3DRect(x, y, cell_size, cell_size, true); 
        } 
       } 
      } 
      this.endPaint(); // paints accumulated image in one shot 
     } 
    } 

    private TetrisPiece randomPiece() { 
     int rand = Math.abs(random.nextInt()); 
     return new TetrisPiece(rand % (colors.length)); 
    } 

    private void installNewPiece() { 
     next_piece_canvas.clear(); 
     cur_piece = next_piece; 
     cur_piece.setPosition(3, -4); //-4 to start above top of grid 
     if (cur_piece.canPaste()) { 
      next_piece = randomPiece(); 
      next_piece.setPosition(0, 0); 
      next_piece.paste(next_piece_grid); 
      next_piece_canvas.repaint(); 
     } else 
      gameOver(); 
    } 

    private void gameOver() { 
     System.out.println("Game Over!"); 
     timer.setPaused(true); 
    } 



    public void start() { 
     timer = new Timer(INITIAL_DELAY, new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       synchronized (timer) { 
        if (cur_piece.canStepDown()) { 
         cur_piece.cut(); 
         cur_piece.stepDown(); 
         cur_piece.paste(); 
        } else { // it hit something 
         timer.setFast(false); 
         if (!cur_piece.isTotallyOnGrid()) 
          gameOver(); 
         else { 
          installNewPiece(); 
         } 
        } 
       } 
       game_grid.repaint(); 
      } 
     }); 
     timer.start(); 
    } 

    public void stop() { 
     pauseGame(); 
     synchronized (timer) { 
      timer.stop(); 
     } 
     timer = null; 
    } 

    public void startGame() { 
     timer.setDelay(INITIAL_DELAY); 
     timer.setPaused(false); 
    } 


    private void pauseGame() { 
     timer.setPaused(true); 
    } 


    public void init() { 
     installNewPiece(); 

     //create key listener for moving left, moving right 

     class TAdapter extends KeyAdapter { 
      public void keyPressed(KeyEvent e) { 

       int keycode = e.getKeyCode(); 

       switch (keycode) { 
        case KeyEvent.VK_LEFT: 
         if (e.getKeyCode() == 37 || e.getKeyCode() == 38) { 
          int dir = e.getKeyCode() == 37 ? -1 : 1; 
          synchronized (timer) { 
           cur_piece.cut(); 
           cur_piece.setX(cur_piece.getX() + dir); 

           if (!cur_piece.canPaste()) { 
            cur_piece.setX(cur_piece.getX() - dir); 
           } 
          } 
         } 


       } 
      } 
     } 


     //setting background colour and Tetrus grid 
     this.setLayout(new GridLayout(1, 2)); 
     this.add(game_grid); 
     this.setBackground(BACKGROUND_COLOR); 
     this.validate(); 
    } 

    public int[][] getGrid() { 
     return grid; 
    } 


    class DoubleBufferedCanvas extends Canvas { 
     private Image mActiveOffscreenImage = null; 
     private Dimension mOffscreenSize = new Dimension(-1, -1); 
     private Graphics mActiveOffscreenGraphics = null; 
     private Graphics mSystemGraphics = null; 

     DoubleBufferedCanvas() { 

     } 

     public void update(Graphics g) { 
      paint(g); 
     } 

     public Graphics startPaint(Graphics sysgraph) { 
      mSystemGraphics = sysgraph; 
      Dimension d = getSize(); 
      if ((mActiveOffscreenImage == null) || 
        (d.width != mOffscreenSize.width) || 
        (d.height != mOffscreenSize.height)) { 
       mActiveOffscreenImage = createImage(d.width, d.height); 
       mActiveOffscreenGraphics = mActiveOffscreenImage.getGraphics(); 
       mOffscreenSize = d; 
       mActiveOffscreenGraphics.setFont(getFont()); 
      } 

      return mActiveOffscreenGraphics; 
     } 

     public void endPaint() { 

      mSystemGraphics.drawImage(mActiveOffscreenImage, 0, 0, null); 
     } 
    } 
} 

TetrisPiece.java

import java.awt.*; 

    private boolean squares[][]; 
    private int type; 
    private Point position = new Point(3, -4); // -4 to start above top row 
    public int getX() { return position.x; } 
    public int getY() { return position.y; } 
    public void setX(int newx) { position.x = newx; } 
    public void setY(int newy) { position.y = newy; } 
    public void setPosition(int newx, int newy) { setX(newx); setY(newy); 


    } 

    public TetrisPiece(int type) { 
     this.type = type; 
     this.squares = new boolean[4][4]; 
     for(int i=0; i<4; i++) 
      for(int j=0; j<4; j++) 
       this.squares[i][j] = Tetris.PIECE_BITS[type][i][j]; 
    } 

    public boolean canStepDown() { 
     synchronized(Tetris.timer) { 
      cut(); 
      position.y++; 
      boolean OK = canPaste(); 
      position.y--; 
      paste(); 
      return OK; 
     } 
    } 

    public boolean canPaste() { 
     for(int i=0; i<4; i++) { 
      for(int j=0; j<4; j++) { 
       int to_x = j + position.x; 
       int to_y = i + position.y; 
       if(squares[i][j]) { 

        //checks if sqaure is too far left or right 

        if(0 > to_x || to_x >= Tetris.COLUMNS 
          || to_y >= Tetris.ROWS) // square off bottom? 
        { 
         return false; 

         // This allows the user to move a piece before it drops 
         // completely into view. 
        } 
        if(to_y >= 0 && Tetris.grid[to_y][to_x] != Tetris.EMPTY) 
         return false; 
       } 
      } 
     } 
     return true; 
    } 

    public void stepDown() { 
     position.y++; 
    } 

    public void cut() { 
     for(int i=0; i<4; i++) 
      for(int j=0; j<4; j++) 
       if(squares[i][j] && position.y+i>=0) 
        Tetris.grid[position.y + i][position.x + j] = Tetris.EMPTY; 
    } 

    /** 
    * Paste the color info of this piece into the given grid 
    */ 
    public void paste(int into[][]) { 
     for(int i=0; i<4; i++) 
      for(int j=0; j<4; j++) 
       if(squares[i][j] && position.y+i>=0) 
        into[position.y + i][position.x + j] = type; 
    } 

    /** 
    * No argument version assumes pasting into main game grid 
    */ 
    public void paste() { 
     paste(Tetris.grid); 
    } 

    // this method is a bit of a hack to check for the case 
    // where a piece may be safely on the grid but have one or more 
    // rows of empty squares that are above the grid and therefore OK 
    public boolean isTotallyOnGrid() { 
     for(int i=0; i<4; i++) { 
      if(position.y + i >= 0) 
       return true; //everything from here down is on grid 
      // this row is above grid so look for non-empty squares 
      for(int j=0; j<4; j++) 
       if(squares[i][j]) 
        return false; 
     } 
     System.err.println("TetrisPiece.isTotallyOnGrid internal error"); 
     return false; 
    } 
} 

TetrisMain.java

import java.awt.*; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 


public class TetrisMain { 
    public static void main(String[] args) { 

     Frame frame = new Frame("Tetris"); 
     Tetris tetris = new Tetris(); 
     frame.add(tetris); 
     tetris.init(); 
     tetris.start(); 
     tetris.startGame(); 

     frame.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 

     frame.setSize(250,500); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 
} 
+0

運行它,你是如何運行它 –

+2

你必須運行'TetrisMain'不'Tetris' – Jens

+0

去「跑/調試配置「並更改您的Main類。 – Andrew

回答

1

我猜你試圖從Tetris類的IDE中運行,這就是爲什麼它正在尋找你的main()(「你的程序的入口點」)方法。即

"Exception in thread "main" java.lang.NoSuchMethodException: Tetris.main([Ljava.lang.String;) 

您已經將main()方法TetrisMain所以你需要從那裏