我目前的代码" QuickSorting"然而,它显示在屏幕的第二个打印上排序。我希望能够更好地观察当我调用" QuickSort"时发生的事情,以便在大约5个屏幕打印中对其进行排序。 我的代码在下面。
1
public class Frame1 extends javax.swing.JFrame {
public Presentation() {
initComponents();
}
MyArrayClass myArrayObject = new MyArrayClass(25);
public void BeginTimer() {
class RemindTask extends TimerTask {
@Override
public void run() {
paintAndSort();
}
}
new Timer().scheduleAtFixedRate(new RemindTask(), 2000, 2000);
}
public void paintAndSort() {
int size = Integer.parseInt(txtSizeOfArray.getText());
Graphics g = this.getGraphics();
g.clearRect(15, 200, 270, 200);
g.drawRect(15, 200, 270, 200);
g.setColor(Color.DARK_GRAY);
int x = 15;
for (int i = 0; i < size; i++) {
int value = myArrayObject.MyArray[i];
g.fillRect(x, 300 - value, 9, value);
x += 10;
}
sortIt();
}
Public void sortIt() {
int size = Integer.parseInt(txtSizeOfArray.getText());
myArrayObject.quickSort(0, size - 1);
}
}
2
public class MyArrayClass {
int myArray[];
int arraySize;
int result;
public MyArrayClass(int size) {
arraySize = size;
myArray = new int[arraySize];
}
public void numberGenerator() {
for (int i = 0; i < arraySize; i++) {
myArray[i] = (int) (Math.random() * 99) + 1;
}
}
public void quickSort(int left, int right) {
if (right - left <= 0) {
return;
} else {
int pivot = myArray[right];
partitioning(left, right, pivot);
int pivotValue = result;
quickSort(left, pivotValue - 1);
quickSort(pivotValue + 1, right);
}
}
public void partitioning(int left, int right, int pivot) {
int leftP = left - 1;
int rightP = right;
while (true) {
while (leftP < right && myArray[++leftP] < pivot) {
}
while (rightP > 0 && myArray[--rightP] > pivot) {
}
if (rightP <= leftP) {
swapValues(leftP, right);
result = leftP;
break;
} else {
swapValues(leftP, rightP);
break;
}
}
}
private void swapValues(int leftP, int rightP) {
int aux = myArray[leftP];
myArray[leftP] = myArray[rightP];
myArray[rightP] = aux;
}
}
答案 0 :(得分:0)
代码存在一些问题。一般来说:
永远在getGraphics
上致电Component
!
除此之外,还有一些小问题(不要扩展JFrame,在EDT上创建GUI,以及评论中提到的命名约定)。
代码的当前结构使得将其连接到GUI组件变得非常容易。您应该清楚地考虑如何建立此连接,以及您希望更新组件的时间点。
此示例显示了如何基本完成。 不是一个非常好的通用结构,但我不想完全重写你的MyArrayClass
或者在它周围生成一个庞大的接口,定时器,监听器等基础设施。它只显示非常简单和非常务实的方法。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GraphicalQuickSort
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BorderLayout());
final MyArrayClass myArrayClass = new MyArrayClass(25);
myArrayClass.NumberGenerator();
GraphicalQuickSortPanel panel =
new GraphicalQuickSortPanel(myArrayClass);
myArrayClass.graphicalQuickSortPanel = panel;
JButton sortButton = new JButton("Sort");
sortButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
performSorting(myArrayClass);
}
});
f.getContentPane().add(sortButton, BorderLayout.NORTH);
f.getContentPane().add(panel, BorderLayout.CENTER);
f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static void performSorting(final MyArrayClass myArrayClass)
{
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
myArrayClass.QuickSort(0, myArrayClass.ArraySize-1);
System.out.println("Done");
}
});
thread.start();
}
}
class GraphicalQuickSortPanel extends JPanel
{
private final MyArrayClass myArrayClass;
public GraphicalQuickSortPanel(MyArrayClass myArrayClass)
{
this.myArrayClass = myArrayClass;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
int size = myArrayClass.ArraySize;
int barWidth = getWidth() / size;
for (int i = 0; i < size; i++)
{
int value = myArrayClass.MyArray[i];
int x = i * barWidth;
int y = getHeight()-value;
g.fillRect(x+1, y, barWidth-2, value);
}
}
}
class MyArrayClass {
int MyArray[];
int ArraySize;
int res;
// This should NOT be a direct reference, but
// a generic listener instead!
GraphicalQuickSortPanel graphicalQuickSortPanel;
long delayAfterSwapMS = 500;
public MyArrayClass(int size) {
ArraySize = size;
MyArray = new int[ArraySize];
}
public void NumberGenerator() {
for (int i = 0; i < ArraySize; i++) {
MyArray[i] = (int) (Math.random() * 99) + 1;
}
}
public void QuickSort(int Left, int Right) {
if (Right - Left <= 0) {
return;
} else {
int Pivot = MyArray[Right];
Partitioning(Left, Right, Pivot);
int PivotValue = res;
QuickSort(Left, PivotValue - 1);
QuickSort(PivotValue + 1, Right);
}
}
public void Partitioning(int Left, int Right, int Pivot) {
int LeftP = Left - 1;
int RightP = Right;
while (true) {
while (LeftP < Right && MyArray[++LeftP] < Pivot) {
}
while (RightP > 0 && MyArray[--RightP] > Pivot) {
}
if (RightP <= LeftP) {
SwapValues(LeftP, Right);
res = LeftP;
break;
} else {
SwapValues(LeftP, RightP);
break;
}
}
}
private void SwapValues(int LeftP, int RightP) {
int aux = MyArray[LeftP];
MyArray[LeftP] = MyArray[RightP];
MyArray[RightP] = aux;
// This should be solved differently
try
{
Thread.sleep(delayAfterSwapMS);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
graphicalQuickSortPanel.repaint();
}
}