Buttonの背景色を変更

3045 ワード

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());
	}