Drawing in a JPanel

Hello all,

I am trying to set up a Java Desktop Application using net beans. It requires that I do some drawing; hence, fromsurfing the web, is discovered that most people draw on Jpanels. I am currently trying to draw on a JPanel, but this has proved abortive. 99% of the examples I see online are for Applets, I have tried to extract what I can, but this has not helped me. I have tried using both Graphics, and Graphics2D components, still nothing. Can anyone please rescue me…PLEASE!!!


package justdraw;

import java.awt.*;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class JustDrawApp extends SingleFrameApplication {

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new JustDrawView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of XtreemSimpleBarCodeGeneratorApp
     */
    public static JustDrawApp getApplication() {
        return Application.getInstance(JustDrawApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(JustDrawApp.class, args);
    }
}


package justdraw;

import java.awt.*;
import org.jdesktop.application.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * The application's main frame.
 */
public class JustDrawView extends FrameView
{
    public JustDrawView(SingleFrameApplication app)
    {
        super(app);

        initComponents();
        //
        displayPnl.setBackground(Color.white);
        displayPnl.add(new MyDrawLine());
    }//public JustDrawView(SingleFrameApplication app)

    // Variables declaration - do not modify
    private javax.swing.JPanel displayPnl;
    // End of variables declaration
}//public class JustDrawView extends FrameView 


public class MyDrawLine extends JPanel
{
    public MyDrawLine(){}

    @Override
    public Dimension getPreferredSize()
    {
        return new  Dimension(240, 50);
    }//public Dimension getPreferredSize()

    @Override
    public int getY() { return 0; }
    @Override
    public int getX() { return 0; }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor( Color.red );
        // X Start, Y Start, X End, Y End
        // X = <---------->
        g.drawLine ( 0, 0, 240, 50 );
    }//protected void paintComponent(Graphics g)
}//public class MyDrawLine extends JPanel

Currently when it runs nothing is drawn in the JPanel. For now I am still testing trying to figure out how everything works, but eventually, the users will set certain parameters, which will be used to draw at run-time as they vary parameters.

Cheers.

I found a solution on YouTube

Painting with java.awt on a JPanel in a Desktop Application Java

Cheers.