2012-10-08 3 views
0

NetBeans를 사용하고 있으며 대화 상자 내에있는 JDialog로 확장되는 클래스가 있으며 탭 분할 창을 작성했습니다. Tabbed Pane에는 6 개의 다른 탭이 있으며 6 개의 다른 패널이 있습니다. 내가 원하는 것은 다른 탭을 클릭 할 때, 다이어그램이 paint 메소드로 그려지기로되어있는 것입니다.탭이있는 분할 창으로 JDialog를 사용하여 다른 그림 그리기

제 질문은 Dialog 클래스에서 호출되는 다른 클래스의 한 페인트 메서드로 다른 패널에 어떻게 그릴 수 있습니까?

import java.awt.Graphics; 
import javax.swing.JPanel; 

public class Lienzo { 

    private int width = 5; 
    private int height = 5; 
    private int y = 5; 
    private int x = 0; 
    private int x1 = 0; 

    public Graphics Draw(Graphics g, int[] pistas) { 
     //Im not sure if this is the correct way to do it 
     //The diagram gets drawn according to values from an array 
     //The array is not always the same thats why I used the different Panels 

     for (int i = 0; i < pistas.length; i++) { 
      x = pistas[i]; 
      x1 = pistas[i + 1]; 

      g.drawOval(x, y, width, height); 
      g.drawString(Integer.toString(x), x, y); 
      g.drawLine(x, y, x1, y); 

     } 
     return g; 
    } 
} 

나는 너희들이 나에 노력하고 이해를 바랍니다 : 이것은 페인트 방법에 대해 만든 다른 클래스가

package GUI;  

public class NewJDialog extends javax.swing.JDialog{ 

    /** 
    * Creates new form NewJDialog 
    */ 
    public NewJDialog(java.awt.Frame parent, boolean modal) { 
     super(parent, modal); 
     initComponents(); 
    } 

    /** 
    * 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">       
    private void initComponents() { 

     jTabbedPane1 = new javax.swing.JTabbedPane(); 
     jPanel1 = new javax.swing.JPanel(); 
     jPanel2 = new javax.swing.JPanel(); 
     jPanel3 = new javax.swing.JPanel(); 
     jPanel4 = new javax.swing.JPanel(); 
     jPanel5 = new javax.swing.JPanel(); 
     jPanel6 = new javax.swing.JPanel(); 
     jPanel7 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("FCFS", jPanel1); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("SSTF", jPanel2); 

     javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); 
     jPanel3.setLayout(jPanel3Layout); 
     jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("LOOK", jPanel3); 

     javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); 
     jPanel4.setLayout(jPanel4Layout); 
     jPanel4Layout.setHorizontalGroup(
      jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel4Layout.setVerticalGroup(
      jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("LOOK C", jPanel4); 

     javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); 
     jPanel5.setLayout(jPanel5Layout); 
     jPanel5Layout.setHorizontalGroup(
      jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel5Layout.setVerticalGroup(
      jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("SCAN", jPanel5); 

     javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); 
     jPanel6.setLayout(jPanel6Layout); 
     jPanel6Layout.setHorizontalGroup(
      jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel6Layout.setVerticalGroup(
      jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("SCAN C", jPanel6); 

     getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER); 

     jLabel1.setText("Distancia:"); 

     jLabel2.setText("___________"); 

     javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7); 
     jPanel7.setLayout(jPanel7Layout); 
     jPanel7Layout.setHorizontalGroup(
      jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel7Layout.createSequentialGroup() 
       .addGap(21, 21, 21) 
       .addComponent(jLabel1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jLabel2) 
       .addContainerGap(331, Short.MAX_VALUE)) 
     ); 
     jPanel7Layout.setVerticalGroup(
      jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel7Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jLabel2)) 
       .addContainerGap(15, Short.MAX_VALUE)) 
     ); 

     getContentPane().add(jPanel7, java.awt.BorderLayout.PAGE_START); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @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(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the dialog */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true); 
       dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
        @Override 
        public void windowClosing(java.awt.event.WindowEvent e) { 
         System.exit(0); 
        } 
       }); 
       dialog.setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JPanel jPanel3; 
    private javax.swing.JPanel jPanel4; 
    private javax.swing.JPanel jPanel5; 
    private javax.swing.JPanel jPanel6; 
    private javax.swing.JPanel jPanel7; 
    private javax.swing.JTabbedPane jTabbedPane1; 
    // End of variables declaration 
} 

: 여기

는 대화 상자 클래스에 대한 내 코드입니다 내 프로그램으로 해.

답변

3

JPanel 같은 것을 확장하는 "GraphPane"을 만들 것입니다. 이 클래스에서는 그래프에 대한 배열 정보를 제공 할 수있는 방법을 제공합니다. 나는 당신의 draw 방법에서 논리를 취하고 의 paintComponent 방법을 추가 할 것입니다.

그런 다음이 GraphPane의 인스턴스를 각 탭에 추가하고 각 탭에 적절한 배열 데이터를 입력합니다.

데이터를 가져올 위치에 따라 배열 데이터를 만들 때 대화 상자로 전달해야 할 수도 있습니다.