Highscore

Java Kaffee

Mitglied
Hier habe ich Quelltext für einen (einfachen) Highscore.
Er ist noch nicht ganz perfekt, aber für nicht allzu hohe Ansprüche geeignet.

Das ist der Quelltext der Highscore.java(Basisklasse):
Java:
package highscore;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;

public class Highscore {
	/*
	 * Fields
	 */
	private static final String FILE_NAME = "Highscore.sco";
	private List<HighscoreEntry> mylist = null;
	public static final int SORT_DESC = 1;
	public static final int SORT_ASC = 0;
	private static int MaxHighscoreEntries = 10;
	private int sortOrder;

	/*
	 * Fields End
	 */
	/*
	 * Constructors
	 */

	/**
	 * @param int hm (is how much entries can be in the Highscore)
	 */
	public Highscore(int hm, int sortOrder) {
		this.sortOrder = sortOrder;
		MaxHighscoreEntries = hm;
		loadMyList();
		Collections.sort(mylist);
	}

	/**
	 * @param int hm (is how much entries can be in the Highscore)
	 */
	public Highscore(int hm) {
		this(hm, SORT_DESC);
	}

	public Highscore() {
		this(MaxHighscoreEntries);
	}

	/*
	 * Constructors End
	 */
	/*
	 * Methods
	 */

	/**
	 * @param HighscoreEntry
	 *            newentry
	 * 
	 */
	public void addEntry(HighscoreEntry newentry) {
		mylist.add(newentry);
		mylist = sortEntries(mylist);
		try {
			mylist.remove(MaxHighscoreEntries);
		} catch (Exception e) {
		}
		saveMyList();
	}

	/**
     * 
     */
	private void createDefaultList() {
		mylist = new ArrayList<HighscoreEntry>();
	}

	/**
     * 
     */
	@SuppressWarnings("unchecked")
	private void loadMyList() {
		try {
			FileInputStream fIn = new FileInputStream(FILE_NAME);
			ObjectInputStream in = new ObjectInputStream(fIn);
			mylist = (List<HighscoreEntry>) in.readObject();
		} catch (FileNotFoundException e1) {
			createDefaultList();
		} catch (IOException e2) {
			createDefaultList();
		} catch (ClassNotFoundException e3) {
			createDefaultList();
		}
	}

	private void saveMyList() {
		try {
			// oder
			FileOutputStream fOut = new FileOutputStream(FILE_NAME);
			ObjectOutput out = new ObjectOutputStream(fOut);
			out.writeObject(mylist);
			out.flush();
			out.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();

		} catch (IOException e2) {
			e2.printStackTrace();
		}
	}


	public List<HighscoreEntry> getMylist() {
		return mylist;
	}

	public String toString() {
		StringBuffer r = new StringBuffer();
		for (HighscoreEntry hse : mylist) {
			r.append(hse.toString());

		}
		return r.toString();
	}

	public List<HighscoreEntry> sortEntries(List<HighscoreEntry> list) {
		List<HighscoreEntry> l_return;
		Collections.sort(list);
		if (sortOrder == SORT_ASC) {
			l_return = new ArrayList<HighscoreEntry>();
			for (int x = list.size(); x > 0; x--) {
				l_return.add(list.get(x - 1));
			}
		} else {
			l_return = list;
		}

		return l_return;
	}
	
	public void showHighscores(JFrame frame,String title) {
		new HighscoreDialog(frame,true,mylist,title);
	}
	/*
	 * Methods End
	 */
}

Das ist der von der HighscoreEntry.java(Erzeugt einen Eintrag in dem Highscore):
Java:
package highscore;

import java.io.Serializable;

public class HighscoreEntry implements Comparable<HighscoreEntry> , Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1235567L;
	
	private String name;
	private int points;

	public HighscoreEntry(String name, int points) {
		this.name = name;
		this.points = points;
	}

	protected int getPoints() {
		return points;
	}

	protected String getName() {
		return name;
	}

	public String toString() {
		StringBuffer n = new StringBuffer();
		n.append(getName());
		do {
			n.append(" ");
		}while(n.length()< 30);
		
		return n.toString() +"\t\t\t"+ getPoints() + "\n";
	}
	
	@Override
	public int compareTo(HighscoreEntry compareObject) {
		if (getPoints() < compareObject.getPoints()) 
			return 1; 
		else if (getPoints() == compareObject.getPoints()) 
			return 0; 
		else return -1; 	}
}

Und das ist der von der HighscoreDialog.java(zeigt den Highscore in einem Fenster an.):
Java:
package highscore;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class HighscoreDialog extends JDialog implements ActionListener {
    /**
	 * 
	 */
	private static final long serialVersionUID = 7507064737909415827L;
	private JPanel myPanel = null;
    private JButton yesButton = null;
    private JButton noButton = null;
    private boolean answer = false;
    public boolean getAnswer() { return answer; }

    public HighscoreDialog(JFrame frame, boolean modal, List<HighscoreEntry> scores,String title) {
	super(frame, modal);
	myPanel = new JPanel();
	String header[] = {"Name", title};
	
	String data[][] = new String[scores.size()][2];
	int i = 0;
	for (HighscoreEntry hse : scores) {
		data[i][0] = hse.getName();
		data[i][1] = String.valueOf(hse.getPoints());
		i++;
	}
	DefaultTableModel model = new DefaultTableModel(data, header);
	JTable jt = new JTable( model) {
        /**
		 * 
		 */
		private static final long serialVersionUID = -940668665876271544L;

		public boolean isCellEditable(int x, int y) {
            return false;
        }
    };

	
    
	
	JScrollPane pane = new JScrollPane( jt );
    getContentPane().add( pane );
	
	yesButton = new JButton("Yes");
	yesButton.addActionListener(this);
	myPanel.add(yesButton);	
	noButton = new JButton("No");
	noButton.addActionListener(this);
	myPanel.add(noButton);	
	pack();
	setLocationRelativeTo(frame);
	setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
	if(yesButton == e.getSource()) {
	    System.err.println("User chose yes.");
	    answer = true;
	    setVisible(false);
	}
	else if(noButton == e.getSource()) {
	    System.err.println("User chose no.");
	    answer = false;
	    setVisible(false);
	}
    }
    
}
Bitte sagt mir ehrlich, was ihr von diesem Highscore haltet.
 

jannig10

Mitglied
ne blöde frage, aber wie binde ich das ein?

ich dachte mit extends bei meiner oberklasse, also spielfeld.
also ich hab in meinem spielfeld die actionlistener und dachte man soll irgendwie diese dann so ansprechen können.
ich hab auch eine klasse sprite in dem alle figuren auch vererben.
 
Zuletzt bearbeitet:

Ähnliche Java Themen


Oben