Chinaunix首页 | 论坛 | 博客
  • 博客访问: 176611
  • 博文数量: 17
  • 博客积分: 299
  • 博客等级: 下士
  • 技术积分: 292
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-24 12:11
文章分类
文章存档

2017年(1)

2013年(1)

2012年(15)

我的朋友

分类: Java

2012-03-19 13:39:02

由于希望改变TabbedPanel的tab的形状,查看了BasicTabbedPaneUI.java的一部分代码,
其中:
void paint ->paintContentBorder
             paintTabArea (整个区域?)         -> paintTab -> paintTabBackground
                                                              paintTabBorder(外框)
                                                              paintText(标题)
                                                              paintIcon(图片)
                                                              paintFocusIndicator
 
 
据我目前的理解应该是:要修改形状,主要是修改paintTabBackground和paintTabBorder?
Tab上Label的位置是由layoutLabel来决定。
 
下面是一个例子:
大部分函数是从BasicTabbedPaneUI.java 拷贝过来,但是修改了斜体字部分。
将Tab的形状修改为类似:
clip_image001

 

Codes:

 

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.text.View;

public class TabShapeTest extends JFrame{

/**
     *
     */
    private static final long serialVersionUID = 1L;

public static void main(String [] args)  {

    new TabShapeTest().setVisible(true);
}

public TabShapeTest() {
    JTabbedPane tpShape = new JTabbedPane();
    tpShape.setUI(new ShapeTabTabbedPaneUI());
    add(tpShape);
    setPreferredSize(new Dimension(280,300));
    tpShape.addTab("first",new JPanel());
    tpShape.addTab("second?????", new JPanel());
    tpShape.addTab("third", new JPanel());
    tpShape.setBackgroundAt(1, Color.yellow);
    tpShape.setBackgroundAt(2, Color.BLUE);

    pack();
}

public static class ShapeTabTabbedPaneUI extends javax.swing.plaf.basic.BasicTabbedPaneUI {
    private   Color selectedColor;

    protected void installDefaults() {
        super.installDefaults();
    }
    public ShapeTabTabbedPaneUI() {

        }

