Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/03/2011, 23:38
celineadiction
 
Fecha de Ingreso: octubre-2010
Mensajes: 93
Antigüedad: 13 años, 6 meses
Puntos: 0
Menus llamando clases

Hola amigos, el día de hoy vengo con una duda... verán, hice algunas clases en java con netbeans para graficar, una de ellas es esta:

GraphingData.java
Código Java:
Ver original
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class GraphingData extends JPanel {
  5. int x0,xN,y0,yN;
  6. double xmin,xmax,ymin,ymax;
  7. int h, w;
  8.  
  9.    
  10. public void paintComponent(Graphics g) {
  11.  
  12.     super.paintComponent(g);
  13.     Graphics2D g2 = (Graphics2D)g;
  14.     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  15.                             RenderingHints.VALUE_ANTIALIAS_ON);
  16.     double x1,y1,x2,y2;
  17.     int j1,j2;
  18.     w=this.getParent().getWidth();
  19.     h=this.getParent().getHeight();
  20.     x0 = y0 = 0;
  21.     xN = w-1;
  22.     yN = h-1;
  23.     xmin = -5.0;
  24.     xmax = 5.0;
  25.     ymin = -7.0;
  26.     ymax = 1.0;
  27.  
  28.     j1 = ValorY( 0 );
  29.  
  30.     g2.setPaint(Color.blue);
  31.  
  32.     for( int i=0; i < w; i++ ){
  33.          j2 = ValorY( i+1 );
  34.          g2.drawLine(i,j1,i+1,j2 );
  35.         j1 = j2;
  36.     }//for en el que se imprimen los valores de la grafica
  37. }
  38.  
  39. private int ValorY( int valor ) {
  40.          double x,y;
  41.          int retorno;
  42.          // Cartesianas equivalentes al punto de la pantalla
  43.          x = (valor * (xmax-xmin) / (h-1)) + xmin;
  44.          // Calculamos LA FUNCION
  45.          y = (3*x*x) -6;
  46.          // Escalamos la coordenada y dentro de los limites de la ventana
  47.          retorno = (int)( (y-ymin) * (w-1) / (ymax-ymin) );
  48.          // Reconvertinos el valor cartesiano a punto de pantalla
  49.          retorno = h - retorno;
  50.          return( retorno );
  51. }//funcion ValorY
  52. }//fin de la clase

Después lo que estoy intentando hacer es generar un menú con los trabajos que he hecho, para que en vez de abrir clase por clase, mi maestro vea el menu y de ahi seleccione el trabajo que va a calificar

tarea.java
Código Java:
Ver original
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.event.*;
  4. public class tarea extends JFrame {
  5.  
  6.     public tarea() {
  7.         initComponents();
  8.     }
  9.  
  10.    
  11.     @SuppressWarnings("unchecked")
  12.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  13.     private void initComponents() {
  14.  
  15.         jPopupMenu1 = new javax.swing.JPopupMenu();
  16.         jMenuBar1 = new javax.swing.JMenuBar();
  17.         Unidad1 = new javax.swing.JMenu();
  18.         Suma = new javax.swing.JMenuItem();
  19.         Coseno = new javax.swing.JMenuItem();
  20.         Funcion = new javax.swing.JMenuItem();
  21.         GraficarSeno = new javax.swing.JMenuItem();
  22.  
  23.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  24.  
  25.         Unidad1.setText("Unidad1");
  26.         Unidad1.addActionListener(new java.awt.event.ActionListener() {
  27.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  28.                 Unidad1ActionPerformed(evt);
  29.             }
  30.         });
  31.  
  32.         Suma.setText("Suma Producto");
  33.         Unidad1.add(Suma);
  34.  
  35.         Coseno.setText("Graficar Coseno");
  36.         Unidad1.add(Coseno);
  37.  
  38.         Funcion.setText("Funcion");
  39.         Funcion.addKeyListener(new java.awt.event.KeyAdapter() {
  40.             public void keyPressed(java.awt.event.KeyEvent evt) {
  41.                 FuncionKeyPressed(evt);
  42.             }
  43.         });
  44.         Unidad1.add(Funcion);
  45.  
  46.         GraficarSeno.setText("Graficar Seno");
  47.         Unidad1.add(GraficarSeno);
  48.  
  49.         jMenuBar1.add(Unidad1);
  50.  
  51.         setJMenuBar(jMenuBar1);
  52.  
  53.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  54.         getContentPane().setLayout(layout);
  55.         layout.setHorizontalGroup(
  56.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  57.             .addGap(0, 400, Short.MAX_VALUE)
  58.         );
  59.         layout.setVerticalGroup(
  60.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61.             .addGap(0, 279, Short.MAX_VALUE)
  62.         );
  63.  
  64.         pack();
  65.     }// </editor-fold>//GEN-END:initComponents
  66.  
  67.     private void Unidad1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Unidad1ActionPerformed
  68.        
  69.     }//GEN-LAST:event_Unidad1ActionPerformed
  70.  
  71. private void FuncionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_FuncionKeyPressed
  72.         JFrame f = new JFrame();
  73.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74.         f.add(new GraphingData());
  75.         f.setSize(400,400);
  76.         f.setLocation(200,200);
  77.         f.setVisible(true);
  78.     }//GEN-LAST:event_FuncionKeyPressed
  79.  
  80.     public static void main(String args[]) {
  81.         java.awt.EventQueue.invokeLater(new Runnable() {
  82.             public void run() {
  83.                 new tarea().setVisible(true);
  84.             }
  85.         });
  86.     }
  87.  
  88.     // Variables declaration - do not modify//GEN-BEGIN:variables
  89.     private javax.swing.JMenuItem Coseno;
  90.     private javax.swing.JMenuItem Funcion;
  91.     private javax.swing.JMenuItem GraficarSeno;
  92.     private javax.swing.JMenuItem Suma;
  93.     private javax.swing.JMenu Unidad1;
  94.     private javax.swing.JMenuBar jMenuBar1;
  95.     private javax.swing.JPopupMenu jPopupMenu1;
  96.     // End of variables declaration//GEN-END:variables
  97.  
  98. }

el problema radica en las líneas 71 en adelante en la funcion FuncionKeyPressed, son las mismas líneas que agrego en el main de la clase GraphingData para que al momento de correrla salga la funcion, pero en este caso, al mandarla llamar desde la clase tarea no pasa nada... quisiera que porfavor me orientaran y de ante mano muchisimas gracias por la ayuda que siempre me dan

Última edición por celineadiction; 02/03/2011 a las 23:57