0

This is what I got so far for my bubble sorting visual representation but if I add CanvasCourse to main (you can see it commented in code), my program stops working the proper way. In general, I need to somehow add painting process to actionListener for my "sort an array" button and I also need to add timer for this process so that it is shown on the screen how the sorting works. Here is my code:

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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*class publicArr {
public int[] intArr = new int [7];
public String[] stringArr = new String [7];
}*/

public class courseWork extends JFrame {

private JLabel visual = new JLabel("Visualization of bubble sorting: array of 7 elements");
private JButton enter = new JButton("Enter an array");
private JButton generate = new JButton("Randomize an array");
private JButton sort = new JButton("Sort an array");
private JTextField input = new JTextField(50);
public int[] array = new int [7];
public String[] futArr = new String[7];

public courseWork(){
    super("Visualization of bubble sorting");
    createGUI();
}

public void createGUI(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();///////////////////////////////////////////////////////
    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    panel.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill=GridBagConstraints.HORIZONTAL;

    constraints.gridy = 50;
    constraints.gridx = 0;
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;

    visual.setHorizontalAlignment(JLabel.CENTER);
    visual.setFont(new Font("Calibri", Font.PLAIN, 30));

    constraints.gridy = 100;
    constraints.gridx = 0;
    constraints.gridwidth = 3;
    panel.add(visual, constraints);

    input.setHorizontalAlignment(JLabel.LEFT);
    input.setFont(new Font("Calibri", Font.PLAIN, 30));
    constraints.gridwidth = 1;
    constraints.gridx = 0;
    constraints.gridy = 240;
    constraints.weightx = 0.5;
    panel.add(input, constraints);

    enter.setHorizontalAlignment(JLabel.CENTER);
    enter.setFont(new Font("Calibri", Font.PLAIN, 30));
    constraints.gridx = 1;
    constraints.gridy = 240;
    constraints.weightx = 0.5;
    panel.add(enter, constraints);

    generate.setHorizontalAlignment(JLabel.CENTER);
    generate.setFont(new Font("Calibri", Font.PLAIN, 30));
    constraints.gridx = 2;
    constraints.gridy = 240;
    constraints.weightx = 0.5;
    panel.add(generate, constraints);

    sort.setHorizontalAlignment(JLabel.CENTER);
    sort.setFont(new Font("Calibri", Font.PLAIN, 30));

    constraints.gridy = 400;
    constraints.gridx = 0;
    constraints.gridwidth = 3;
    panel.add(sort, constraints);////////////////////////////////////////////// setting buttons

    enter.addActionListener(e-> {
        getArr();
        panel.remove(enter);
        panel.remove(input);
        panel.remove(generate);
        revalidate();
        repaint();
        outputArr();
    });

    generate.addActionListener(e-> {
        randomizeArr();
        panel.remove(enter);
        panel.remove(input);
        panel.remove(generate);
        revalidate();
        repaint();
        outputArr();
    });

    panel.setPreferredSize(new Dimension(1300, 800));
    getContentPane().add(panel);
}

private void getArr(){
    String text = input.getText();
    futArr = text.split(" ");
    for (int i = 0; i<7; i++) {
        array[i] = Integer.parseInt(futArr[i]);
    }
}

private void randomizeArr(){
    for (int i = 0; i < 7; i++){
        array[i] = (int)(Math.random()*10+1);
        futArr[i] = Integer.toString(array[i]);
    }

}

private void outputArr(){
    for (int i = 0; i < 7; i++){
        System.out.print(array[i] + " int ");
        System.out.print(futArr[i] + " str ");
    }

}

public int[] returnIntArr(){
    return array;
}

public String[] returnStrArr(){
    return futArr;
}

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    courseWork frame = new courseWork();
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    CanvasCourse PA = new CanvasCourse();
    PA.intArr = frame.returnIntArr();
    PA.stringArr = frame.returnStrArr();

    /*CanvasCourse canv=new CanvasCourse();
    frame.add(canv);
    frame.setVisible(true);*/
}
}
class CanvasCourse extends JComponent {
public void paint(Graphics g) {
    super.paint(g);
    g.setFont(new Font("TimesRoman", Font.BOLD,922));
    paintNumbers(g);
    repaint();
}

public int[] intArr = new int [7];
public String[] stringArr = new String [7];


public void paintNumbers(Graphics g){
    int x = 100;
    int y = 200;
    for (int i = 0; i< 7; i++){
        g.setColor(Color.BLACK);
        g.drawString(stringArr[i], x, y);
        x = x + 10;
    }
}

public void BubbleSorting(Graphics g){
    int x = 100;
    int y = 200;
    int temp = 0;
    int count = 0;
    String temp1;
    for (int j = 0; j < 5; j++){
        count = 0;
        for (int i = 0; i < 6; i++){
            g.setColor(Color.PINK);
            g.drawString(stringArr[i], x, y);
            g.drawString(stringArr[i+1], x+10, y);
            if (intArr[i] < intArr[i+1]){
                g.setColor(Color.RED);
                g.drawString(stringArr[i], x, y);
                g.drawString(stringArr[i+1], x+10, y);
                temp = intArr[i];
                intArr[i] = intArr[i+1];
                intArr[i+1] = temp;
                temp1 = stringArr[i];
                stringArr[i] = stringArr[i+1];
                stringArr[i+1] = temp1;
                g.setColor(Color.BLACK);
                g.drawString(stringArr[i], x, y);
                g.drawString(stringArr[i+1], x+10, y);
                count++;
                x=x+10;
            }
            else{
                g.setColor(Color.GREEN);
                g.drawString(stringArr[i], x, y);
                g.drawString(stringArr[i+1], x+10, y);
                g.setColor(Color.BLACK);
                g.drawString(stringArr[i], x, y);
                g.drawString(stringArr[i+1], x+10, y);
                x=x+10;
            }
        }
        if(count==0)
            break;
    }
}
}
SDY
  • 1
  • First, you need to separate the graphics logic from sorting logic. You have everything mixed. I suggest creating a sorting algorithm that returns a snapshot of every step. That would be an array of arrays. Then you ca use it to graphically "animate" the process by drawing each step. – user1275011 May 20 '21 at 14:36
  • see: https://stackoverflow.com/questions/64196198/bubble-sort-animation for two approaches. – camickr May 20 '21 at 14:42
  • One idea is to completely separate the painting of the array from the sorting of the array. You should have a drawing JPanel that does nothing but draw the integer array in its current state. Take a look at my [Bubble Sort Animation](https://github.com/ggleblanc2/bubble-sort-animation) on GitHub to see one way to accomplish this. – Gilbert Le Blanc May 20 '21 at 14:47

0 Answers0