Reputation: 11
When I try to export as a Runnable JAR file, I have no options for 'Launch configuration'. Why is this? I am new to programming, can anyone help me?
It's a code for the game Snake. Do I need a 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
}
}
Upvotes: 0
Views: 98
Reputation: 13858
Basically, you need a Run configuration that was already used to execute your Java application to have a runnable jar file. This is used to select the correct starting point of the program.
If you have executed your Java application using the Run command (either from the Run menu or the toolbar), a Run configuration was created that executes. However, judging from your question, you have not done so, as you have no entrypoint defined for your application.
For that java uses a static main method with predefined parameters, and any Java class that has such a method can be executed. After you have successfully executed your application, it can be started, and then the created run configuration can be used to export a jar file.
Upvotes: 1