2015-11-04 90 views
0

當我嘗試導出爲Runnable JAR文件時,我沒有「啓動配置」選項。爲什麼是這樣?我是編程新手,任何人都可以幫助我嗎?如何從eclipse導出爲Runnable JAR文件?

這是一個遊戲蛇的代碼。我需要一個main()嗎?

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Point; 
import java.awt.Toolkit; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.net.URL; 
import java.util.LinkedList; 
import java.util.Random; 

import javax.swing.JOptionPane; 

@SuppressWarnings("serial") 
public class snakeCanvas extends Canvas implements Runnable, KeyListener 
{ 
private final int BOX_HEIGHT = 15; 
private final int BOX_WIDTH = 15; 
private final int GRID_HEIGHT = 25; 
private final int GRID_WIDTH = 25; 

private LinkedList<Point> snake; 
private Point fruit; 
private int direction = Direction.NO_DIRECTION; 

private Thread runThread; //allows us to run in the background 
private int score = 0; 
private String highScore = ""; 

private Image menuImage = null; 
private boolean isInMenu = true; 

public void paint(Graphics g) 
{ 
    if (runThread == null) 
    { 
     this.setPreferredSize(new Dimension(640, 480)); 
     this.addKeyListener(this); 
     runThread = new Thread(this); 
     runThread.start(); 
    } 
    if (isInMenu) 
    { 
     DrawMenu(g); 
     //draw menu 
    } 
    else 
    { 
     if (snake == null) 
     { 
     snake = new LinkedList<Point>(); 
     GenerateDefaultSnake(); 
     PlaceFruit(); 
     } 
     if (highScore.equals("")) 
     { 
      highScore = this.GetHighScore(); 
      System.out.println(highScore); 
     } 
     DrawSnake(g); 
     DrawFruit(g); 
     DrawGrid(g); 
     DrawScore(g); 
     //draw everything else 
    } 
} 

public void DrawMenu(Graphics g) 
{ 
    if (this.menuImage == null) 
    { 
     try 
     { 
      URL imagePath = snakeCanvas.class.getResource("snakeMenu.png"); 
      this.menuImage = Toolkit.getDefaultToolkit().getImage(imagePath); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 
    g.drawImage(menuImage, 0, 0, 640, 480, this); 

} 

public void update(Graphics g) 
{ 
    Graphics offScreenGraphics; 
    BufferedImage offscreen = null; 
    Dimension d= this.getSize(); 

    offscreen = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); 
    offScreenGraphics = offscreen.getGraphics(); 
    offScreenGraphics.setColor(this.getBackground()); 
    offScreenGraphics.fillRect(0, 0, d.width, d.height); 
    offScreenGraphics.setColor(this.getForeground()); 
    paint(offScreenGraphics); 

    g.drawImage(offscreen, 0, 0, this); 
} 

public void GenerateDefaultSnake() 
{ 
    score = 0; 
    snake.clear(); 

    snake.add(new Point (0,2)); 
    snake.add(new Point(0,1)); 
    snake.add(new Point(0,0)); 
    direction = Direction.NO_DIRECTION; 
} 

public void Move() //adds a new 'block' at front of snake, deleting the back 'block' to create movement 
{ 
    Point head = snake.peekFirst(); //grab the first item with no problems 
    Point newPoint = head; 
    switch (direction) { 
    case Direction.NORTH: 
     newPoint = new Point(head.x, head.y - 1); //vector of -j 
     break; 
    case Direction.SOUTH: 
     newPoint = new Point(head.x, head.y + 1); 
     break; 
    case Direction.WEST: 
     newPoint = new Point(head.x - 1, head.y); 
     break; 
    case Direction.EAST: 
     newPoint = new Point (head.x + 1, head.y); 
     break; 
    } 


    snake.remove(snake.peekLast()); //delete the last block 

    if (newPoint.equals(fruit)) 
    { 
     //the snake hits the fruit 
     score+=10; 
     Point addPoint = (Point) newPoint.clone(); //creating the end piece upon eating fruit 

     switch (direction) { 
     case Direction.NORTH: 
      newPoint = new Point(head.x, head.y - 1); //vector of -j 
      break; 
     case Direction.SOUTH: 
      newPoint = new Point(head.x, head.y + 1); 
      break; 
     case Direction.WEST: 
      newPoint = new Point(head.x - 1, head.y); 
      break; 
     case Direction.EAST: 
      newPoint = new Point (head.x + 1, head.y); 
      break; 
     } 
     //the movement upon eating fruit 
     snake.push(addPoint); 
     PlaceFruit(); 

    } 
    else if (newPoint.x < 0 || newPoint.x > (GRID_WIDTH - 1)) 
    { 
     //snake has gone out of bounds - reset game 
     CheckScore(); 
     GenerateDefaultSnake(); 
     return; 
    } 
    else if (newPoint.y < 0 || newPoint.y > (GRID_HEIGHT - 1)) 
    { 
     //snake has gone out of bounds - reset game 
     CheckScore(); 
     GenerateDefaultSnake(); 
     return; 
    } 
    else if (snake.contains(newPoint)) 
    { 
     //running into your tail - reset game 
     CheckScore(); 
     GenerateDefaultSnake(); 
     return; 
    } 

    snake.push(newPoint); 
} 

public void DrawScore(Graphics g) 
{ 
    g.drawString("Score: " + score, 0, BOX_HEIGHT * GRID_HEIGHT + 15); 
    g.drawString("Highscore:" + highScore, 0, BOX_HEIGHT * GRID_HEIGHT + 30); 
} 

public void CheckScore() 
{ 
    if (highScore.equals("")) 
     return; 

    if (score > Integer.parseInt((highScore.split(":")[1]))) 
    { 
     String name = JOptionPane.showInputDialog("New highscore. Enter your name!"); 
     highScore = name + ":" + score; 

     File scoreFile = new File("highscore.dat"); 
     if (!scoreFile.exists()) 
     { 

      try { 
       scoreFile.createNewFile(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     FileWriter writeFile = null; 
     BufferedWriter writer = null; 
     try 
     { 
      writeFile = new FileWriter(scoreFile); 
      writer = new BufferedWriter(writeFile); 
      writer.write(this.highScore); 
     } 
     catch (Exception e) 
     { 
     } 
     finally 
     { 
      try 
     { 
      if (writer != null) 
       writer.close(); 
     } 
     catch (Exception e) {} 
     } 
    } 
} 

public void DrawGrid(Graphics g) 
{ 
    //drawing the outside rectangle 
    g.drawRect(0, 0, GRID_WIDTH * BOX_WIDTH, GRID_HEIGHT * BOX_HEIGHT); 
    //drawing the vertical lines 
    for (int x = BOX_WIDTH; x < GRID_WIDTH * BOX_WIDTH; x+=BOX_WIDTH) 
    { 
     g.drawLine(x, 0, x, BOX_HEIGHT * GRID_HEIGHT); 
    } 
    //drawing the horizontal lines 
    for (int y = BOX_HEIGHT; y < GRID_HEIGHT * BOX_HEIGHT; y+=BOX_HEIGHT) 
    { 
     g.drawLine(0, y, GRID_WIDTH * BOX_WIDTH, y); 
    } 
} 

public void DrawSnake(Graphics g) 
{ 
    g.setColor(Color.ORANGE); 
    for (Point p : snake) 
    { 
     g.fillRect(p.x * BOX_WIDTH, p.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT); 
    } 
    g.setColor(Color.BLACK); 
} 

public void DrawFruit(Graphics g) 
{ 
    g.setColor(Color.RED); 
    g.fillOval(fruit.x * BOX_WIDTH, fruit.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT); 
    g.setColor(Color.BLACK); 
} 

public void PlaceFruit() 
{ 
    Random rand = new Random(); 
    int randomX = rand.nextInt(GRID_WIDTH); 
    int randomY = rand.nextInt(GRID_HEIGHT); 
    Point randomPoint = new Point(randomX, randomY); 
    while (snake.contains(randomPoint)) //If the fruit happens to spawn on the snake, it will change position 
    { 
     randomX= rand.nextInt(GRID_WIDTH); 
     randomY= rand.nextInt(GRID_HEIGHT); 
     randomPoint = new Point(randomX, randomY); 
    } 
    fruit = randomPoint; 
} 

@Override 
public void run() { 
    while (true) 
    { 
     repaint(); 
     //runs indefinitely (CONSTANTLY LOOPS) 
     if (!isInMenu) 
      Move(); 

        //Draws grid, snake and fruit 

     //buffer to slow down the snake 
     try 
     { 
      Thread.currentThread(); 
      Thread.sleep(100); 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

} 

public String GetHighScore() 
{ 
    FileReader readFile = null; 
    BufferedReader reader = null; 
    try 
    { 
    readFile = new FileReader("highscore.dat"); 
    reader = new BufferedReader(readFile); 
    return reader.readLine(); 

} 
    catch (Exception e) 
    { 
     return "Nobody:0"; 
    } 
    finally 
    { 
     try{ 
      if (reader != null) 
       reader.close(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 
} 

@Override 
public void keyPressed(KeyEvent e) { 
    switch (e.getKeyCode()) 
    { 
    case KeyEvent.VK_UP: 
     if (direction != Direction.SOUTH) 
      direction = Direction.NORTH; 
     break; 
    case KeyEvent.VK_DOWN: 
     if (direction != Direction.NORTH) 
      direction = Direction.SOUTH; 
     break; 
    case KeyEvent.VK_RIGHT: 
     if (direction != Direction.WEST) 
      direction = Direction.EAST; 
     break; 
    case KeyEvent.VK_LEFT: 
     if (direction != Direction.EAST) 
     direction = Direction.WEST; 
     break; 
    case KeyEvent.VK_ENTER: 
     if (isInMenu) 
     { 
      isInMenu = false; 
      repaint(); 
     } 
     break; 
    case KeyEvent.VK_ESCAPE: 
     isInMenu = true; 
     break; 
    } 

} 

@Override 
public void keyReleased(KeyEvent e) { 


} 

@Override 
public void keyTyped(KeyEvent e) { 
    // TODO Auto-generated method stub 

} 

}

+0

是的,你需要一個主要的方法...在這一點上,只需要運行()進入主要 –

+0

你介意告訴我你將如何做到這一點?這是我的第一個項目,我不確定如何編寫它。 – AClark

回答

1

基本上,你需要一個已經用來執行Java應用程序有一個可運行jar文件運行配置。這用於選擇程序的正確起點。

如果您使用運行命令(運行菜單或工具欄)執行了您的Java應用程序,則會創建一個運行配置並執行。但是,從您的問題來看,您還沒有這樣做,因爲您沒有爲您的應用程序定義入口點。

因爲java使用static main method with predefined parameters,並且可以執行任何具有這種方法的Java類。成功執行應用程序後,可以啓動它,然後可以使用創建的運行配置導出jar文件。

相關問題