Cześć wszystkim,
Próbuję napisać program, który będzie wizualizował różne typy sortowań, na początek sortowanie bąbelkowe. Ma to wyglądać podobnie jak na tej http://cg.scs.carleton.ca/~morin/misc/sortalg/ stronie. Niestety jeszcze średnio sobie radzę z tym wszystkim, w poniższym programie wyskakuje mi błąd NullPointerException w klasie BubbleSortRunnable, nie mam pojęcia jak to naprawić... Każda pomoc, wskazówka będzie bardzo przydatna:) Dziękuję z góry!
public abstract class Sort
{
protected class wspolrzedne
{
double x2;
double y;
}
protected wspolrzedne a[]; //współrzędne wszystkich linii
protected boolean sorted;
public Sort(int width, int height)
{
int j=width;
int ilosc=0;
for(int i=height;i>=0;i=i-20)
ilosc++;
a=new wspolrzedne[ilosc];
for(int i=0;i<a.length;i++)
a[i]=new wspolrzedne();
int k=0;
for(int i=height;i>=0;i=i-20)
{
a[k].x2=j;
a[k].y=i;
j=j-20;
k++;
}
sorted=true;
}
public void mixArray()
{
Random generator=new Random();
for(int k=0;k<3*a.length;k++)
{
int i=generator.nextInt(a.length);
int j=generator.nextInt(a.length);
double temp=a[i].y;
a[i].y=a[j].y;
a[j].y=temp;
wspolrzedne temp2=a[i];
a[i]=a[j];
a[j]=temp2;
}
sorted=false;
}
}
public class BubbleSortRunnable extends Sort implements Runnable {
private Component component;
public BubbleSortRunnable(int width,int height,Component c)
{
super(width,height);
}
public void run()
{
try {
for(int j = 0; j < a.length - 1; j++)
for(int i = 0; i < a.length - 1; i++)
{
if(a[i].x2 > a[i + 1].x2)
{
double temp=a[i].y;
a[i].y=a[i+1].y;
a[i+1].y=temp;
wspolrzedne temp2=a[i];
a[i]=a[i+1];
a[i+1]=temp2;
}
component.repaint();
Thread.sleep(10);
}
}
catch(InterruptedException e)
{
}
}
}
public class SortPanel extends JPanel {
private Sort s;
public void add(Sort s)
{
this.s=s;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setStroke(new BasicStroke(5.0F,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
for(int i=0;i<s.a.length;i++) {
g2.draw(new Line2D.Double(50,s.a[i].y+5,s.a[i].x2+50,s.a[i].y+5));
}
}
}
public class SortFrame extends JFrame{
private BubbleSortRunnable r;
private SortPanel panel;
public SortFrame() throws InterruptedException
{
setSize(400,400);
setTitle("Sortowanie Bąbelkowe");
panel=new SortPanel();
r=new BubbleSortRunnable(400,400,panel);
r.mixArray();
panel.add(r);
add(panel,BorderLayout.CENTER);
JButton button=new JButton("Start");
add(button,BorderLayout.WEST);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
try {
run();
}
catch (InterruptedException ex) {
Logger.getLogger(SortFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void run() throws InterruptedException
{
Thread t=new Thread(r);
t.start();
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
SortFrame window = new SortFrame();
window.setSize(400, 400);
window.setVisible(true);
}
}