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.

Edited 6 Years Ago by gauravk_bhanot: n/a

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

This article has been dead for over six months. Start a new discussion instead.