修改Button的背景颜色

在ButtionUI的方法public void paint(Graphics g, JComponent c)中调用drawXpButton(),可以设置Button的背景颜色

 

 

	private void drawXpButton(Graphics g, AbstractButton b, Color c, boolean isRollover) {
		if(!b.isContentAreaFilled()) return;
		if(!b.isOpaque()) return;//不完成透明

		int w = b.getWidth();//Button的宽度
		int h = b.getHeight();//Button的高度

		// paint border background 绘制边框
		Color bg = b.getParent().getBackground();
		g.setColor(bg);
                //g.drawLine(int x1, int y1, int x2, int y2) 
                //在此图形上下文的坐标系统中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。
                //g.drawRect(0, 0, w - 1, h - 1) 是在左上对角与右下对角之间画一条连线
		g.drawRect(0, 0, w - 1, h - 1);

		int spread1 = Theme.buttonSpreadLight[Theme.style];
		int spread2 = Theme.buttonSpreadDark[Theme.style];
		if(!b.isEnabled()) {//按钮不可用
			spread1 = Theme.buttonSpreadLightDisabled[Theme.style];
			spread2 = Theme.buttonSpreadDarkDisabled[Theme.style];
		}

		float spreadStep1 = 10.0f * spread1 / (h - 3);
		float spreadStep2 = 10.0f * spread2 / (h - 3);
		int halfY = h / 2;
		int yd;

		for (int y = 1; y < h - 1; y++) {
			if(y < halfY) {
				yd = halfY - y;
				g.setColor(ColorRoutines.lighten(c, (int)(yd * spreadStep1)));
			}
			else if(y == halfY) {
				g.setColor(c);
			}
			else {
				yd = y - halfY;
				g.setColor(ColorRoutines.darken(c, (int)(yd * spreadStep2)));
			}

			g.drawLine(2, y, w - 3, y);

			if(y == 1) {
				// left vertical line 画左垂直的线
				g.drawLine(1, 1, 1, h - 2);

				if(isRollover || isDefault) {
					// right vertical line 画右垂直的线
					g.drawLine(w - 2, 1, w - 2, h - 2);
				}
			}
			else if(y == h - 2 && !isRollover && !isDefault) {
				// right vertical line 画右垂直的线
				g.drawLine(w - 2, 1, w - 2, h - 2);
			}
		}

		// 1 pixel away from each corner
		if(isRollover) {//指示鼠标在按钮上
                    //获取指示鼠标在按钮上的颜色
			g.setColor(Theme.buttonRolloverColor[Theme.style].getColor());
			g.drawLine(1, h - 2, 1, h - 2);//绘制左下角点
			g.drawLine(w - 2, h - 2, w - 2, h - 2);//绘制右上角点
		}
		else if(isDefault) {
                    //获取默认的颜色
			g.setColor(Theme.buttonDefaultColor[Theme.style].getColor());
			g.drawLine(1, h - 2, 1, h - 2);//绘制左下角点
			g.drawLine(w - 2, h - 2, w - 2, h - 2);//绘制右上角点
		}
	}
	
	
	
	public static Color lighten(Color c, int amount) {
		if (amount < 0) return c;
		
		if (amount > 100) amount = 100;

		int dr = (int)Math.round((255 - c.getRed()) * amount / 100.0);
		int dg = (int)Math.round((255 - c.getGreen()) * amount / 100.0);
		int db = (int)Math.round((255 - c.getBlue()) * amount / 100.0);

		return new Color(c.getRed() + dr, c.getGreen() + dg, c.getBlue() + db, c.getAlpha());
	}

	public static Color darken(Color c, int amount) {
		if (amount < 0 || amount > 100) return c;

		int r = (int)Math.round(c.getRed() * (100 - amount) / 100.0);
		int g = (int)Math.round(c.getGreen() * (100 - amount) / 100.0);
		int b = (int)Math.round(c.getBlue() * (100 - amount) / 100.0);

		return new Color(r, g, b, c.getAlpha());
	}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值