我记得Graphics中有个类似translate的抽象函数好像其子类的实现是可以实现你的想法的……jdk中的demo中有一个也有你要的功能

解决方案 »

  1.   

    能在详细一点吗?translate 应该怎么用呢?
      

  2.   

    从JPanel中继承出一个LabelPanel类,包含angle,text属性。
    然后重写paintComponent方法:
        protected void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
            g2.setFont(font);
            FontMetrics fm = g2.getFontMetrics();
            int height = fm.getHeight();
            int width = fm.stringWidth(text);        if(Math.abs(angle-Math.toRadians(90))<0.000001d) {
                g2.rotate(angle, height/2, height/2);
            }
            else if(Math.abs(angle-Math.toRadians(270))<0.000001d) {
                g2.rotate(angle, width/2, width/2);
            }
            else {
                g2.rotate(0);
            }        if (null!=bgcolor)
            {
                g2.setColor(bgcolor);
                g2.fillRect(0, 0, width, height);
            }
            g2.setColor(color);
            g2.drawString(text, 0, font.getSize());
        }