Netbeans 8.0.2 Simple 2D Drawing

Having trouble getting started with Java 2D using Netbeans 8.0.2. Would really appreciate if someone could post some very basic code for just a frame with a panel and a simple shape within.

I've been through so many tutorials, overridden the paintcomponent(), super.paintcomponent() etc. but really not sure how or what actually invokes this method, as it isn't getting invoked in my code :(

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);

 }
}

Answers


Here's the simple code for drawing an oval on JPanel within a 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();
 }
}

Need Your Help

How can i use 6 bits to store value?

c++ types bit-manipulation bit flags

My data unit (a network packet header) i am currently working on has 2 flags in its definition, stored in a byte field and accessed via bitwise operators. Unfortunately, i need only 2 bits and thin...

SQLAlchemy unstable behaviour with Oracle

python oracle flask sqlalchemy

I've been trying to resolve an SQLAlchemy issue that I'm having and narrowed it down to this simple script: