Hello all, i have recently been coding a little 2D game to get back into JAVA coding as i haven't done it for about a year and i keep stumbling on problem after problem. My first very simple build of the game worked fine but now that i have tried to implement a highscore system into it the whole thing has shut down, it wont load in browser and now not even in netbeans. Any help or advice would be greatly appreciated as this is driving me crazy.
The Errors i get when trying to run in Netbeans:
Code:java.lang.InstantiationException: javaapplication2.Main at java.lang.Class.newInstance0(Class.java:340) at java.lang.Class.newInstance(Class.java:308) at sun.applet.AppletPanel.createApplet(AppletPanel.java:785) at sun.applet.AppletPanel.runLoader(AppletPanel.java:714) load: javaapplication2/Main.class can't be instantiated. at sun.applet.AppletPanel.run(AppletPanel.java:368) at java.lang.Thread.run(Thread.java:619)
Here are all my classes, some are from an old highscore system im working off and others are being currently used in my new system.
New System uses:
Main.java
Ball.java *
Player.java *
Score.java *
FileReader.java
ScoreComparator.java
* these classes are all fine so i wont put in post but if needs be i can post them.
** (EDIT: I have now put in all the .java classes for you to see.
Main.java:
Code:package javaapplication2; import java.awt.*; import java.util.*; import java.applet.*; import java.io.*; import java.util.Scanner; //Author: Scott public class Main extends Applet implements Runnable { private int speed; boolean isStoped = true; private Player player; private Ball redball; private Ball blueball; private Ball greenball; private Ball purpleball; Thread th; AudioClip shotnoise; AudioClip hitnoise; AudioClip outnoise; @SuppressWarnings("unchecked") FileReader hm = new FileReader(); Font f = new Font("Serif", Font.BOLD, 20); Cursor c; private Image dbImage; private Graphics dbg; public void init() { c = new Cursor(Cursor.CROSSHAIR_CURSOR); this.setCursor(c); Color superblue = new Color(0, 0, 255); setBackground(Color.black); setFont(f); if (getParameter("speed") != null) { speed = Integer.parseInt(getParameter("speed")); } else { speed = 15; } hitnoise = getAudioClip(getCodeBase(), "gun.au"); hitnoise.play(); hitnoise.stop(); shotnoise = getAudioClip(getCodeBase(), "miss.au"); shotnoise.play(); shotnoise.stop(); outnoise = getAudioClip(getCodeBase(), "error.au"); outnoise.play(); outnoise.stop(); player = new Player(); redball = new Ball(10, 190, 250, 1, -1, 4, Color.red, outnoise, player); blueball = new Ball(10, 190, 150, 1, -1, 4, Color.blue, outnoise, player); greenball = new Ball(10, 190, 200, 1, 1, 4, Color.green, outnoise, player); } public void start() { th = new Thread(this); th.start(); } public void stop() { th.stop(); } public boolean mouseDown(Event e, int x, int y) { if (!isStoped) { if (redball.userHit(x, y)) { hitnoise.play(); redball.ballWa****(); } if (blueball.userHit(x, y)) { hitnoise.play(); blueball.ballWa****(); } if (player.getScore() >= 1000) { if (greenball.userHit(x, y)) { hitnoise.play(); greenball.ballWa****(); } } else { shotnoise.play(); } } else if (isStoped && e.clickCount == 2) { isStoped = false; init(); } return true; } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true) { if (player.getLives() >= 0 && !isStoped) { redball.move(); blueball.move(); } if (player.getScore() >= 1000 && !isStoped) { greenball.move(); } repaint(); try { Thread.sleep(speed); } catch (InterruptedException ex) { // do nothing } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint(Graphics g) { if (player.getLives() >= 0) { g.setColor(Color.yellow); g.drawString("Score: " + player.getScore(), 10, 40); g.drawString("Lives: " + player.getLives(), 300, 40); redball.DrawBall(g); blueball.DrawBall(g); if (player.getScore() >= 1000) { greenball.DrawBall(g); } if (isStoped) { g.setColor(Color.yellow); g.drawString("Doubleclick To Start!", 90, 200); g.drawString("By: " + Author, 300, 300); } } else if (player.getLives() < 0) { g.setColor(Color.yellow); g.drawString("Game over!", 130, 100); g.drawString("You scored " + player.getScore() + " Points", 90, 140); g.drawString("Doubleclick to play again!", 90, 220); isStoped = true; } } public void update(Graphics g) { if (dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); dbg.setColor(getForeground()); paint(dbg); g.drawImage(dbImage, 0, 0, this); } String Author = "Scott"; public Main(String aFileName){ fFile = new File(aFileName); } public void processLineByLine() throws FileNotFoundException { Scanner scanner = new Scanner(fFile); try { while ( scanner.hasNextLine() ){ processLine( scanner.nextLine() ); } } finally { scanner.close(); } } public void processLine(String aLine) throws FileNotFoundException { Scanner scanner = new Scanner(aLine); scanner.useDelimiter("="); if ( scanner.hasNext() ){ String value = scanner.next(); log("" + quote(value.trim()) ); } else { log("Empty or invalid line. Unable to process."); } scanner.close(); } private final File fFile; public static void log(Object aObject) throws FileNotFoundException { System.out.println(String.valueOf(aObject)); } private String quote(String aText){ String QUOTE = ""; return QUOTE + aText + QUOTE; } }
Ball.java
Player.javaCode:package javaapplication2; import java.applet.*; import java.awt.*; import java.util.*; import java.net.*; public class Ball { private int pos_x; // Position of Ball private int pos_y; // Position of Ball private int x_speed; // ball speed (X) private int y_speed; // ball speed (Y) private int radius; private int first_x; // Start x Position private int first_y; // Start y Position private int maxspeed; private final int x_leftout = 10; private final int x_rightout = 380; private final int y_upout = 45; private final int y_downout = 380; Color color; AudioClip out; Player player; Random rnd = new Random (); //Generates random number public Ball (int radius, int x, int y, int vx, int vy, int ms, Color color, AudioClip out, Player player) { this.radius = radius; pos_x = x; pos_y = y; first_x = x; first_y = y; x_speed = vx; y_speed = vy; maxspeed = ms; this.color = color; this.out = out; this.player = player; } public void move () { pos_x += x_speed; pos_y += y_speed; isOut(); } public void ballWa**** () { pos_x = first_x; pos_y = first_y; x_speed = (rnd.nextInt ()) % maxspeed; } public boolean userHit (int maus_x, int maus_y) { //mouse hit detection double x = maus_x - pos_x; double y = maus_y - pos_y; double distance = Math.sqrt ((x*x) + (y*y)); // mouse distance from ball if (distance < 30) { player.addScore (10*Math.abs(x_speed) + 10); return true; } else return false; } private boolean isOut () { if (pos_x < x_leftout) { pos_x = first_x; pos_y = first_y; out.play(); x_speed = (rnd.nextInt ()) % maxspeed; player.looseLife(); return true; } else if (pos_x > x_rightout) { pos_x = first_x; pos_y = first_y; out.play(); x_speed = (rnd.nextInt ()) % maxspeed; player.looseLife(); return true; } else if (pos_y < y_upout) { pos_x = first_x; pos_y = first_y; out.play(); x_speed = (rnd.nextInt ()) % maxspeed; player.looseLife(); return true; } else if (pos_y > y_downout) { pos_x = first_x; pos_y = first_y; out.play(); x_speed = (rnd.nextInt ()) % maxspeed; player.looseLife(); return true; } else return false; } public void DrawBall (Graphics g) { g.setColor (color); g.fillOval (pos_x - radius, pos_y - radius, 2 * radius, 2 * radius); } }
Score.javaCode:package javaapplication2; public class Player { private int score; private int lives; public Player() { lives = 10; score = 0; } public int getScore() { return score; } public int getLives() { return lives; } public void addScore (int plus) { score += plus; } public void looseLife() { lives --; } }
FileReader.javaCode:package javaapplication2; import java.io.Serializable; public class Score implements Serializable { private int score; public int getScore() { return score; } public Score(int score) { this.score = score; } }
ScoreComparator.javaCode:package javaapplication2; import java.util.*; import java.io.*; public class FileReader { private ArrayList<Score> scores; private final String HIGHSCORE_FILE = "scores.txt"; ObjectOutputStream outputStream = null; ObjectInputStream inputStream = null; public FileReader() { scores = new ArrayList<Score>(); } public ArrayList<Score> getScores() { sort(); return scores; } private void sort() { ScoreComparator comparator = new ScoreComparator(); Collections.sort(scores, comparator); } public void addScore(int score) { scores.add(new Score(score)); updateScoreFile(); } public void updateScoreFile() { try { FileWriter file = new FileWriter (HIGHSCORE_FILE); PrintWriter output = new PrintWriter(file); output.println(scores); } catch (FileNotFoundException e) { System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file"); } catch (IOException e) { System.out.println("[Update] IO Error: " + e.getMessage()); } } public String getHighscoreString() { String highscoreString = ""; int max = 3; ArrayList<Score> scores; scores = getScores(); int i = 0; int x = scores.size(); if (x > max) { x = max; } while (i < x) { highscoreString += (i + 1) + "\t\t" + scores.get(i).getScore() + "\n"; i++; } return highscoreString; } }
Now for old files that may still be in use :S i can quite tell:Code:package javaapplication2; import java.util.Comparator; public class ScoreComparator implements Comparator<Score> { public int compare(Score score1, Score score2) { int sc1 = score1.getScore(); int sc2 = score2.getScore(); if (sc1 > sc2){ return -1; }else if (sc1 < sc2){ return +1; }else{ return 0; } } }
HighscoreManager.java
Again any help or advice would be greatly appreciated.Code:package javaapplication2; import java.util.*; import java.io.*; @SuppressWarnings("unchecked") public class HighscoreManager { private ArrayList<Score> scores; private final String HIGHSCORE_FILE = "scores.dat"; ObjectOutputStream outputStream = null; ObjectInputStream inputStream = null; public HighscoreManager() { scores = new ArrayList<Score>(); } public ArrayList<Score> getScores() { loadScoreFile(); sort(); return scores; } private void sort() { ScoreComparator comparator = new ScoreComparator(); Collections.sort(scores, comparator); } public void addScore(int score) { loadScoreFile(); scores.add(new Score(score)); updateScoreFile(); } public void loadScoreFile() { try { inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE)); scores = (ArrayList<Score>) inputStream.readObject(); } catch (FileNotFoundException e) { System.out.println("[HighScore] FNF Error: " + e.getMessage()); } catch (IOException e) { System.out.println("[HighScore] IO Error: " + e.getMessage()); } catch (ClassNotFoundException e) { System.out.println("[HighScore] CNF Error: " + e.getMessage()); } finally { try { if (outputStream != null) { outputStream.flush(); outputStream.close(); } } catch (IOException e) { System.out.println("[HighScore] IO Error: " + e.getMessage()); } } } public void updateScoreFile() { try { outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE)); outputStream.writeObject(scores); } catch (FileNotFoundException e) { System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file"); } catch (IOException e) { System.out.println("[Update] IO Error: " + e.getMessage()); } finally { try { if (outputStream != null) { outputStream.flush(); outputStream.close(); } } catch (IOException e) { System.out.println("[Update] Error: " + e.getMessage()); } } } public String getHighscoreString() { String highscoreString = ""; int max = 3; ArrayList<Score> scores; scores = getScores(); int i = 0; int x = scores.size(); if (x > max) { x = max; } while (i < x) { highscoreString += (i + 1) + "\t\t" + scores.get(i).getScore() + "\n"; i++; } return highscoreString; } }
Kind regards,
Cloakd


LinkBack URL
About LinkBacks




Reply With Quote
then post em to:
, otherwise i'll create it myself.)



Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum