Go Back   CodeCall Programming Forum > Software Development > Java Help
Register Blogs Search Today's Posts Mark Forums Read

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-2009, 03:43 PM
Newbie
 
Join Date: Sep 2009
Posts: 3
Cloakd is an unknown quantity at this point
Problems That Are Driving Me Insane...

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
Code:
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);
	}

}
Player.java
Code:
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 --;
	}
}
Score.java
Code:
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;
    }
}
FileReader.java
Code:
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;
}
}
ScoreComparator.java
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;
            }
        }
}
Now for old files that may still be in use :S i can quite tell:
HighscoreManager.java
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;
}
}
Again any help or advice would be greatly appreciated.
Kind regards,
Cloakd

Last edited by Cloakd; 09-25-2009 at 10:56 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-24-2009, 05:53 PM
oxano's Avatar
Programmer
 
Join Date: May 2009
Age: 19
Posts: 145
oxano is on a distinguished road
Re: Problems That Are Driving Me Insane...

Yea, i'd really like those other classes cause without those pretty much all the code is showed red as wrong because my program doesn't find those classes :s
If you don't like to post your whole program into the world wide web then post em to:
wimdeclercq@hotmail.com (if you're mailing, that scores.dat file might also be interesting , otherwise i'll create it myself.)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-25-2009, 10:56 AM
Newbie
 
Join Date: Sep 2009
Posts: 3
Cloakd is an unknown quantity at this point
Re: Problems That Are Driving Me Insane...

Scores.dat is just an empty File and i have just edited the post with all the classes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-25-2009, 04:11 PM
oxano's Avatar
Programmer
 
Join Date: May 2009
Age: 19
Posts: 145
oxano is on a distinguished road
Re: Problems That Are Driving Me Insane...

to run it you create a new main, then call init() then start()?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-26-2009, 08:52 AM
Newbie
 
Join Date: Sep 2009
Posts: 3
Cloakd is an unknown quantity at this point
no offence but the run(); command and the init(); command are allready implamented so that was no help atall.

bump?

bump

Last edited by WingedPanther; 10-02-2009 at 08:24 PM.. Reason: Triple post
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-02-2009, 08:27 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Problems That Are Driving Me Insane...

He was asking you a question, trying to verify what was supposed to happen.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
What car are you driving? Oigen The Lounge 11 07-28-2008 02:14 PM
Ubuntu 8 problems? Prog Linux Installation & Configuration 9 07-22-2008 05:36 AM
Linux and Video card problems! TcM Linux/Unix General 6 07-22-2008 05:19 AM
Few problems ... Algorithm and Data Structure nt_virus C# Programming 12 03-31-2008 12:59 PM


All times are GMT -5. The time now is 11:01 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0