Here is the code it i long but i think the problem is somewhere in the RectangleDemo class in the buildUI() but cant figure it out? It will start up if i run it through linux and work properly but wont work through a windows machine on blueJ. Dont know why. But after refreching it three or four time times or after three or four operations is crashes quite unusual behavior
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.Button;
import java.awt.Label;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
public class RectangleDemo extends JApplet implements ActionListener
{
Container container;
JLabel label;
JLabel Fast;
JLabel Slow;
JLabel speedlabel;
JLabel valcomp;
JButton swapbutton;
JButton refresh;
JButton stop;
JButton pause, next;
JSlider slider;
JPanel comparisons;
JPanel animate;
JPanel code;
JPanel buttons;
JPanel buttons2;
JPanel control;
JPanel speedcontrol;
JComboBox algorithms;
RectangleArea rectangleArea;
Sort sort;
boolean stopRequested = false;
boolean paused = false;
boolean started = false;
int compares = 0;
int rate;
String name = "Insertion Sort";
Mutex mutex = new Mutex();
Font f = new Font("MS Sans Serif Western", 0, 11);
public void init()
{
container = getContentPane();
buildUI();
}
void buildUI()
{
WindowUtilities.setNativeLookAndFeel();
container.setLayout(null);
container.setBackground(Color.white);
animate = new JPanel();
animate.setLayout(new BorderLayout());
comparisons = new JPanel();
comparisons.setBackground(Color.white);
comparisons.setForeground(Color.red);
Font font = new Font("Times New Roman", 0, 14);
comparisons.setFont(font);
comparisons.setLayout(new BoxLayout(comparisons, BoxLayout.X_AXIS));
rectangleArea = new RectangleArea(this);
rectangleArea.setBackground(Color.white);
rectangleArea.setLayout(null);
Insets rectInsets = container.getInsets();
animate.add(rectangleArea, BorderLayout.CENTER);
swapbutton = new JButton("Sort ");
swapbutton.setFont(new Font(f.getName(), f.getStyle(), f.getSize()+3));
swapbutton.addActionListener(this);
swapbutton.setBounds(280 + rectInsets.left, 270 + rectInsets.top, 110, 25);
rectangleArea.add(swapbutton);
refresh = new JButton("Refresh");
refresh.setFont(f);
refresh.addActionListener(this);
refresh.setBounds(280 + rectInsets.left, 600 + rectInsets.top, 110, 25);
rectangleArea.add(refresh);
stop = new JButton("STOP ");
stop.addActionListener(this);
stop.setFont(f);
stop.setBounds(280 + rectInsets.left, 350 + rectInsets.top, 110, 25);
rectangleArea.add(stop);
pause = new JButton("Pause | |");
pause.setFont(f);
pause.addActionListener(this);
pause.setBounds(160 + rectInsets.left, 350 + rectInsets.top, 110, 25);
rectangleArea.add(pause);
next = new JButton("Next >>");
next.setFont(f);
next.addActionListener(this);
next.setBounds(40 + rectInsets.left, 350 + rectInsets.top, 110, 25);
rectangleArea.add(next);
String[] algStrings = {"Insertion Sort" , "Quick Sort" , "Bubble Sort" , "Bi-directional Bubble Sort" , "Merge Sort" };
algorithms = new JComboBox(algStrings);
algorithms.addActionListener(this);
algorithms.setFont(new Font(f.getName(), f.getStyle(), f.getSize()+1));
algorithms.setBackground(Color.white);
algorithms.setBounds(30 + rectInsets.left, 270 + rectInsets.top, 200, 25);
rectangleArea.add(algorithms);
slider = new JSlider(JSlider.VERTICAL, 0, 4, 0);
slider.setPaintTicks(false);
slider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent evt)
{
JSlider slider = (JSlider)evt.getSource();
if (!slider.getValueIsAdjusting())
{
mutex.acquire();
rate = slider.getValue();
mutex.release();
}
}
});
Fast = new JLabel("Fast ");
Fast.setBounds(460 + rectInsets.left, 320 + rectInsets.top, 50, 25);
rectangleArea.add(Fast);
Slow = new JLabel("Slow ");
Slow.setBounds(460 + rectInsets.left, 500 + rectInsets.top, 50, 25);
rectangleArea.add(Slow);
slider.setBounds(430 + rectInsets.left, 300 + rectInsets.top, 20, 250);
rectangleArea.add(slider);
JLabel numberofcomp = new JLabel(" Number of Comparisons : ");
numberofcomp.setFont(font);
numberofcomp.setForeground(Color.red);
comparisons.add(numberofcomp);
String compares1 = Integer.toString(compares);
valcomp = new JLabel(compares1);
valcomp.setForeground(Color.red);
comparisons.add(valcomp);
comparisons.setBounds(40 + rectInsets.left, 420 + rectInsets.top, 300, 25);
rectangleArea.add(comparisons);
Insets insets = container.getInsets();
animate.setBounds(0 + insets.left, 0 + insets.top, 930, 780);
container.add(animate);
}
public void actionPerformed(ActionEvent i)
{
if(i.getSource() == swapbutton)
{
if((started == false))
{
started = true;
sort = new Sort(this);
name = (String) algorithms.getSelectedItem();
sort.start();
}
}
else
if(i.getSource() == refresh)
{
stopRequested = true;
started = false;
int counter = 0;
compares = 0;
valcomp.setText(Integer.toString(compares));
while(counter <= rectangleArea.num.length-1)
{
int y = ((counter + 1) * 3);
Element e = new Element(counter,y);
rectangleArea.num[counter] = e;
counter++;
}
rectangleArea.randomise();
rectangleArea.current1 = 55;
rectangleArea.current2 = 55;
rectangleArea.leftarrow = 55;
rectangleArea.rightarrow = 55;
rectangleArea.temp.size = 0;
rectangleArea.pivot.size = 0;
rectangleArea.highlight = -1;
rectangleArea.repaint();
}
else
if(i.getSource() == stop)
{
stopRequested = true;
}
else
if(i.getSource() == next)
{
paused = false;
try{sort.sleep(20);}
catch(InterruptedException e){}
paused = true;
}
else
if(i.getSource() == pause)
{
if(paused)
{
paused = false;
pause.setText("Pause | |");
}
else
{
paused = true;
pause.setText("Resume >");
}
}
else
if(i.getSource() == algorithms)
{
if(started == false)
{
name = (String) algorithms.getSelectedItem();
rectangleArea.repaint();
}
}
}
public void insertionSort()
{
highlight(0);
highlight(1);
for(int i = 1; i <= rectangleArea.num.length; i++)
{
highlight(2);
highlight(3);
while(paused)
{
}
rectangleArea.leftarrow = i;
rectangleArea.current1 = i;
rectangleArea.paintImmediately(0,0,500,600);
highlight(4);
int counter = 0;
highlight(5);
rectangleArea.moveElement(i, rectangleArea.temp.i);
highlight(6);
int j = i;
highlight(7);
while(j > 0 && rectangleArea.temp.size < rectangleArea.num[j-1].size && !stopRequested)
{
highlight(8);
compares++;
valcomp.setText(Integer.toString(compares));
while(paused)
{
}
highlight(9);
highlight(10);
rectangleArea.moveElement((j-1),j);
highlight(11);
j = j-1;
highlight(12);
counter++;
highlight(13);
}
highlight(8);
highlight(14);
compares++;
valcomp.setText(Integer.toString(compares));
if(counter > 0)
{
highlight(15);
highlight(16);
rectangleArea.moveElement(rectangleArea.temp.i, j);
highlight(17);
}
highlight(14);
if(stopRequested)
{
stopRequested = false;
started = false;
break;
}
highlight(18);
}
highlight(2);
started = false;
highlight(19);
highlight(20);
highlight(-1);
}
public void quickSort(int left, int right)
{
if(right > (left) && (stopRequested == false))
{
int p = partition(left, right);
quickSort(left, p - 1);
quickSort(p + 1, right);
}
if((left == 0) && (right == 49))
{
started = false;
}
}
public int partition(int left, int right)
{
while(paused){}
rectangleArea.moveElement(right, rectangleArea.pivot.i);
int pL = left;
rectangleArea.current1 = pL;
rectangleArea.leftarrow = pL;
int pR = right;
rectangleArea.current2 = pR;
rectangleArea.rightarrow = pR;
rectangleArea.paintImmediately(0,0,500,600);
while(pL < pR && !stopRequested)
{
while(paused){}
while(rectangleArea.num[pL].size < rectangleArea.pivot.size && !stopRequested)
{
compares++;
valcomp.setText(Integer.toString(compares));
while(paused){}
pL++;
rectangleArea.current1 = pL;
rectangleArea.leftarrow = pL;
rectangleArea.paintImmediately(0,0,500,600);
}
compares++;
valcomp.setText(Integer.toString(compares));
while(rectangleArea.num[pR].size >= rectangleArea.pivot.size && pR > left && !stopRequested)
{
compares++;
valcomp.setText(Integer.toString(compares));
while(paused){}
pR--;
rectangleArea.current2 = pR;
rectangleArea.rightarrow = pR;
rectangleArea.paintImmediately(0,0,500,600);
}
compares++;
valcomp.setText(Integer.toString(compares));
if(stopRequested){break;}
if(pL < pR)
{
rectangleArea.moveElement(rectangleArea.num[pR].i, rectangleArea.temp.i);
rectangleArea.moveElement(pL,pR);
rectangleArea.current2 = pR;
rectangleArea.repaint();
rectangleArea.moveElement(rectangleArea.temp.i, pL);
rectangleArea.current1 = pL;
}
}
rectangleArea.moveElement(pL, right);
rectangleArea.moveElement(rectangleArea.pivot.i, pL);
return pL;
}
public void bubbleSort()
{
highlight(0);
highlight(1);
for(int i = 0; i < (rectangleArea.num.length - 1); i++)
{
highlight(2);
highlight(3);
while(paused){}
if(stopRequested){break;}
for(int j = 0; j < (rectangleArea.num.length - 1 - i); j++)
{
highlight(4);
highlight(5);
while(paused){}
if(stopRequested){break;}
rectangleArea.current1 = j+1;
rectangleArea.paintImmediately(0,0,500,600);
if(rectangleArea.num[j+1].size < rectangleArea.num[j].size)
{
highlight(6);
highlight(7);
highlight(8);
rectangleArea.moveElement(j+1, rectangleArea.temp.i);
highlight(9);
rectangleArea.moveElement(j, j+1);
highlight(10);
rectangleArea.moveElement(rectangleArea.temp.i, j);
highlight(11);
}
compares++;
valcomp.setText(Integer.toString(compares));
highlight(12);
}
highlight(4);
highlight(13);
}
highlight(2);
highlight(14);
rectangleArea.current1 = 55;
started = false;
highlight(-1);
}
public void biBubbleSort()
{
int j;
int limit = rectangleArea.num.length;
int st = -1;
while (st < limit)
{
while(paused){}
if(stopRequested){break;}
boolean swapped = false;
st++;
limit--;
for (j = st; j < limit; j++)
{
while(paused){}
if(stopRequested){break;}
if (rectangleArea.num[j].size > rectangleArea.num[j + 1].size)
{
rectangleArea.moveElement(rectangleArea.num[j].i, rectangleArea.temp.i);
rectangleArea.moveElement(rectangleArea.num[j + 1].i,rectangleArea.num[j].i);
rectangleArea.moveElement(rectangleArea.temp.i,rectangleArea.num[j + 1].i);
swapped = true;
}
compares++;
valcomp.setText(Integer.toString(compares));
}
if (!swapped)
{
return;
}
for (j = limit; --j >= st
{
while(paused){}
if(stopRequested){break;}
if (rectangleArea.num[j].size > rectangleArea.num[j + 1].size)
{
rectangleArea.moveElement(rectangleArea.num[j].i, rectangleArea.temp.i);
rectangleArea.moveElement(rectangleArea.num[j + 1].i, rectangleArea.num[j].i);
rectangleArea.moveElement(rectangleArea.temp.i, rectangleArea.num[j + 1].i);
swapped = true;
}
compares++;
valcomp.setText(Integer.toString(compares));
}
if (!swapped)
{
return;
}
}
rectangleArea.current1 = 55;
started = false;
}
public void mergeSort(Element num[], int low, int high)
{
/*
* * @(#)MergeSortAlgorithm.java 1.0 95/06/23 Jason Harrison
* * *
* * ** Copyright (c) 1995 University of British Columbia
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* UBC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UBC SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/**
* A merge sort demonstration algorithm
* SortAlgorithm.java, Thu Oct 27 10:32:35 1994
* @author Jason
Harrison@cs.ubc.ca * @version 1.1, 12 Jan 1998
**/
int lo = low;
int hi = high;
if (lo >= hi)
{
return;
}
int mid = (lo + hi) / 2;
/*
* Partition the list into two lists and sort them recursively
*/
mergeSort(num,lo, mid);
mergeSort(num,mid + 1, hi);
/*
* Merge the two sorted lists
*/
int end_lo = mid;
int start_hi = mid + 1;
while ((lo <= end_lo) && (start_hi <= hi))
{
while(paused){}
if(stopRequested){break;}
if (num[lo].size < num[start_hi].size)
{
lo++;
}
else
{
/*
* a[lo] >= a[start_hi]
* The next element comes from the second list,
* move the a[start_hi] element into the next
* position and shuffle all the other elements up.
*/
rectangleArea.moveElement(num[start_hi].i, rectangleArea.temp.i);
for (int k = start_hi - 1; k >= lo; k--)
{
while(paused){}
if(stopRequested){break;}
rectangleArea.moveElement(num[k].i, num[k+1].i);
}
rectangleArea.moveElement(rectangleArea.temp.i, num[lo].i);
lo++;
end_lo++;
start_hi++;
}
compares++;
valcomp.setText(Integer.toString(compares));
}
}
public void highlight(int x)
{
rectangleArea.highlight(x);
while(paused){}
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.Button;
import java.awt.Label;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
class RectangleArea extends JPanel
{
RectangleDemo controller;
Element [] num;
Element temp = new Element(52,0);
Element pivot = new Element(57,0);
int leftarrow = 55;
int rightarrow = 55;
int [] leftxpoints = new int[3];
int [] rightxpoints = new int[3];
int [] ypoints = new int[3];
int current1 = 55;
int current2 = 55;
BufferedReader reader;
String info;
String file;
int highlight = -1;
public RectangleArea(RectangleDemo controller)
{
this.controller = controller;
num = new Element[50];
int counter = 0;
while(counter <= num.length-1)
{
int y = ((counter + 1) * 3);
Element e = new Element(counter,y);
num[counter] = e;
counter++;
}
randomise();
}
public void randomise()
{
Element replace = new Element(60,0);
for(int i = 0; i < (num.length - 1); i++)
{
int x = (int) (Math.random()*50);
replace = num[i];
replace.i = num[x].i;
num[i] = num[x];
num[i].i = i;
num[x] = replace;
num[i].setX();
num[x].setX();
}
paintImmediately(0,0,500,500);
}
public void paintComponent(Graphics g)
{
int i = 0;
super.paintComponent(g);
g.drawLine(15,200,420,200); // x axis
g.drawLine(15,20,15,200); // y axis
g.drawRect(20,255,400,420); // buttons rectangle
// Temp variable and pivot display
g.setColor(Color.red);
g.fillRect(temp.x, temp.y, temp.width, temp.size);
if(temp.size > 0)
{
g.setColor(Color.black);
g.drawRect(temp.outlinex, temp.outliney, temp.outlinewidth, temp.outlinesize);
}
g.setColor(Color.green);
g.fillRect(pivot.x, pivot.y, pivot.width, pivot.size);
if(pivot.size > 0)
{
g.setColor(Color.black);
g.drawRect(pivot.outlinex, pivot.outliney, pivot.outlinewidth, pivot.outlinesize);
}
g.setColor(new Color(155,0,75));
g.drawRect(427, 40, 20, 170);
Font f = g.getFont();
Font d = new Font("Times New Roman", 1, f.getSize()+2);
g.setFont(d);
g.drawString("Temp",424,225);
if(controller.name == "Quick Sort")
{
g.setColor(Color.green);
g.drawRect(466, 40, 20, 170);
g.drawString("Pivot",463,225);
}
while(i < num.length)
{
g.setColor(Color.blue);
if(i == current1 || i == current2)
{
g.setColor(Color.black);
g.drawRect(num[i].outlinex, num[i].outliney, num[i].outlinewidth, num[i].outlinesize);
g.setColor(Color.red);
}
g.fillRect(num[i].x, num[i].y, num[i].width, num[i].size);
i++;
}
// draw arrows
if(leftarrow <=49 || rightarrow <=49)
{
leftxpoints[0] = (leftarrow * 8) + 15;
leftxpoints[1] = leftxpoints[0] + 6;
leftxpoints[2] = leftxpoints[0] + 12;
rightxpoints[0] = (rightarrow * 8) + 15;
rightxpoints[1] = rightxpoints[0] + 6;
rightxpoints[2] = rightxpoints[0] + 12;
ypoints[0] = 220;
ypoints[1] = 200;
ypoints[2] = 220;
}
if(leftarrow <= 49)
{
g.setColor(Color.red);
g.fillPolygon(leftxpoints, ypoints, 3);
}
if(rightarrow <= 49)
{
g.setColor(Color.cyan);
g.fillPolygon(rightxpoints, ypoints, 3);
}
//CodeArea
g.setColor(Color.red);
g.drawRect(500,5,380,670);
file = "C:/Java/BlueJ/examples/Applet/InsertionSort.txt";
// /pwp/tier-1/pwpstore3/21//mac2000/
System.out.println(file);
if(highlight >= 0)
{
g.setColor(Color.yellow);
g.fillRect(515,(12 + (highlight*15)),360,15);
}
try
{
g.setColor(Color.black);
reader = new BufferedReader(new FileReader(file));
int y = 25;
int x = 525;
while((info = reader.readLine()) != null)
{
g.drawString(info,x,y);
y = y + 15;
}
}
catch(IOException exc)
{
System.out.println("Could not open FileReader");
}
}
public void moveElement(int i, int j)
{
current1 = i;
int moveto = j;
paintImmediately(0,0,500,500);
if(current1 != temp.i && current1 != pivot.i)
{
if(current1 < moveto)
{
while(num[current1].x <= (8*(moveto) + 15))
{
num[current1].x = num[current1].x + 4;
paintImmediately(0,0,900,600);
controller.mutex.acquire();
try{controller.sort.sleep(20*(4 - controller.rate));}
catch(InterruptedException e){}
controller.mutex.release();
}
}
else
if(current1 > moveto)
{
while(num[current1].x >= (8*(moveto) + 23))
{
num[current1].x = num[current1].x - 4;
paintImmediately(0,0,500,500);
controller.mutex.acquire();
try{controller.sort.sleep(20*(4 - controller.rate));}
catch(InterruptedException e){}
controller.mutex.release();
}
}
paintImmediately(0,0,500,500);
Element replace = new Element(0,0);
replace.i = moveto;
replace.size = num[current1].size;
replace.setX();
num[current1].i = num[current1].place;
num[current1].setX();
if(moveto == temp.i)
{
temp = replace;
}
else
if(moveto == pivot.i)
{
pivot = replace;
}
else
{
num[moveto] = replace;
}
current2 = moveto;
paintImmediately(0,0,500,500);
}
else
if(current1 == temp.i)
{
while(temp.x >= (8*(moveto) + 23))
{
temp.x = temp.x - 4;
paintImmediately(0,0,500,500);
controller.mutex.acquire();
try{controller.sort.sleep(20*(4 - controller.rate));}
catch(InterruptedException e){}
controller.mutex.release();
}
Element replace = new Element(0,0);
replace.i = moveto;
replace.size = temp.size;
replace.setX();
temp.i = temp.place;
temp.setX();
num[moveto] = replace;
current2 = moveto;
paintImmediately(0,0,500,500);
}
else
if(current1 == pivot.i)
{
while(pivot.x >= (8*(moveto) + 23))
{
pivot.x = pivot.x - 4;
paintImmediately(0,0,500,500);
controller.mutex.acquire();
try{controller.sort.sleep(20*(4 - controller.rate));}
catch(InterruptedException e){}
controller.mutex.release();
}
Element replace = new Element(0,0);
replace.i = moveto;
replace.size = pivot.size;
replace.setX();
pivot.i = pivot.place;
pivot.setX();
num[moveto] = replace;
current2 = moveto;
paintImmediately(0,0,500,500);
}
current1 = 55;
current2 = 55;
repaint();
}
public void highlight(int x)
{
highlight = x;
paintImmediately(0,0,900,600);
try{controller.sort.sleep(50*(4 - controller.rate));}
catch(InterruptedException e){}
}
}