Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.RenderingHints;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Arrays;
- import java.util.Random;
- import javax.swing.AbstractButton;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.SwingConstants;
- import javax.swing.SwingUtilities;
- import javax.swing.border.EmptyBorder;
- import javax.swing.plaf.basic.BasicButtonUI;
- @SuppressWarnings("serial")
- public class BubbleSort extends JPanel implements ActionListener
- {
- int swaps;
- int maxswaps;
- int[] array;
- int arrayLength = 12;
- int maxNumber = 100;
- int minNumber = 0;
- JLabel arrayLabel;
- JLabel tipLabel = new JLabel();
- JButton startButton = new JButton("Start");
- JButton titleButton = new JButton("BubbleSort");
- public static void main(String[] args)
- {
- BubbleSort panel = new BubbleSort();
- panel.setLayout(null);
- panel.setBackground(Color.WHITE);
- panel.setFocusable(true);
- panel.setBackground(new Color(47,47,47));
- JFrame frame = new JFrame("Bubble Sort");
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setSize(800, 500);
- frame.setContentPane(panel);
- frame.setVisible(true);
- moveToMiddle(frame);
- }
- public BubbleSort()
- {
- array = new int[arrayLength];
- Random r = new Random();
- for(int i=0; i < array.length; i++)
- {
- int num = r.nextInt(maxNumber) + minNumber;
- array[i] = num;
- }
- arrayLabel = new JLabel(Arrays.toString(array));
- arrayLabel.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
- tipLabel.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
- tipLabel.setVerticalAlignment(SwingConstants.CENTER);
- tipLabel.setHorizontalAlignment(SwingConstants.CENTER);
- tipLabel.setForeground(Color.white);
- arrayLabel.setVerticalAlignment(SwingConstants.CENTER);
- arrayLabel.setHorizontalAlignment(SwingConstants.CENTER);
- arrayLabel.setForeground(Color.white);
- startButton.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
- startButton.setUI(new MyButtonUI());
- startButton.setForeground(Color.white);
- startButton.setBackground(new Color(0x551a8b));
- startButton.addActionListener(this);
- startButton.setActionCommand("start");
- titleButton.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
- titleButton.setUI(new MyButtonUI());
- titleButton.setForeground(Color.white);
- titleButton.setBackground(new Color(0x551a8b));
- titleButton.addActionListener(this);
- titleButton.setActionCommand("title");
- this.add(arrayLabel); this.add(tipLabel); this.add(startButton); this.add(titleButton);
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- arrayLabel.setBounds(50, 125, getWidth()-100, 100);
- tipLabel.setBounds(50 , 275 , getWidth()-100, 100);
- startButton.setBounds(0, 60, getWidth(), 50);
- titleButton.setBounds(0, 0, getWidth(), 50);
- }
- private void bubbleSort(int[] array)
- {
- maxswaps = (int) Math.pow(array.length,2);
- int tempnum = 0;
- for(int i=0; i < array.length; i++)
- {
- for(int num=1; num < (array.length-i); num++)
- {
- if(array[num-1] > array[num])
- {
- tempnum = array[num-1];
- array[num-1] = array[num];
- array[num] = tempnum;
- tipLabel.setText("Swapping " + tempnum + " and " + array[num-1]);
- arrayLabel.setText(Arrays.toString(array));
- swaps++;
- SwingUtilities.invokeLater(()->repaint());
- try
- {
- Thread.sleep(700);
- Thread.currentThread().interrupt();
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- }
- tipLabel.setForeground(Color.RED);
- tipLabel.setText("Sorted in ascending order!");
- startButton.setEnabled(true);
- titleButton.setEnabled(true);
- }
- public static void moveToMiddle(JFrame frame)
- {
- GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
- int screenWidth = gd.getDisplayMode().getWidth();
- int screenHeight = gd.getDisplayMode().getHeight();
- frame.setBounds((screenWidth/2) - (frame.getWidth()/2), (screenHeight/2) - (frame.getHeight()/2), frame.getWidth(), frame.getHeight());
- }
- public void actionPerformed(ActionEvent arg0)
- {
- String e = arg0.getActionCommand();
- if(e.equals("title"))
- {
- array = new int[arrayLength];
- Random r = new Random();
- for(int i=0; i < array.length; i++)
- {
- int num = r.nextInt(maxNumber) + minNumber;
- array[i] = num;
- }
- arrayLabel.setText(Arrays.toString(array));
- }
- if(e.equals("start"))
- {
- new Thread(()->bubbleSort(array)).start();
- startButton.setEnabled(false);
- titleButton.setEnabled(false);
- }
- }
- }
- class MyButtonUI extends BasicButtonUI
- {
- public void installUI (JComponent c)
- {
- super.installUI(c);
- AbstractButton button = (AbstractButton) c;
- button.setOpaque(false);
- button.setBorder(new EmptyBorder(5, 15, 5, 15));
- }
- public void paint (Graphics g, JComponent c)
- {
- AbstractButton b = (AbstractButton) c;
- paintBackground(g, b, b.getModel().isPressed() ? 2 : 0);
- super.paint(g, c);
- }
- private void paintBackground (Graphics g, JComponent c, int yOffset)
- {
- Dimension size = c.getSize();
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setColor(c.getBackground().darker());
- g.fillRoundRect(0, yOffset, size.width, size.height - yOffset, 10, 10);
- g.setColor(c.getBackground());
- g.fillRoundRect(0, yOffset, size.width, size.height + yOffset - 5, 10, 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement