|
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.Collections;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- @SuppressWarnings("serial")
- //Frame structure
- public class ALGHW1 extends JFrame
- {
- //UI config
- private JFrame mainframe;
- private DPanel Dpanel;
- private JButton RunAlgBtn;
- private JButton RandDataBtn;
- private JPanel actionpanel;
- private JTextField JTX;
- private JLabel TXB;
-
- //Data STR
- ArrayList<Integer> DataArray=new ArrayList<Integer>(100);
-
- public boolean Run = false;
- public boolean Done = false;
- public int time = 100;
-
- //FrameworkUI
- public ALGHW1()
- {
- //Init Arry for further use
- for(int i=0;i<=99;i++)
- {
- DataArray.add(i+1);
- }
- //Rand Array
-
- Collections.shuffle(DataArray);
- Arraysort arraysort = new Arraysort(DataArray,Dpanel);
- Thread arraysorthread = new Thread(arraysort);
-
- //Frame
- mainframe = new JFrame("ALGHW1");
- mainframe.setLayout(new BorderLayout());
-
- //Panel
- Dpanel = new DPanel();
- Dpanel.setLayout(new GridLayout(2,9,2,2));
- Dpanel.setBackground( Color.BLACK );
-
- //add panel to frame
- mainframe.add(Dpanel, BorderLayout.CENTER);
-
- //ActPanel with BtnACT
- actionpanel = new JPanel();
- RunAlgBtn = new JButton("開始");
- RunAlgBtn.addActionListener
- (
- new ActionListener()
- {
- @SuppressWarnings("deprecation")
- public void actionPerformed(ActionEvent event)
- {
- if(Done == false){
- time = Integer.parseInt(JTX.getText());
- Arraysort arraysort = new Arraysort(DataArray,Dpanel);
- Thread arraysorthread = new Thread(arraysort);
- arraysorthread.start();
- RunAlgBtn.setVisible(false);
- Run = true;
- }
- }
- }
- );
- RandDataBtn = new JButton("重新亂數");
- RandDataBtn.addActionListener
- (
- new ActionListener()
- {
- @SuppressWarnings("deprecation")
- public void actionPerformed(ActionEvent event)
- {
- if(Run)
- {
- RunAlgBtn.setVisible(false);
- }
- else
- {
-
- Collections.shuffle(DataArray);
- RunAlgBtn.setVisible(true);
- Done = false;
- Dpanel.repaint();
- }
- }
- }
- );
-
- JTX = new JTextField(3);
- JTX.setText(String.valueOf(time));
- TXB = new JLabel("命令時間");
- //addBtn
- actionpanel.add(RandDataBtn);
- actionpanel.add(RunAlgBtn);
- actionpanel.add(TXB);
- actionpanel.add(JTX);
- //add panel to frame
- mainframe.add(actionpanel, BorderLayout.SOUTH);
-
- //Set Frame
- mainframe.setSize(600,600);
- mainframe.setVisible(true);
- mainframe.setResizable(true);
- mainframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
-
- }
- public static void main(String[] args)
- {
- ALGHW1 alg = new ALGHW1();
- alg.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public class DPanel extends JPanel
- {
- private int marked1;
- private int marked2;
-
- public void paintComponent( Graphics g )
- {
- super.paintComponent(g);
- int c = Dpanel.getWidth()/80;
- int d = Dpanel.getHeight()/80;
- int e = Dpanel.getWidth();
- int f = Dpanel.getHeight();
- if(DataArray.isEmpty())
- return;
- Graphics2D g2 = (Graphics2D) g;
- for (int i = 0; i < DataArray.size(); i++)
- {
- int v = DataArray.get(i);
- int a = i*Dpanel.getWidth()/100;
- int b = (Dpanel.getHeight()-v*Dpanel.getHeight()/100);
-
-
- if(i == marked1)
- {
- g2.setColor(Color.RED);
- g2.fillRoundRect(a, b,c,d,e,f);
- }
- else if(i == marked2)
- {
- g2.setColor(Color.GREEN);
- g2.fillRoundRect(a, b,c,d,e,f);
- }
- else
- {
- g2.setColor(Color.WHITE);
- g2.fillRoundRect(a, b,c,d,e,f);
- }
- }
- }
-
- public synchronized void setMARK(int marked1, int marked2)
- {
- this.marked1 = marked1;
- this.marked2 = marked2;
- }
- }
-
-
- class Arraysort implements Runnable
- {
- private ArrayList<Integer> dataArray;
- public Arraysort(ArrayList<Integer> dataArray, DPanel dpanel)
- {
- // TODO Auto-generated method stub
- this.dataArray = DataArray;
-
- }
- public void run() //CALL fquickSort
- { //CALL fquickSort
- fquickSort(); //CALL fquickSort
- } //CALL fquickSort
-
- public void fquickSort() //CALL recQuickSort
- { //CALL recQuickSort
- recQuickSort(0, dataArray.size() - 1); //CALL recQuickSort
- } //CALL recQuickSort
-
- public void recQuickSort(int left, int right) {
- int size = right - left + 1;
- if (size < 8 && size>0)
- {
- // insertion sort if small
- insertionSort(left, right);//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序
- }
- else if(size>=8) // quicksort if large
- {
- long median = medianOf3(left, right); //算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中間
- //算中間
- int partition = partitionIt(left, right, median);//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中
- 間分分樂//中間分分樂//中間分分樂//中間分分樂
- recQuickSort(left, partition - 1); //遞回1
- recQuickSort(partition + 1, right); //遞回2
- }
- }
-
- public long medianOf3(int left, int right) //算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中
- 間//算中間
- {
- int center = (left + right) / 2;
- // order left & center
- if (dataArray.get(left) > dataArray.get(center))
- {
- swap(left, center);
- }
- // order left & right
- if (dataArray.get(left) > dataArray.get(right))
- {
- swap(left, right);
- }
- // order center & right
- if (dataArray.get(center) > dataArray.get(right))
- {
- swap(center, right);
- }
- swap(center, right - 1);
- return dataArray.get(right - 1);
- }//算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中間//算中
- 間//算中間//算中間//算中間
-
- public void swap(int d1, int d2)//交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //
- 交換廚 //交換廚 //交換廚 //交換廚
- {
- Dpanel.setMARK(d1,d2);
- long temp = dataArray.get(d1);
- try {
- Thread.sleep(time);
- dataArray.set(d1,dataArray.get(d2));
- dataArray.set(d2,(int) temp);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Dpanel.repaint();
- }//交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換
- 廚 //交換廚 //交換廚 //交換廚 //交換廚 //交換廚
- public int partitionIt(int left, int right, long pivot) //中間分分樂//中間分分樂//中間分分樂//中間分分樂//
- 中間分分樂//中間分分樂//中間分分樂//中間分分樂
- {
- int leftPtr = left; // right of first elem
- int rightPtr = right - 1; // left of pivot
- while (true)
- {
- //find bigger
- while (dataArray.get(++leftPtr) < pivot)
- ;
- //find smaller
- while (dataArray.get(--rightPtr) > pivot)
- ;
- if (leftPtr >= rightPtr) // if pointers cross, partition done
- break;
- else
- swap(leftPtr, rightPtr);
- }
- swap(leftPtr, right - 1); // restore pivot
- return leftPtr; // return pivot location
- }
- //中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分
- 樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂//中間分分樂
-
- public void insertionSort(int left, int right) //插序//插序//插序//插序//插序//插序//插序//插序//插序
- {
- int in, out;
- // sorted on left of out
- for (out = left + 1; out <= right; out++)
- {
- long temp = dataArray.get(out); // remove marked item
- in = out; // start shifts at out
- // until one is smaller,
- while (in > left && dataArray.get(in - 1) >= temp)
- {
- dataArray.set(in,dataArray.get(in-1)); // shift item to right
- --in; // go left one position
- }
- Dpanel.setMARK(in,out);
- if(out >= 90)
- {
- RunAlgBtn.setVisible(true);
- Run = false;
- Done = true;
- }
- dataArray.set(in,(int) temp);
-
- }
- } //插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插序//插
- 序//插序//插序
- }
-
- //drawing agent
- }
複製代碼 |
|