I created a Class with JPanel extended to it. Then when I created an object of it and added to an existing JPanel, it doesn't show up. What am I doing wrong here?
public class StartTower extends JPanel{
private int value;
public StartTower(int value){
this.value = value;
}
public static StartTower send(int value){
return new StartTower(value);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(752, 359);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(120, 60, 10, 270);
g.fillRect(20, 330, 210, 10);
g.fillRect(375, 60, 10, 270);
g.fillRect(275, 330, 210, 10);
g.fillRect(630, 60, 10, 270);
g.fillRect(530, 330, 210, 10);
int val1 = 20;
int val2 = 315;
int val3 = 210;
int col = 100;
for (int i = 0; i < value; i++) {
g.setColor(new Color(100, col, 100));
g.fillRect(val1, val2, val3, 15);
System.out.println(val1 + " " + val2 + " " + val3 + " " + col);
val1 += 10;
val2 -= 15;
val3 -= 20;
col -= 10;
}
}
}
Class with JPanel extended
private void startBtActionPerformed(java.awt.event.ActionEvent evt) {
//mainPanel.repaint();
name = javax.swing.JOptionPane.showInputDialog(rootPane, "Enter you name!", "Ready?", WIDTH);
if (name != null) {
clockFunc(true);
game.initialDisk((int) disksSpinner.getValue());
StartTower startTower = new StartTower((int) disksSpinner.getValue());
mainPanel.setLayout(null);
this.add(startTower);
a2bBt.setEnabled(true);
a2cBt.setEnabled(true);
b2aBt.setEnabled(true);
b2cBt.setEnabled(true);
c2aBt.setEnabled(true);
c2bBt.setEnabled(true);
optimumLabel.setText(getNoOfOptimumMoves((int) disksSpinner.getValue()));
disksSpinner.setEnabled(false);
startBt.setEnabled(false);
}
}
Method where I made the object of the before said class and added it to the existing JPanel. (mainPanel is the existing JPanel created using drag drop in NetBeans).
Why isn't this showing up? What's wrong in the code? Thanks in advance!