// (C)2007 Attila Kovacs <attila@submm.caltech.edu>

import java.awt.*;
import java.awt.geom.*;
import java.text.*;
import javax.swing.*;
import javax.swing.border.*;


public class AxisLabel extends JPanel {
    Font font = new Font("Monospaced", Font.BOLD | Font.ITALIC, 14);
    int axis = 0;
    int thickness = 30;
    String label = "";

    final static int BOTTOM = 0;
    final static int LEFT = 1;
    final static int TOP = 2;
    final static int RIGHT = 3;
    
    public AxisLabel() { create(); }

    public AxisLabel(String text, int orientation) { 
	label = text;
	axis = orientation;
	create();
    }

    public void create() {
	int width = axis % 2 == 0 ? 1 : thickness;
	int height = axis % 2 == 0 ? thickness : 1;
	
	setPreferredSize(new Dimension(width, height));
	setBackground(Color.WHITE);
    }

        
    public void paintComponent(Graphics g){
	super.paintComponent(g); 

	double angle = axis * Math.PI / 2.0;
	AffineTransform transform = new AffineTransform();

	FontMetrics fm = g.getFontMetrics(font);
	int fontheight=fm.getHeight();
	setFont(font);

	int height = getHeight();
	int width = getWidth();

	Graphics2D g2 = (Graphics2D) g;
	Rectangle bounds = font.getStringBounds(label, g2.getFontRenderContext()).getBounds();	

	int x0 = (int)Math.round((getWidth() - bounds.width) / 2.0);
	int y0 = (int)Math.round((getHeight() - bounds.height) / 2.0 + fm.getAscent());

	g2.rotate(angle, width/2.0, height/2.0);	 
	g.drawString(label, x0, y0);
    }
}