    protected void paintTabBackground(Graphics g, int tabPlacement,
               int tabIndex, int x, int y, int w, int h, boolean isSelected) {
              Graphics2D g2d = (Graphics2D) g;

               g.setColor(!isSelected || selectedColor == null?
                       tabPane.getBackgroundAt(tabIndex) : selectedColor);
            switch(tabPlacement) {
              case LEFT:
                  g.fillRect(x+1, y+1, w-1, h-3);
                  break;
              case RIGHT:
                  g.fillRect(x, y+1, w-2, h-3);
                  break;
              case BOTTOM:
                  g.fillRect(x+1, y, w-3, h-1);
                  break;
              case TOP:
              default:
                  //g.fillRect(x+1, y+1, w-3, h-1);
                  g2d.fill(this.getTabArea(x+1, y+1, w-11, h-1));
            }

            }
    private Shape getTabArea(int x, int y, int w, int h) {
          Rectangle2D rec = new Rectangle2D.Double(x, y, w, h );
          Area aRec = new Area(rec);
          Path2D.Double triangle=new Path2D.Double();
          triangle.moveTo(x+w, y);
          triangle.lineTo(x+w+10, y+h/2);
          triangle.lineTo(x+w, y+h);
          triangle.closePath();
          aRec.add(new Area(triangle));
          triangle.reset();

          return aRec;
        }

    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
                   int x, int y, int w, int h, boolean isSelected) {
               g.setColor(lightHighlight);

                switch (tabPlacement) {
                  case LEFT:
                      g.drawLine(x+1, y+h-2, x+1, y+h-2); // bottom-left highlight
                      g.drawLine(x, y+2, x, y+h-3); // left highlight
                      g.drawLine(x+1, y+1, x+1, y+1); // top-left highlight
                      g.drawLine(x+2, y, x+w-1, y); // top highlight

                      g.setColor(shadow);
                      g.drawLine(x+2, y+h-2, x+w-1, y+h-2); // bottom shadow

                      g.setColor(darkShadow);
                      g.drawLine(x+2, y+h-1, x+w-1, y+h-1); // bottom dark shadow
                      break;
                  case RIGHT:
                      g.drawLine(x, y, x+w-3, y); // top highlight

                      g.setColor(shadow);
                      g.drawLine(x, y+h-2, x+w-3, y+h-2); // bottom shadow
                      g.drawLine(x+w-2, y+2, x+w-2, y+h-3); // right shadow

                      g.setColor(darkShadow);
                      g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right dark shadow
                      g.drawLine(x+w-2, y+h-2, x+w-2, y+h-2); // bottom-right dark shadow
                      g.drawLine(x+w-1, y+2, x+w-1, y+h-3); // right dark shadow
                      g.drawLine(x, y+h-1, x+w-3, y+h-1); // bottom dark shadow
                      break;
                  case BOTTOM:
                      g.drawLine(x, y, x, y+h-3); // left highlight
                      g.drawLine(x+1, y+h-2, x+1, y+h-2); // bottom-left highlight

                      g.setColor(shadow);
                      g.drawLine(x+2, y+h-2, x+w-3, y+h-2); // bottom shadow
                      g.drawLine(x+w-2, y, x+w-2, y+h-3); // right shadow

                      g.setColor(darkShadow);
                      g.drawLine(x+2, y+h-1, x+w-3, y+h-1); // bottom dark shadow
                      g.drawLine(x+w-2, y+h-2, x+w-2, y+h-2); // bottom-right dark shadow
                      g.drawLine(x+w-1, y, x+w-1, y+h-3); // right dark shadow
                      break;
                  case TOP:
                  default:
                      g.drawLine(x, y+2, x, y+h-1); // left highlight
                      g.drawLine(x+1, y+1, x+1, y+1); // top-left highlight
                    //  g.drawLine(x+2, y, x+w-3, y); // top highlight
                      g.drawLine(x+2,y, x+w-10, y);
                      g.setColor(shadow);
                    //  g.drawLine(x+w-2, y+2, x+w-2, y+h-1); // right shadow
                      g.drawLine(x+w-10,y, x+w-1, y+h/2-1);
                      g.drawLine(x+w-1,y+h/2+1,x+w-10,y+h);
                      g.setColor(darkShadow);
                      //g.drawLine(x+w-1, y+2, x+w-1, y+h-1); // right dark-shadow
                      //g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right shadow
                      g.drawLine(x+w-1,y+h/2+2,x+w-10,y+h+1);
                }
        }
    protected void layoutLabel(int tabPlacement,
                   FontMetrics metrics, int tabIndex,
                   String title, Icon icon,
                   Rectangle tabRect, Rectangle iconRect,
                   Rectangle textRect, boolean isSelected ) {
                  textRect.x = textRect.y = iconRect.x = iconRect.y = 0;

                  View v = getTextViewForTab(tabIndex);
                  if (v != null) {
                      tabPane.putClientProperty("html", v);
                  }

                  SwingUtilities.layoutCompoundLabel(tabPane,
                               metrics, title, icon,
                               SwingUtilities.CENTER,
                               SwingUtilities.CENTER,
                               SwingUtilities.CENTER,
                               SwingUtilities.TRAILING,
                               tabRect,
                               iconRect,
                               textRect,
                               textIconGap);

                  tabPane.putClientProperty("html", null);

                  int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
                  int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
                  iconRect.x += xNudge;
                  iconRect.y += yNudge;
                  textRect.y += yNudge;
                switch (tabPlacement) {
                  case LEFT:
                      textRect.x += xNudge;
                      break;
                  case RIGHT:
                      textRect.x += xNudge;
                      break;
                  case BOTTOM:
                      textRect.x += xNudge;
                      break;
                  case TOP:
                  default:
                      textRect.x += xNudge-4;
                }
           }
}
}

 
结果:
image
 
 
 
阅读(3439) | 评论(0) | 转发(0) |
0

上一篇:IBM VIOS login(IVM)

下一篇:Clean project

给主人留下些什么吧!~~