ti ho allegato le due classi che utilizzo allegerite
codice:
class SortAlgorithm {
/**
* oggetto sort.
*/
private SortItem parent;
/**
* Se vero stop ord.
*/
protected boolean stopRequested = false;
/**
* Set padre.
*/
public void setParent(SortItem p) {
parent = p;
}
/**
* Pause .
*/
protected void pause() throws Exception {
if (stopRequested) {
throw new Exception("Sort Algorithm");
}
parent.pause(parent.h1, parent.h2,parent.temp);
}
/**
* Pause .
*/
protected void pause(int H1) throws Exception {
if (stopRequested) {
throw new Exception("Sort Algorithm");
}
parent.pause(H1, parent.h2,parent.temp);
}
/**
* Pause e segna oggetto1 & 2.
*/
protected void pause(int H1, int H2,int T) throws Exception {
if (stopRequested) {
throw new Exception("Sort Algorithm");
}
parent.pause(H1, H2,T);
}
/**
* Stop ord.
*/
public void stop() {
stopRequested = true;
}
/**
* inizializza
*/
public void init() {
stopRequested = false;
}
/**
* metodo chiamato per ord array
*
*/
void sort(int a[]) throws Exception {
}
}
codice:
public class SortItem
extends Applet
implements Runnable, MouseListener {
/**
* thread che ordina).
*/
private Thread kicker;
/**
* array che deve be ordinato
*/
int []arr= new int[13];
int mid=0;
int temp=0;
int app=0;
/**
* Limite sup.
*/
int h1 = -1;
/**
* Limite inf
*/
int h2 = -1;
/**
* nome algo.
*/
String algName;
/**
* algo di ord.
*/
SortAlgorithm algorithm;
Dimension initialSize = null;
private JButton jBStart = null;
private Image img = null;
private JPanel jPCarte = null;
private JRadioButton jRBB = null;
private JRadioButton jRBQ = null;
private JRadioButton jRBH = null;
private JRadioButton jRBI = null;
private JLabel jLabel = null;
ButtonGroup bc = new ButtonGroup( );
private JButton jBNuovoOrd = null;
String at = "";
private JSlider jSlider = null;
private JLabel jLabel1 = null;
int sleep=0;
int paint=0;
/**
* Questo metodo genera i numeri da 1 a 13
*/
public void GenNum (){
Random rgen = new Random();
for (int i=0; i<arr.length; i++) {
arr[i] = i+1;
}
for (int i=0; i<arr.length; i++) {
int randomPosition = rgen.nextInt(arr.length);
int temp = arr[i];
arr[i] = arr[randomPosition];
arr[randomPosition] = temp;
}
}
/**
* Pause
* @see SortAlgorithm
*/
void pause() {
pause(-1, -1,0);
}
/**
* Pause
* @see SortAlgorithm
*/
void pause(int H1) {
pause(H1, -1,0);
}
/**
* Pause
* @see SortAlgorithm
*/
void pause(int H1, int H2,int T) {
h1 = H1;
h2 = H2;
temp=T;
if (kicker != null) {
//System.out.println("pause");
repaint();
}
//System.out.println("Sleep"+sleep);
try {Thread.sleep(sleep);} catch (InterruptedException e){}
}
/**
* Initialize the applet.
*/
public void init() {
this.setLayout(null);
this.setSize(new Dimension(1006, 501));
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(445, 111, 206, 24));
jLabel1.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
jLabel1.setText("Seleziona la velocità di esecuzione");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(29, 26, 234, 19));
jLabel.setText("Seleziona l'algoritmo");
this.setName("PSfondo");
this.add(getJBStart(), null);
this.add(getJPCarte(), null);
this.setVisible(true);
this.add(getJRBB(), null);
this.add(getJRBQ(), null);
this.add(getJRBH(), null);
this.add(getJRBI(), null);
this.add(jLabel, null);
this.add(getJBNuovoOrd(), null);
//addMouseListener(this);
this.add(getJSlider(), null);
this.add(jLabel1, null);
bc.add(jRBB);
bc.add(jRBH);
bc.add(jRBQ);
bc.add(jRBI);
jBNuovoOrd.setEnabled(false);
this.setVisible(true);
GenNum();
}
public void start() {
h1 = h2 = -1;
GenNum();
//System.out.println("start"+"indice:"+h1+" "+ h2);
repaint();
showStatus(getParameter("alg"));
}
/**
* Deallocate resources of applet.
*/
public void destroy() {
removeMouseListener(this);
}
/**
* Paint the array of numbers as a list
* of horizontal lines of varying lengths.
*/
public void paintComponent(Graphics g) {
super.paintComponents(g);
int pos=50;
int a[] = arr;
... ho eliminato tutta la parte dei disegni
}
/**
* Update without erasing the background.
*/
public void update(Graphics g) {
// System.out.println("update");
paintComponent(g);
}
/**
* Run the sorting algorithm. This method is
* called by class Thread once the sorting algorithm
* is started.
* @see java.lang.Thread#run
* @see SortItem#mouseUp
*/
public void run() {
sleep=jSlider.getValue()*1000;
try {
if (algorithm == null) {
algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
algorithm.setParent(this);
}
algorithm.init();
algorithm.sort(arr);
} catch(Exception e) {
}
}
/**
* Stop the applet. Kill any sorting algorithm that
* is still sorting.
*/
public synchronized void stop() {
if (algorithm != null){
try {
algorithm.stop();
} catch (IllegalThreadStateException e) {
// ignore this exception
}
kicker = null;
}
}
/**
* For a Thread to actually do the sorting. This routine makes
* sure we do not simultaneously start several sorts if the user
* repeatedly clicks on the sort item. It needs to be
* synchronized with the stop() method because they both
* manipulate the common kicker variable.
*/
private synchronized void startSort() {
if (kicker == null || !kicker.isAlive()) {
kicker = new Thread(this);
kicker.start();
}
}
public void mouseClicked(MouseEvent e) {
showStatus(getParameter("alg"));
}