2015-05-04 95 views
0

使用Netbeans 8.0.2開始使用Java 2D時遇到困難。如果有人可以發佈一些非常基本的代碼,只需要一個面板和一個簡單的形狀,就會非常感激。Netbeans 8.0.2簡單的2D繪圖

我經歷過這麼多的教程,覆蓋了paintcomponent()super.paintcomponent()等,但真的不知道怎樣還是怎樣實際調用此方法,因爲它沒有得到我的代碼:(

package guitest2; 

import java.awt.Color; 
import java.awt.Graphics; 

public class TablePanel extends javax.swing.JPanel { 

public TablePanel() { 
    initComponents(); 
    this.setSize(50, 50); 
} 

/** 
* 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() { 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 300, Short.MAX_VALUE) 
    ); 
}// </editor-fold>       
@Override 
public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    g.setColor(Color.black); 
    g.drawRect(100, 100, 100, 100); 
}   
} 
調用
package guitest2; 

public class TableFrame extends javax.swing.JFrame { 
public TableFrame() { 
    initComponents(); 
} 


@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 529, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 393, Short.MAX_VALUE) 
    ); 

    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(TableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TableFrame.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 TableFrame().setVisible(true); 
     } 
    }); 
}    
} 

package guitest2; 
import javax.swing.JFrame; 
public class GUITest2 { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    TableFrame tf = new TableFrame(); 
    tf.setTitle("BJ"); 
    tf.setSize(1200,800); 
    tf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    TablePanel p = new TablePanel(); 
    tf.setVisible(true); 
    tf.add(p); 

} 
} 
+0

您應該發佈無法顯示您嘗試的代碼。 – Karsten

+0

「它沒有在我的代碼中被調用」,但你的代碼在哪裏?發佈你的代碼或解釋你的嘗試。 – Ajoy

+0

添加代碼,謝謝 – Noob999

回答

0

下面是對JPanel繪製一個橢圓的簡單代碼Frame

import javax.swing.*; 
import java.awt.*; 
public class Test extends JFrame 
{ 
public Test() 
{ 
    setSize (300,300); 
    JPanel panel=new JPanel(){ 
     @Override 
     public void paint (Graphics g){ 
      g.setColor (Color.BLUE); 
      g.fillOval(100, 100, 100, 100); 
     } 
    }; 
    add(panel); 
    setVisible (true); 
} 


public static void main (String args[]){ 
    new Test(); 
} 
}