Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.GraphicsDevice;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.util.Arrays;
  12. import java.util.Random;
  13.  
  14. import javax.swing.AbstractButton;
  15. import javax.swing.JButton;
  16. import javax.swing.JComponent;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.SwingConstants;
  21. import javax.swing.SwingUtilities;
  22. import javax.swing.border.EmptyBorder;
  23. import javax.swing.plaf.basic.BasicButtonUI;
  24.  
  25. @SuppressWarnings("serial")
  26. public class BubbleSort extends JPanel implements ActionListener
  27. {
  28. int swaps;
  29. int maxswaps;
  30. int[] array;
  31.  
  32. int arrayLength = 12;
  33. int maxNumber = 100;
  34. int minNumber = 0;
  35.  
  36. JLabel arrayLabel;
  37. JLabel tipLabel = new JLabel();
  38. JButton startButton = new JButton("Start");
  39. JButton titleButton = new JButton("BubbleSort");
  40.  
  41. public static void main(String[] args)
  42. {
  43. BubbleSort panel = new BubbleSort();
  44. panel.setLayout(null);
  45. panel.setBackground(Color.WHITE);
  46. panel.setFocusable(true);
  47. panel.setBackground(new Color(47,47,47));
  48.  
  49. JFrame frame = new JFrame("Bubble Sort");
  50. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  51. frame.setSize(800, 500);
  52. frame.setContentPane(panel);
  53. frame.setVisible(true);
  54. moveToMiddle(frame);
  55. }
  56.  
  57. public BubbleSort()
  58. {
  59. array = new int[arrayLength];
  60. Random r = new Random();
  61. for(int i=0; i < array.length; i++)
  62. {
  63. int num = r.nextInt(maxNumber) + minNumber;
  64. array[i] = num;
  65. }
  66.  
  67. arrayLabel = new JLabel(Arrays.toString(array));
  68. arrayLabel.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
  69.  
  70. tipLabel.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
  71.  
  72. tipLabel.setVerticalAlignment(SwingConstants.CENTER);
  73. tipLabel.setHorizontalAlignment(SwingConstants.CENTER);
  74. tipLabel.setForeground(Color.white);
  75.  
  76. arrayLabel.setVerticalAlignment(SwingConstants.CENTER);
  77. arrayLabel.setHorizontalAlignment(SwingConstants.CENTER);
  78. arrayLabel.setForeground(Color.white);
  79.  
  80. startButton.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
  81. startButton.setUI(new MyButtonUI());
  82. startButton.setForeground(Color.white);
  83. startButton.setBackground(new Color(0x551a8b));
  84. startButton.addActionListener(this);
  85. startButton.setActionCommand("start");
  86.  
  87. titleButton.setFont(new Font("Bahnschrift", Font.PLAIN, 35));
  88. titleButton.setUI(new MyButtonUI());
  89. titleButton.setForeground(Color.white);
  90. titleButton.setBackground(new Color(0x551a8b));
  91. titleButton.addActionListener(this);
  92. titleButton.setActionCommand("title");
  93.  
  94. this.add(arrayLabel); this.add(tipLabel); this.add(startButton); this.add(titleButton);
  95. }
  96.  
  97. public void paintComponent(Graphics g)
  98. {
  99. super.paintComponent(g);
  100.  
  101. arrayLabel.setBounds(50, 125, getWidth()-100, 100);
  102. tipLabel.setBounds(50 , 275 , getWidth()-100, 100);
  103. startButton.setBounds(0, 60, getWidth(), 50);
  104. titleButton.setBounds(0, 0, getWidth(), 50);
  105. }
  106.  
  107. private void bubbleSort(int[] array)
  108. {
  109. maxswaps = (int) Math.pow(array.length,2);
  110. int tempnum = 0;
  111. for(int i=0; i < array.length; i++)
  112. {
  113. for(int num=1; num < (array.length-i); num++)
  114. {
  115. if(array[num-1] > array[num])
  116. {
  117. tempnum = array[num-1];
  118. array[num-1] = array[num];
  119. array[num] = tempnum;
  120. tipLabel.setText("Swapping " + tempnum + " and " + array[num-1]);
  121. arrayLabel.setText(Arrays.toString(array));
  122. swaps++;
  123. SwingUtilities.invokeLater(()->repaint());
  124. try
  125. {
  126. Thread.sleep(700);
  127. Thread.currentThread().interrupt();
  128. }
  129. catch (InterruptedException e)
  130. {
  131.  
  132. }
  133. }
  134. }
  135. }
  136. tipLabel.setForeground(Color.RED);
  137. tipLabel.setText("Sorted in ascending order!");
  138. startButton.setEnabled(true);
  139. titleButton.setEnabled(true);
  140. }
  141.  
  142. public static void moveToMiddle(JFrame frame)
  143. {
  144. GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  145. int screenWidth = gd.getDisplayMode().getWidth();
  146. int screenHeight = gd.getDisplayMode().getHeight();
  147. frame.setBounds((screenWidth/2) - (frame.getWidth()/2), (screenHeight/2) - (frame.getHeight()/2), frame.getWidth(), frame.getHeight());
  148. }
  149.  
  150. public void actionPerformed(ActionEvent arg0)
  151. {
  152. String e = arg0.getActionCommand();
  153. if(e.equals("title"))
  154. {
  155. array = new int[arrayLength];
  156. Random r = new Random();
  157. for(int i=0; i < array.length; i++)
  158. {
  159. int num = r.nextInt(maxNumber) + minNumber;
  160. array[i] = num;
  161. }
  162. arrayLabel.setText(Arrays.toString(array));
  163. }
  164. if(e.equals("start"))
  165. {
  166. new Thread(()->bubbleSort(array)).start();
  167. startButton.setEnabled(false);
  168. titleButton.setEnabled(false);
  169. }
  170. }
  171. }
  172.  
  173. class MyButtonUI extends BasicButtonUI
  174. {
  175. public void installUI (JComponent c)
  176. {
  177. super.installUI(c);
  178. AbstractButton button = (AbstractButton) c;
  179. button.setOpaque(false);
  180. button.setBorder(new EmptyBorder(5, 15, 5, 15));
  181. }
  182. public void paint (Graphics g, JComponent c)
  183. {
  184. AbstractButton b = (AbstractButton) c;
  185. paintBackground(g, b, b.getModel().isPressed() ? 2 : 0);
  186. super.paint(g, c);
  187. }
  188. private void paintBackground (Graphics g, JComponent c, int yOffset)
  189. {
  190. Dimension size = c.getSize();
  191. Graphics2D g2 = (Graphics2D) g;
  192. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  193. g.setColor(c.getBackground().darker());
  194. g.fillRoundRect(0, yOffset, size.width, size.height - yOffset, 10, 10);
  195. g.setColor(c.getBackground());
  196. g.fillRoundRect(0, yOffset, size.width, size.height + yOffset - 5, 10, 10);
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement