-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphs.java
More file actions
424 lines (377 loc) · 18.3 KB
/
Graphs.java
File metadata and controls
424 lines (377 loc) · 18.3 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Graficas;
import java.awt.Graphics;
import javax.swing.JOptionPane;
/**
*
* @author acer
*/
public class graficas extends javax.swing.JFrame {
private int maxX,maxY,mx,my;
double escX,escY,x,y;
/**
* Creates new form graficas
*/
public graficas() {
initComponents();
maxX=this.PanelPrincipal.getWidth();
maxY=this.PanelPrincipal.getHeight();
mx=maxX/2;
my=maxY/2;
escX=10;
escY=10;
x=y=0;
}
@Override
public void paint(Graphics g){
this.paintComponents(g);
segmento(0,maxY,0,-maxY);
segmento(maxX,0,-maxX,0);
punto(x,y);
}
public void segmento(double x1,double y1, double x2, double y2){
this.PanelPrincipal.getGraphics().drawLine((int)(mx+escX*x1), (int)(my-escY*y1), (int)(mx+escX*x2), (int)(my-escY*y2));
}
public void punto(double x, double y){
PanelPrincipal.getGraphics().drawOval((int)(mx+escX*x), (int)(my-escY*y), 1, 1);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
buttonGroup3 = new javax.swing.ButtonGroup();
buttonGroup4 = new javax.swing.ButtonGroup();
buttonGroup5 = new javax.swing.ButtonGroup();
buttonGroup6 = new javax.swing.ButtonGroup();
PanelPrincipal = new javax.swing.JPanel();
jButtonPolinomio = new javax.swing.JButton();
jButtonRacional = new javax.swing.JButton();
jButtonExponencial = new javax.swing.JButton();
jButtonLogaritmo = new javax.swing.JButton();
jButtonTrigomometrica = new javax.swing.JButton();
jLabelEscala = new javax.swing.JLabel();
jLabelA = new javax.swing.JLabel();
jTextFieldA = new javax.swing.JTextField();
jLabelB = new javax.swing.JLabel();
jTextFieldB = new javax.swing.JTextField();
jSpinnerEsc = new javax.swing.JSpinner();
jButtonBorrar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
PanelPrincipal.setBorder(javax.swing.BorderFactory.createTitledBorder("Grafica de la funcion en el intervalo [a,b]"));
javax.swing.GroupLayout PanelPrincipalLayout = new javax.swing.GroupLayout(PanelPrincipal);
PanelPrincipal.setLayout(PanelPrincipalLayout);
PanelPrincipalLayout.setHorizontalGroup(
PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 691, Short.MAX_VALUE)
);
PanelPrincipalLayout.setVerticalGroup(
PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jButtonPolinomio.setText("Polinomio");
jButtonPolinomio.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPolinomioActionPerformed(evt);
}
});
jButtonRacional.setText("Racional");
jButtonRacional.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonRacionalActionPerformed(evt);
}
});
jButtonExponencial.setText("Exponencial");
jButtonExponencial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonExponencialActionPerformed(evt);
}
});
jButtonLogaritmo.setText("Logaritmo");
jButtonLogaritmo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonLogaritmoActionPerformed(evt);
}
});
jButtonTrigomometrica.setText("Trigonométrica");
jButtonTrigomometrica.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonTrigomometricaActionPerformed(evt);
}
});
jLabelEscala.setText("Ingrese la escala:");
jLabelA.setText("Ingrese a");
jTextFieldA.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldAActionPerformed(evt);
}
});
jLabelB.setText("Ingrese b");
jSpinnerEsc.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinnerEscStateChanged(evt);
}
});
jButtonBorrar.setText("Borrar");
jButtonBorrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonBorrarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(PanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButtonPolinomio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextFieldB, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextFieldA, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonRacional, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonExponencial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonLogaritmo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonTrigomometrica, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jLabelA))
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(jLabelB))
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabelEscala))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jSpinnerEsc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jButtonBorrar)))
.addContainerGap(36, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jLabelA, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabelB)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextFieldB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabelEscala)
.addGap(18, 18, 18)
.addComponent(jSpinnerEsc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jButtonPolinomio)
.addGap(18, 18, 18)
.addComponent(jButtonRacional)
.addGap(18, 18, 18)
.addComponent(jButtonExponencial)
.addGap(18, 18, 18)
.addComponent(jButtonLogaritmo)
.addGap(18, 18, 18)
.addComponent(jButtonTrigomometrica)
.addGap(27, 27, 27)
.addComponent(jButtonBorrar)
.addContainerGap(62, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(PanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(20, 20, 20))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jTextFieldAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldAActionPerformed
private void jButtonRacionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRacionalActionPerformed
// TODO add your handling code here:
double x1,x2,fx1,fx2,inicio,fin;
try{
inicio = Integer.parseInt(jTextFieldA.getText());
fin = Integer.parseInt(jTextFieldB.getText());
if(fin<=inicio){
inicio=fin;
}
for(int i=0; i<1000; i++){
x1 = inicio + i*(fin-inicio)/1000;
fx1=1/x1;
x2 = inicio + (i+1)*(fin-inicio)/1000;
fx2=1/x2;
if(x1==0 || x2==0);
else segmento(x1,fx1,x2,fx2);
}
}catch(RuntimeException ex){
JOptionPane.showMessageDialog(this,
"Error con el intervalo",
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jButtonRacionalActionPerformed
private void jSpinnerEscStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinnerEscStateChanged
// TODO add your handling code here:
escX=escY=(int)(this.jSpinnerEsc.getValue());
repaint();
}//GEN-LAST:event_jSpinnerEscStateChanged
private void jButtonPolinomioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPolinomioActionPerformed
// TODO add your handling code here:
double x1,x2,fx1,fx2,inicio,fin;
try{
inicio = Integer.parseInt(jTextFieldA.getText());
fin = Integer.parseInt(jTextFieldB.getText());
if(fin<=inicio){
inicio=fin;
}
for(int i=0; i<1000; i++){
x1 = inicio + i*(fin-inicio)/1000;
fx1=x1*x1*x1-x1;
x2 = inicio + (i+1)*(fin-inicio)/1000;
fx2=x2*x2*x2-x2;
segmento(x1,fx1,x2,fx2);
}
}catch(RuntimeException ex){
JOptionPane.showMessageDialog(this,
"Error con el intervalo",
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jButtonPolinomioActionPerformed
private void jButtonBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBorrarActionPerformed
// TODO add your handling code here:
repaint();
}//GEN-LAST:event_jButtonBorrarActionPerformed
private void jButtonExponencialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExponencialActionPerformed
// TODO add your handling code here:
double x1,x2,fx1,fx2,inicio,fin;
try{
inicio = Integer.parseInt(jTextFieldA.getText());
fin = Integer.parseInt(jTextFieldB.getText());
if(fin<=inicio){
inicio=fin;
}
for(int i=0; i<1000; i++){
x1 = inicio + i*(fin-inicio)/1000;
fx1=20*Math.exp(-x1*x1);
x2 = inicio + (i+1)*(fin-inicio)/1000;
fx2=20*Math.exp(-x2*x2);
segmento(x1,fx1,x2,fx2);
}
}catch(RuntimeException ex){
JOptionPane.showMessageDialog(this,
"Error con el intervalo",
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jButtonExponencialActionPerformed
private void jButtonLogaritmoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLogaritmoActionPerformed
// TODO add your handling code here:
double x1,x2,fx1,fx2,inicio,fin;
try{
inicio = Integer.parseInt(jTextFieldA.getText());
fin = Integer.parseInt(jTextFieldB.getText());
if(fin<=inicio){
inicio=fin;
}
for(int i=0; i<1000; i++){
x1 = inicio + i*(fin-inicio)/1000;
fx1=Math.log(x1*x1);
x2 = inicio + (i+1)*(fin-inicio)/1000;
fx2=Math.log(x2*x2);
segmento(x1,fx1,x2,fx2);
}
}catch(RuntimeException ex){
JOptionPane.showMessageDialog(this,
"Error con el intervalo",
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jButtonLogaritmoActionPerformed
private void jButtonTrigomometricaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTrigomometricaActionPerformed
// TODO add your handling code here:
double x1,x2,fx1,fx2,inicio,fin;
try{
inicio = Integer.parseInt(jTextFieldA.getText());
fin = Integer.parseInt(jTextFieldB.getText());
if(fin<=inicio){
inicio=fin;
}
for(int i=0; i<1000; i++){
x1 = inicio + i*(fin-inicio)/1000;
fx1=10*Math.sin(x1*x1-5);
x2 = inicio + (i+1)*(fin-inicio)/1000;
fx2=10*Math.sin(x2*x2-5);
segmento(x1,fx1,x2,fx2);
}
}catch(RuntimeException ex){
JOptionPane.showMessageDialog(this,
"Error con el intervalo",
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jButtonTrigomometricaActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(graficas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(graficas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(graficas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(graficas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new graficas().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel PanelPrincipal;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.ButtonGroup buttonGroup3;
private javax.swing.ButtonGroup buttonGroup4;
private javax.swing.ButtonGroup buttonGroup5;
private javax.swing.ButtonGroup buttonGroup6;
private javax.swing.JButton jButtonBorrar;
private javax.swing.JButton jButtonExponencial;
private javax.swing.JButton jButtonLogaritmo;
private javax.swing.JButton jButtonPolinomio;
private javax.swing.JButton jButtonRacional;
private javax.swing.JButton jButtonTrigomometrica;
private javax.swing.JLabel jLabelA;
private javax.swing.JLabel jLabelB;
private javax.swing.JLabel jLabelEscala;
private javax.swing.JSpinner jSpinnerEsc;
private javax.swing.JTextField jTextFieldA;
private javax.swing.JTextField jTextFieldB;
// End of variables declaration//GEN-END:variables
}