I have 8 jpanels with background color and I am trying to draw a rectangle on top of it but it shows under it and I cant get it on top
Here is the code for initializing a JPanel
tempPanel = new JPanel();
tempPanel.setBackground(Color.black);
tempPanel.setOpaque(true);
tempPanel.setLayout(null);
panel.add(tempPanel);
tempPanel.setBounds(195 + insets.left, 155 + insets.top,
main.getLanes().get(5).getDistance()*5, 20);
if (!added)
lanes.add(tempPanel);
and the code for drawing rectangle; it is inside a class that extends Jpanel and inisde paintComponents:
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawLanes();
int i, j;
ArrayList<Lane> lanes2= new ArrayList<Lane>();
ArrayList<Car> cars= new ArrayList<Car>();
for(int counter = 0; counter < 10; counter ++) {
lanes2 = main.getLanes();
for( i = 0; i< lanes.size(); i++) {
lanes.get(i).removeAll();
cars = lanes2.get(i).getCars();
for ( j = 0; j < cars.size(); j++) {
Insets insets = lanes.get(i).getInsets();
g2d.setColor(Color.red);
g2d.draw(new Rectangle(lanes.get(i).getLocation().x + 5*(lanes2.get(i).getDistance() - cars.get(j).getCurrent_pos()), lanes.get(i).getLocation().y,
cars.get(j).getCar_size()*5, lanes.get(i).getHeight()));
JLabel temp = new JLabel("Car");
temp.setForeground(Color.red);
temp.setBounds(insets.left + 5*(lanes2.get(i).getDistance() - cars.get(j).getCurrent_pos()) , insets.top,
cars.get(j).getCar_size()*5, lanes.get(i).getHeight());
lanes.get(i).add(temp);
}
}
}
Thanks in adavnce