gpt4 book ai didi

java - 绘制选择排序的视觉效果

转载 作者:行者123 更新时间:2023-12-01 23:53:05 25 4
gpt4 key购买 nike

我正在编写一个程序,该程序创建一个包含 50 个数字的数组,然后将它们在面板中绘制为矩形,大小基于数字。单击该面板时,将对数组进行排序,并重新绘制面板以显示正在排序的数字的动画。这是单击面板之前和之后的样子: picture

这是我的代码:

public class AnimatedSelectionSortPanel extends javax.swing.JPanel {

int[] numbers = new int[50];
int min = 20;
int max = 100;

private void loadArray() {
int num;
for (int i = 0; i < numbers.length; i++) {
numbers[i] = min + (int) Math.random() * ((max - min) + 1);
}
}

public static void selectionSort(int[] x) {
for (int i = 0; i < x.length - 1; i++) {
int minIndex = i; // Index of smallest remaining value.
for (int j = i + 1; j < x.length; j++) {
if (x[minIndex] > x[j]) {
minIndex = j; // Remember index of new minimum
}
}
if (minIndex != i) {
//... Exchange current element with smallest remaining.
int temp = x[i];
x[i] = x[minIndex];
x[minIndex] = temp;
}
}
}

private void drawPass(Graphics g) {
int xPos = 10;
int yPos = 120;
int rectWidth = 1;

for (int num : numbers) {
g.setColor(Color.black);
g.drawRect(xPos, yPos, rectWidth, num);
xPos += 11;
}
}

@Override
public void paintComponent (Graphics g) {
while (numbers.length == 0) {
loadArray();
}
drawPass(g);
}

private void sortPanelMouseClicked(java.awt.event.MouseEvent evt) {
selectionSort(numbers);
sortPanel.repaint();
}

我遇到的问题是,当我单击面板时,框架中没有绘制任何内容。有人可以告诉我我所做的事情有什么问题吗?

这里是 GUI 构建器自动生成的代码(如果有帮助的话):

    private void initComponents() {

sortPanel = new javax.swing.JPanel();

sortPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
sortPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
sortPanelMouseClicked(evt);
}
});

javax.swing.GroupLayout sortPanelLayout = new javax.swing.GroupLayout(sortPanel);
sortPanel.setLayout(sortPanelLayout);
sortPanelLayout.setHorizontalGroup(
sortPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 398, Short.MAX_VALUE)
);
sortPanelLayout.setVerticalGroup(
sortPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 165, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sortPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(sortPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>

最佳答案

以下是一些建议:

  1. 请发布一个可编译的示例 - SSCCE

  2. 不要在 paintComponent 中放入太多逻辑,也不要在此方法中分配数据。其目的是绘画。提前准备好数字数组。

  3. min + (int) Math.random() * ((max - min) + 1); 始终为 20,因为 (int) Math.random() 始终为零。您应该转换结果,即: min + (int) (Math.random() * ((max - min) + 1));

  4. 将排序过程包装到线程或计时器中。在迭代之间等待并调用 repaint() 来绘制中间结果。包装到 Swing 计时器中更干净、更可取,但在发布的代码中,将 selectionSort 的整个逻辑转储到线程中可能会更容易/更快。请参阅Performing Custom Painting教程中的一些示例。另请参阅How to Use Swing Timers

  5. 看起来 drawPass 绘制了颠倒的矩形。

下面是一个使用现有代码并进行最小更改的示例:

enter image description here enter image description here

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

public class AnimatedSelectionSortPanel extends javax.swing.JPanel {

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new AnimatedSelectionSortPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}


private int[] numbers = new int[50];
private int min = 20;
private int max = 100;
private boolean shuffle = false;
public static final int ITERATION_SLEEP = 100;

public AnimatedSelectionSortPanel() {
loadArray();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (shuffle)
loadArray();
selectionSort(numbers);
shuffle = true;
}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 100);
}

private void loadArray() {
for (int i = 0; i < numbers.length; i++) {
numbers[i] = min + (int) (Math.random() * ((max - min) + 1));
}
}

public void selectionSort(final int[] x) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < x.length - 1; i++) {
int minIndex = i; // Index of smallest remaining value.
for (int j = i + 1; j < x.length; j++) {
if (x[minIndex] > x[j]) {
minIndex = j; // Remember index of new minimum
}
}
if (minIndex != i) {
// ... Exchange current element with smallest remaining.
int temp = x[i];
x[i] = x[minIndex];
x[minIndex] = temp;
}
repaint();
try {
Thread.sleep(ITERATION_SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}

private void drawPass(Graphics g) {
int rectWidth = 1;

int width = getWidth() - 1;
int height = getHeight() - 1;
int colSpan = Math.round((float)width / (float)numbers.length);
int x = 0;

for (int num : numbers) {
int colHeight = (int) ((float) height * ((float) num / (float) 100));
g.fillRect(x, height - colHeight, rectWidth, colHeight);
x += colSpan;
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawPass(g);
}
}

关于java - 绘制选择排序的视觉效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com