简体   繁体   English

Applet 中的 Paint 方法(Java 中的排序算法)

[英]Paint Method in an Applet (Sort Algorithms in Java)

I have two classes: One applet for the GUI and one class to sort numbers.我有两个类:一个用于 GUI 的小程序和一个用于对数字进行排序的类。

Sort Class:排序类:

public class Sortierung {

public int[] Zahlen = {5,2,3,1,4};
int z;



public static int zufallszahl (int z){
 int dauer = ((int)(Math.random()*z))+1;
 return dauer;
}

   public void ArrayErstellen(int l){
    int[] myIntArray = new int[l];
  for(int f=0;f<myIntArray.length;f++){
      myIntArray[f]=zufallszahl(10);
    }
    Zahlen=myIntArray;
}

public int[] BubbleSort() {
  for(int p=0;p<Zahlen.length-1;p++){
   for(int i=0;i<Zahlen.length-1;i++){
     if(Zahlen[i]<Zahlen[i+1]){
       continue;  //beendet den derzeitigen Schleifenablauf
     }
     else{
      z=Zahlen[i];
      Zahlen[i]=Zahlen[i+1];
      Zahlen[i+1]=z;

     }
   }
  }
  return Zahlen;
}

public int[] InsertionSort() {
 int f = 0; //Variable 1
 int j = 0; //Variable 2
  for(int i = 1;i < Zahlen.length;i++){ 
   j = i; 
    while((j>0)&&(Zahlen[j-1]>Zahlen[j])){
     f = Zahlen[j-1];
     Zahlen[j-1] = Zahlen[j];
     Zahlen[j] = f;
     j--;
    }
   }
  return Zahlen;
}

public int[] SelectionSort(){

    for (int i = 0; i < Zahlen.length - 1; i++){
        int s = i; //Neue Variable für die Schleife
        for (int j = i + 1; j < Zahlen.length; j++)
            if (Zahlen[j] < Zahlen[s]){    
                s = j;
            }    

        int smallerNumber = Zahlen[s];  
        Zahlen[s] =Zahlen[i];
        Zahlen[i] = smallerNumber;
    }
    return Zahlen;
}

public void ArrayWiedergeben(int[] eimer){
  for(int u=0;u<eimer.length;u++){
    System.out.println(eimer[u]);
  }
}  }

Gui (Applet):桂(小程序):

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Gui extends JApplet {

    JTextField tf;
    JButton b;
    JButton k;
    JLabel s;
    JLabel u;
    int anzahl;
    int test = 0;

    JPanel p = new JPanel();
    JPanel h = new JPanel();
    JPanel f = new JPanel();
    Sortierung neu = new Sortierung();
    Choice Sortierungsmethode = new Choice();

    ActionListener startOhr = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            anzahl = Integer.parseInt(tf.getText());
            if (anzahl > 100) {
                tf.setText("Wert zu hoch.");
                return;
            }
            neu.ArrayErstellen(anzahl);
            tf.setText("Array[" + anzahl + "] erstellt.");
        }
    };

    ActionListener sortOhr = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            switch (Sortierungsmethode.getSelectedIndex()) {
                case 0:
                    int[] eimer = neu.BubbleSort();
                    neu.ArrayWiedergeben(eimer);
                    test = 1;
                    repaint();

                    break;
                case 1:
                    int[] eimer2 = neu.InsertionSort();
                    neu.ArrayWiedergeben(eimer2);

                    break;
                case 2:
                    int[] eimer3 = neu.SelectionSort();
                    neu.ArrayWiedergeben(eimer3);
                    break;
                default:
                    break;
            }
        }
    };

    public void init() {
        tf = new JTextField(8);
        b = new JButton("Erstellen");
        k = new JButton("Bestätigen");
        s = new JLabel("Array Länge");
        u = new JLabel("Sortmethode");
        Sortierungsmethode.add("Bubblesort");
        Sortierungsmethode.add("Insertionsort");
        Sortierungsmethode.add("Selectionsort");
        setLayout(new BorderLayout());
        p.add(s);
        p.add(tf);
        p.add(b);
        h.add(u);
        h.add(Sortierungsmethode);
        h.add(k);

        this.add(p, BorderLayout.NORTH);
        this.add(h, BorderLayout.CENTER);
        this.add(f, BorderLayout.SOUTH);
        b.addActionListener(startOhr);
        k.addActionListener(sortOhr);
    }
}

My intention is to draw the sorted array in a coordinate system with a paint method.我的目的是使用paint方法在坐标系中绘制排序数组。 The problem is, that when I try to create a paint method, the whole screen is white and I cant see anything问题是,当我尝试创建一个绘制方法时,整个屏幕都是白色的,我看不到任何东西

You need to address several details:您需要解决几个细节:

  1. You should create a class which overrides JPanel to do your custom drawing.您应该创建一个覆盖JPanel的类来进行自定义绘图。 You can create an instance of the class in your Gui class.您可以在Gui类中创建该类的实例。

  2. You should override paintComponet() rather than paint() .您应该覆盖paintComponet()而不是paint()

  3. The first line in paintComponent(Graphics g) should be super.paintComponent(g) in order to ensure that the super class can paint itself. super.paintComponent(g) paintComponent(Graphics g)的第一行应该是super.paintComponent(g)以确保超类可以绘制自己。

  4. You need to be careful that your sorting algorithm doesn't take over the main thread.您需要注意排序算法不会接管主线程。 This will cause your app to freeze and nothing will paint until the sorting has finished.这将导致您的应用程序冻结,并且在排序完成之前不会绘制任何内容。

You need to create a Thread object and set it to call run method 30 times per second.您需要创建一个Thread对象并将其设置为每秒调用run方法 30 次。 In this method, call repaint() method of main JPanel .在这个方法中,调用主JPanel repaint()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM