Hi guys. So me again need help So i create a snake game. Ant this game have more than 1 class. So i need this thing export to jar.. So i have question:
1.Do i have insert to public static void main(String[]args) method to somewhere class?
2.How to export to jar with more than 1 class..
Thanks for help
And again, if I must insert main method in somewhere class, so which class?
snakeCanvas.java :
import java.awt.Event; import java.awt.Point; import javax.swing.event.*; import java.awt.*; 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.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.LinkedList; import java.util.Random; import javax.swing.*; public class snakeCanvas extends Canvas implements Runnable,KeyListener { private LinkedList<Point> snake; private final int BOX_HEIGHT = 15; private final int BOX_WIDTH = 15; private final int GRID_WIDTH = 25; private final int GRID_HEIGHT = 25; private int direction = Direction.NO_DIRECTION; private Point fruit; private Thread runThread; private Graphics globalGraphics; private int score = 0; private String highScore = ""; public void paint(Graphics g) { this.setPreferredSize(new Dimension(640,480)); snake = new LinkedList<Point>(); GenerateDefaultSnake(); PlaceFruit(); this.addKeyListener(this); globalGraphics = g.create(); if (runThread == null ){ runThread = new Thread(this); runThread.start(); } if (highScore.equals("")) { highScore = this.getHighScore(); } } 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(); } } } public void GenerateDefaultSnake() { snake.clear(); score = 0; snake.add(new Point(0,2)); snake.add(new Point(0,1)); snake.add(new Point(0,0)); snake.add(new Point(0,0)); direction = Direction.NO_DIRECTION; } public void Draw(Graphics g) { g.clearRect(0, 0, BOX_WIDTH * GRID_WIDTH, BOX_HEIGHT * GRID_HEIGHT +20); //Create new image //BufferedImage buffer = new BufferedImage(BOX_WIDTH * GRID_WIDTH, BOX_HEIGHT * GRID_HEIGHT, BufferedImage.TYPE_INT_ARGB); //Graphics bufferGraphics = buffer.getGraphics(); DrawFruit(g); DrawGid(g); DrawSnake(g); DrawScore(g); //flip //g.drawImage(buffer, 0, 0, BOX_WIDTH * GRID_WIDTH, BOX_HEIGHT * GRID_HEIGHT, this); } public void move() { Point head = snake.peekFirst(); Point newPoint = head; switch(direction) { case Direction.NORTH: newPoint = new Point(head.x, head.y - 1); 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); } snake.remove(snake.peekLast()); if (newPoint.equals(fruit)) { Point addPoint = (Point) newPoint.clone(); score+=10; switch(direction) { case Direction.NORTH: newPoint = new Point(head.x, head.y - 1); 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); } snake.push(addPoint); PlaceFruit(); } else if (newPoint.x < 0 || newPoint.x > (GRID_WIDTH - 1) ) { CheckScore(); GenerateDefaultSnake(); return; } else if (newPoint.y < 0 || newPoint.y > (GRID_HEIGHT - 1) ) { //oopsssS CheckScore(); GenerateDefaultSnake(); return; } else if (snake.contains(newPoint)) { CheckScore(); GenerateDefaultSnake(); return; } //if we reack code snake.push(newPoint); } public void DrawScore(Graphics g) { g.drawString("Surinkai "+score+" tasku :) ",0, BOX_HEIGHT * GRID_HEIGHT+10); g.drawString("HighScore: "+highScore, 0 , BOX_HEIGHT * GRID_HEIGHT+20); } public void CheckScore() { if (highScore.equals("")) return; if (score > Integer.parseInt(highScore.split(":")[1])) { String name = JOptionPane.showInputDialog("You set new highscore! YOUR NAME"); highScore = name +":"+score; File scoreFile = new File("highscore.dat"); if(!scoreFile.exists()) { try { scoreFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block 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 DrawGid(Graphics g) { //Vertical lines g.drawRect(0, 0, GRID_WIDTH * BOX_WIDTH, GRID_HEIGHT * BOX_HEIGHT); for(int x = BOX_WIDTH; x<GRID_WIDTH * BOX_WIDTH ;x+=BOX_WIDTH) { g.drawLine(x, 0, x, BOX_HEIGHT*GRID_HEIGHT); } for (int y = BOX_HEIGHT; y < GRID_HEIGHT * BOX_HEIGHT; y+= BOX_HEIGHT) { g.drawLine(0,y,GRID_WIDTH*BOX_HEIGHT,y); } } public void DrawSnake(Graphics g) { g.setColor(Color.GREEN); 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)) { randomX = rand.nextInt(GRID_WIDTH); randomY = rand.nextInt(GRID_HEIGHT); randomPoint = new Point(randomX,randomY); } fruit = randomPoint; } @Override public void run() { while (true) { move(); Draw(globalGraphics); try { Thread.currentThread(); Thread.sleep(100); }catch (Exception 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_LEFT: if (direction != Direction.EAST) direction = Direction.WEST; break; case KeyEvent.VK_RIGHT: if (direction != Direction.WEST) direction = Direction.EAST; break; } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }
Direction.java :
public class Direction { public static final int NO_DIRECTION = 0; public static final int NORTH = 1; public static final int SOUTH = 2; public static final int WEST = 3; public static final int EAST = 4; }
TyleType.java
public class TyleType { public static final int FRUIT = 2; public static final int SNAKE_BODY = 1; public static final int EMPTY = 0; }
snakeApplet.java
import java.applet.Applet; import java.awt.Dimension; import java.awt.Graphics; public class snakeApplet extends Applet { private snakeCanvas c; public void init() { c = new snakeCanvas(); c.setVisible(true); c.setFocusable(true); c.setSize(new Dimension(640,480)); this.add(c); this.setVisible(true); this.setSize(new Dimension(640,480)); } public void paint(Graphics g) { this.setSize(new Dimension(640,480)); } }
Thanks for help P.S snake in eclipse works perfectly