Closed Thread
Results 1 to 6 of 6

Thread: Problems That Are Driving Me Insane...

  1. #1
    Cloakd is offline Newbie
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    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 07:56 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,875
    Rep Power
    24

    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:
    <email removed by myself> (if you're mailing, that scores.dat file might also be interesting , otherwise i'll create it myself.)
    Last edited by wim DC; 10-15-2010 at 08:10 AM.

  4. #3
    Cloakd is offline Newbie
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    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.

  5. #4
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,875
    Rep Power
    24

    Re: Problems That Are Driving Me Insane...

    to run it you create a new main, then call init() then start()?

  6. #5
    Cloakd is offline Newbie
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0
    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 05:24 PM. Reason: Triple post

  7. #6
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Problems That Are Driving Me Insane...

    He was asking you a question, trying to verify what was supposed to happen.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Switched to Chrome- Insane!
    By bbqroast in forum Technology Ramble
    Replies: 5
    Last Post: 12-30-2011, 09:10 AM
  2. Please help this is driving me insane!
    By Carlo717 in forum PHP Development
    Replies: 4
    Last Post: 11-06-2010, 11:12 AM
  3. Looking for web developers for my project www.insane-prize.com
    By harnorno2 in forum Community Projects
    Replies: 0
    Last Post: 05-31-2010, 06:00 PM
  4. Insane php/ajax person wanted to take a risk
    By stevebassi in forum Services for Buy/Sell/Trade
    Replies: 2
    Last Post: 01-17-2009, 05:21 AM
  5. What car are you driving?
    By Oigen in forum The Lounge
    Replies: 11
    Last Post: 07-28-2008, 11:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts