2023-03-03 (金) 11:55:53

/** 2005/03/24









 */
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class TestJLayeredPane1 extends JFrame {


	String BG_IMG = "bg.gif";

	public static void main(String[] argv){
		TestJLayeredPane1 f = new TestJLayeredPane1();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		f.setPreferredSize( new Dimension(700,500));
		f.pack();
		f.setVisible(true);
	}

	static final int BACKLAYER = 1;
	static final int FORELAYER = 2;

	BGImageLayeredPane layerPane;

	/** */
	public TestJLayeredPane1(){
		super("TestJLayeredPane1");


		Image img = null;
		File f = new File(BG_IMG);
		if (f.isFile()){
			ImageIcon icon = new ImageIcon(BG_IMG);
			img = icon.getImage();
		}

		layerPane = new BGImageLayeredPane();
		layerPane.setImage(img);

		for (int i=0; i<7; i++){
			JPanel p = createPanel(i);
			p.setLocation(i*70 + 20, i*50 + 15);
			layerPane.add(p, BACKLAYER);
		}
		setContentPane(layerPane);
	}

	int[] colors = { 0xdddddd, 0xaaaaff, 0xffaaaa, 0xaaffaa, 0xffffaa, 0xffaaff, 0xaaffff };

	Color getColor(int i, float f){
		int b = (int)((i & 0xff) * f);
		i = i >> 8;
		int g = (int)((i & 0xff) * f);
		i = i >> 8;
		int r = (int)((i & 0xff) * f);
		return new Color(r,g,b);
	}


	JPanel createPanel(int i){


		JLabel label = new JLabel(s);
		label.setFont(FONT);
		label.setOpaque(true);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBackground( getColor(colors[i], 0.85f));
		Border border1 = BorderFactory.createEmptyBorder(4, 4, 4, 4);;
		label.setBorder(border1);

		JTextArea text = new JTextArea();
		text.setBackground( new Color(colors[i]));
		text.setMargin(new Insets(4,4,4,4));
		text.setLineWrap(true);

		JPanel p = new JPanel();

		Color col = getColor(colors[i], 0.5f);
		Border border = BorderFactory.createLineBorder(col, 1);
		p.setBorder(border);


		DragMouseListener  li = new DragMouseListener(p);
		p.addMouseListener(li);
		p.addMouseMotionListener(li);

		p.setLayout( new BorderLayout());
		p.add(label, BorderLayout.NORTH);
		p.add(text, BorderLayout.CENTER);
		p.setSize( new Dimension(150, 120));
		return p;
	}


	class DragMouseListener implements MouseListener, MouseMotionListener {
		Point origin;
		JPanel panel;

		DragMouseListener(JPanel p){
			panel = p;
		}
		public void mousePressed(MouseEvent e){
			origin = new Point( e.getX(), e.getY());

			layerPane.moveToFront(panel);
		}
		public void mouseDragged(MouseEvent e){
			if (origin == null)
				return;


			int dx = e.getX() - origin.x;
			int dy = e.getY() - origin.y;
			Point p = panel.getLocation();
			panel.setLocation( p.x + dx, p.y + dy);
		}

		public void mouseClicked(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
		public void mouseMoved(MouseEvent e) {}
	}


	class BGImageLayeredPane extends JLayeredPane {

		BGImageLayeredPane(){
		}

		void setImage(Image img){
			bgImage = img;
		}
		Image bgImage;

		//override
		public void paint(Graphics g) {
			if (bgImage != null){
				int imageh = bgImage.getHeight(null);
				int imagew = bgImage.getWidth(null);

				Dimension d = getSize();
				for (int h=0; h<d.getHeight(); h += imageh){
					for (int w=0; w<d.getWidth(); w += imagew){
						g.drawImage(bgImage, w, h, this);
					}
				}
			}
			super.paint(g);
		}
	}
}