hi leute,
ich brauche hilfe bei einem applet für das sortierverfahren bubblesort.
mein problem ist, dass ich nicht weiß was ich bei der ausgabe (draw()-methode) falsch mache, diese gibt einfach nicht meinen string (liste) aus…
hier mein quellcode, ide ist netbeans 7:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.lang.reflect.InvocationTargetException;
public class SortAlgos extends javax.swing.JApplet {
private static final long serialVersionUID = 1L;
@Override
public void init()
{
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/l…
*/
try
{
for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if(„Nimbus“.equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch(ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(SortAlgos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch(InstantiationException ex)
{
java.util.logging.Logger.getLogger(SortAlgos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch(IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(SortAlgos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch(javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(SortAlgos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
try
{
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run()
{
initComponents();
initBG();
getAll();
}
});
}
catch(InvocationTargetException ex)
{
System.out.println("init: " + ex + „\n“ + ex.getLocalizedMessage() + „\n“ + ex.getMessage());
}
catch(InterruptedException ex)
{
System.out.println("init: " + ex + „\n“ + ex.getLocalizedMessage() + „\n“ + ex.getMessage());
}
}
@SuppressWarnings(„unchecked“)
//
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
canvas1 = new java.awt.Canvas();
setBackground(new java.awt.Color(102, 102, 102));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jButton1.setText(„Bubble Sort“);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setText("[9,11,3,4,5,1,8]");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
leeren();
if(run)
{
run = false;
System.out.println(„Abbruch“);
while(bsort.isAlive())
{
try
{
Thread.sleep(1);
}
catch(InterruptedException ex)
{
System.out.println("sleep1: " + ex);
}
}
}
else
{
run = true;
getAll();
bsort.start();
}
}
// Variables declaration - do not modify
private java.awt.Canvas canvas1;
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
private String liste = „“;
private int z = 0;
private int[] elemente;
private Thread bsort = null;
private boolean run = false;
private boolean oppb = false;
private int oppi = 1;
private Image backbuffer;
private Graphics graphic;
public void draw()
{
graphic.drawString(liste, 0, 0);
// hier malen
repaint();
}
@Override
public void update(Graphics g)
{
g.drawImage(backbuffer, 0, 0, this);
}
@Override
public void paint(Graphics g)
{
update(g);
}
private void setThread()
{
bsort = new Thread() {
@Override
public void run()
{
boolean nichtfertig = true;
int temp;
while(nichtfertig)
{
nichtfertig = false;
if(!run)
{
break;
}
//draw();
for(int i = 0; i elemente[i + 1])
{
temp = elemente[i];
elemente[i] = elemente[i + 1];
elemente[i + 1] = temp;
nichtfertig = true;
oppb = true;
draw();
oppb = false;
}
else
{
draw();
}
}
}
draw();
run = false;
}
private void sleep(int i)
{
try
{
Thread.sleep(i);
} catch(InterruptedException ex)
{
//jTextArea1.append("sleep: " + ex);
}
}
};
}
private void leeren()
{
liste = „“;
z = 0;
elemente = null;
bsort = null;
run = false;
}
private void getAll()
{
liste = jTextField1.getText().substring(1, jTextField1.getText().length() - 1);
String[] e = liste.split(",");
elemente = new int[e.length];
for(int i = 0; i
vllt gibt es jemanden, der mir dabei behilflich sein kann mein problem zu beheben.
vielen dank schon mal,
tobi