import javax.swing.*;
import java.awt.*;
class drawer extends JPanel
{ Color color;
int x;
	drawer(int x,Color color){
		this.color=color;//color of rectangle
		this.x=x;//size
	}
	public void paintComponent(Graphics g)
	{

		super.paintComponent(g);
		g.drawRect(0,0,x,x);
		g.setColor(color);
		g.fillRect(0,0,x,x);

	}
}
public class layers {

    public static void main(String[] args) {
    JLayeredPane layerd=new JLayeredPane();
    layerd.setSize(800,800);
layerd.setVisible(true);
    layerd.add(new drawer(50,Color.BLUE),2);
    layerd.validate();
    layerd.add(new drawer(70,Color.red),1);
    layerd.validate();
    JFrame frame=new JFrame();
    frame.setVisible(true);
    frame.setSize(800,800);
    frame.add(layerd);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    }
}

it displays nothing am i missing something.

import javax.swing.*;
import java.awt.*;
class drawer extends JPanel
{ Color color;
int x;
	drawer(int x,Color color){
		this.color=color;//color of rectangle
		this.x=x;//size
	}
	public void paintComponent(Graphics g)
	{

		super.paintComponent(g);
		g.drawRect(0,0,x,x);
		g.setColor(color);
		g.fillRect(0,0,x,x);

	}
}
public class layers {

    public static void main(String[] args) {
    JLayeredPane layerd=new JLayeredPane();
    layerd.setSize(800,800);
    layerd.setVisible(true);
    layerd.add(new drawer(50,Color.BLUE),2);
    layerd.validate();
    layerd.add(new drawer(70,Color.red),1);
    layerd.validate();
    JFrame frame=new JFrame();

    frame.setSize(800,800);
    frame.add(layerd);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    }

change the position of
layerd.setVisible(true)
as i did

-------
hope this is wat u need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.