-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
174 lines (155 loc) · 5.54 KB
/
GUI.java
File metadata and controls
174 lines (155 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;
/**
* Universidad del Valle de Guatemala
* Algoritmos y Estructura de Datos
* Seccion 10
* @author Andre Rodas
* @author Delbert Custodio
* @author Josemite Noe
* @author Rudy Garrido
* La clase <GUI> posee todos los atributos del JFrame la interfaz <ActionListener> para el manejo de evento del click en el boton
*/
public class GUI extends JFrame implements ActionListener{
final private int cantidad = 3000;
private JButton Bselsort,Bquicksort,Binsertsort,Bmergesort,creartxt,leertxt,copiartxt;
private PrintWriter escribir;
private Sorts mySort;
private String s;
private ArrayList<Comparacion> v = new ArrayList<Comparacion>();
/**
* Este procedimiento esconde o no los botones para evitar de que se realize un sort con el arreglo vacio
* <a>: es true si esta vacio, se chequea si el arreglo esta null y su tamaño es mayor a uno
*/
public void habilitar(){
boolean a = false;
if (mySort.vacio())
a = true;
Bselsort.setVisible(a);
Bquicksort.setVisible(a);
Binsertsort.setVisible(a);
Bmergesort.setVisible(a);
copiartxt.setVisible(a);
}
/**
* Clase constructora, se crean 8 botones con ActionListener y se llama al constructor de la clase <Sorts>
*/
public GUI() {
getContentPane().setLayout(null);
Bselsort=new JButton("Selection sort");
Bselsort.setBounds(185,150,130,23);
getContentPane().add(Bselsort);
Bselsort.addActionListener(this);
Bquicksort=new JButton("Quick Sort");
Bquicksort.setBounds(185,180,130,23);
getContentPane().add(Bquicksort);
Bquicksort.addActionListener(this);
Binsertsort=new JButton("Insertion Sort");
Binsertsort.setBounds(185,206,130,23);
getContentPane().add(Binsertsort);
Binsertsort.addActionListener(this);
Bmergesort=new JButton("Merge Sort");
Bmergesort.setBounds(185,240,130,23);
getContentPane().add(Bmergesort);
Bmergesort.addActionListener(this);
creartxt=new JButton("Crear txt de "+String.valueOf(cantidad)+ " numeros");
creartxt.setBounds(150,33,200,23);
getContentPane().add(creartxt);
creartxt.addActionListener(this);
leertxt=new JButton("Txt a arreglo");
leertxt.setBounds(150,72,200,23);
getContentPane().add(leertxt);
leertxt.addActionListener(this);
copiartxt=new JButton("Arreglo a txt");
copiartxt.setBounds(150,320,200,23);
getContentPane().add(copiartxt);
copiartxt.addActionListener(this);
mySort = new Sorts(cantidad);
habilitar();
}
/* (non-Javadoc)
* Se realiza una diferente tarea dependiendo del .getSource del boton
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource()==Bselsort) {
mySort.SelectionSort();
setTitle("Selection Sort");
}
else
if (e.getSource()==Bquicksort) {
mySort.QuickSort(0, mySort.getV().size()-1);
setTitle("Quick Sort");
}
else
if (e.getSource()==Binsertsort) {
mySort.InsertionSort();
setTitle("Insertion Sort");
}
else
if (e.getSource()==Bmergesort) {
v = mySort.getV();
v = mySort.MergeSort(v);
mySort.setArreglo(v);
setTitle("Merge Sort");
}
else
if (e.getSource()==creartxt) {
try {
Path currentRelativePath = Paths.get("");
s = currentRelativePath.toAbsolutePath().toString();
escribir = new PrintWriter(s.replace(System.getProperty("file.separator"), "/")+"/numeros.txt", "UTF-8");
} catch ( Exception m) {
m.printStackTrace();
}
Random rnd = new Random();
for (int i=1;i<=cantidad;i++)
escribir.println(rnd.nextInt(cantidad));
escribir.close();
setTitle("Crear txt");
}
else
if (e.getSource()==leertxt) {
try {
for (String line : Files.readAllLines(Paths.get("numeros.txt"))) { //Permite la lectura del todo el archivo, lee linea por linea
Integer i = Integer.valueOf(line);
mySort.agregar(i);
}
} catch (IOException e1) {
e1.printStackTrace();
}
setTitle("Leer txt");
}
else
if (e.getSource()==copiartxt) {
try {
escribir = new PrintWriter("numeros.txt", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException m) {
m.printStackTrace();
}
for (int i=1;i<=cantidad;i++)
escribir.println(mySort.obtener(i-1)); //Se obtiene los datos del arreglo de MySort y se copia en un archivo de texto nuevo
escribir.close();
setTitle("Copiar txt");
}
habilitar();
}
/**
* Se inicializan el constructor de <GUI> para crear el <JFrame> con sus respectivos elementos
*/
public static void main(String[] ar){
GUI formulario1=new GUI();
formulario1.setBounds(0,0,500,500);
formulario1.setVisible(true);
}
}