user2984101 user2984101 - 3 months ago 15
Java Question

Java Netbeans - How to draw on a Jpanel inside a Jpanel? (Panel constructor)

Im trying to learn Java and i have now met Jpanels, and since i'm using Netbeans i can use their "constructor" in which i can design the panel i want inside my frame.

With netbeans i made a panel inside my panel, and now i wonder:

How do i draw on a panel inside a panel?

(The panel i want to draw on is EvoPanel)

This is my current design

The dark gray box is the second panel inside the panel, if i try to draw a line from the top left corner to the bottom right i end up with this

My code:

Vindue.java

package vindue;
import javax.swing.*;

/**
*
* @author BE56df
*/
public class Vindue {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

paneler panel = new paneler();

JFrame vindue = new JFrame("NEZ - Evolution Simulator v0.1");
vindue.add(panel);

vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

vindue.setResizable(false);
vindue.pack();
vindue.setVisible(true);

}

}


Paneler.java

package vindue;
import java.awt.*;
import javax.swing.*;

/**
*
* @author BE56df
*/
public class paneler extends javax.swing.JPanel {

/**
* Creates new form paneler
*/
public paneler() {
initComponents();
}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawLine(0, 0, 750, 550);

}

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

EvoPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jToggleButton1 = new javax.swing.JToggleButton();

setMaximumSize(new java.awt.Dimension(750, 550));
setMinimumSize(new java.awt.Dimension(750, 550));
setPreferredSize(new java.awt.Dimension(750, 550));

EvoPanel.setBackground(new java.awt.Color(200, 200, 200));
EvoPanel.setMaximumSize(new java.awt.Dimension(550, 550));
EvoPanel.setMinimumSize(new java.awt.Dimension(550, 550));

javax.swing.GroupLayout EvoPanelLayout = new javax.swing.GroupLayout(EvoPanel);
EvoPanel.setLayout(EvoPanelLayout);
EvoPanelLayout.setHorizontalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);
EvoPanelLayout.setVerticalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Control panel");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

jToggleButton1.setText("Auto play");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(jLabel1)
.addGap(0, 58, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton1))
);
}// </editor-fold>


// Variables declaration - do not modify
private javax.swing.JPanel EvoPanel;
private javax.swing.JLabel jLabel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}

Answer

Obviously you paint the line on the main JFrame (paneler panel) when you really wants to paint on EvoPanel. You should create a class EvoPanel (with a paintComponent method) and add after paneler#initComponents.

public paneler() {
  initComponents();
  EvoPanel evoPanel = new EvoPanel();
  add(evoPanel) // properly in a layout
}

Advice: for simple GUI's don't use Mantisse (Netbeans GUI builder tool), it won't give you any benefit.

Comments