⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jstitlebarline.java

📁 Java自定义窗体JsFrame。简介见:http://jason0086.spaces.live.com/Blog/cns!A797D0C5C0C13C92!518.entry
💻 JAVA
字号:
package com.hfkj.jsframe.titlebar;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;

import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;

/**
 * The <code>JsTitleBarLine<code> draws a line with the gradually changing colors.
 * The color of the title line would change gradually from outside color at the start
 * to inside color at the midpoint, then to outside color at the three-quarter point,
 * and then to inside color at the end.
 *  
 * @version 1.0 01/05/09
 * @author Jason (MSN:www.jason0086.com@hotmail.com)
 */
public class JsTitleBarLine extends JComponent {
	
	private static final long serialVersionUID = -3533220014817407249L;

	/**
	 * The outside color.
	 */
	private Color outsideClr = null;

	/**
	 * The inside color
	 */
	private Color insideClr = null;
	
	/**
	 * Constructs a js title bar line.
	 */
	@SuppressWarnings("static-access")
	public JsTitleBarLine() {
		MetalLookAndFeel lookMlaf = (MetalLookAndFeel) UIManager.getLookAndFeel();
		this.outsideClr = lookMlaf.getFocusColor();
		this.insideClr = lookMlaf.getMenuBackground();
	}
	
	/**
	 * Constructs a js title bar line with the gradually changing colors specified by
	 * outsideColor and insideColor.
	 * The color of the title line would change gradually from outsideColor at the start 
	 *  to insideColor at the midpoint, then to outsideColor at the three-quarter point, 
	 *  and then to insideColor at the end.
	 * @param outsideColor the outside color for this title line
	 * @param insideColor the inside color for this title line
	 */
	public JsTitleBarLine(Color outsideColor, Color insideColor) {
		this.outsideClr = outsideColor;
		this.insideClr = insideColor;
	}
	
	/**
	 * Change the icon and string associated with the look and feel.
	 */
	@SuppressWarnings("static-access")
	public void changeLookAndFeel() {
		MetalLookAndFeel lookMlaf = (MetalLookAndFeel) UIManager.getLookAndFeel();
		this.outsideClr = lookMlaf.getFocusColor();
		this.insideClr = lookMlaf.getMenuBackground();
	}
	
	/**
	 * Sets the outside color for this title line.
	 * @param coolor the new outside color
	 */
	public void setOutsideColor(Color coolor) {
		this.outsideClr = coolor;
	}
	
	/**
	 * Returns the outside color of this title line.
	 * @return the outside color
	 */
	public Color getOutsideColor() {
		return this.outsideClr;
	}
	
	/**
	 * Sets the inside color for this title line.
	 * @param color the new inside color
	 */
	public void setInsideColor(Color color) {
		this.insideClr = color;
	}
	
	/**
	 * Returns the inside color of this title line.
	 * @return the inside color
	 */
	public Color getInsideColor() {
		return this.insideClr;
	}
	
	@Override
	public void paint(Graphics xGrp) {
		Graphics2D xG2d = (Graphics2D) xGrp;
		GradientPaint xGp = null;
		Area xArea = null;
		Rectangle xRct = null;
		int xNorthwestInt = 0;
		int yNorthwestInt = 0;
		int xSoutheastInt = 0;
		int ySoutheastInt = 0;
		int quarterInt = this.getWidth() >> 2;
		int twoQuarterInt = this.getWidth() >> 1;
		// draw the line which starts at 0 and ends at two-quarter width of the JComponent.
		yNorthwestInt = this.getHeight() - this.getHeight();
		xSoutheastInt = xNorthwestInt + twoQuarterInt;
		ySoutheastInt = this.getHeight();
		xRct = new Rectangle(xNorthwestInt, 
								yNorthwestInt, 
								twoQuarterInt, 
								this.getHeight());
		xArea = new Area(xRct);
		xGp = new GradientPaint(xNorthwestInt, 
								yNorthwestInt, 
								this.outsideClr, 
								xSoutheastInt, 
								ySoutheastInt, 
								this.insideClr);
		xG2d.setPaint(xGp);
		xG2d.fill(xArea);
		// draw the line which starts at two-quarter width of the JComponent and ends at three-quarter width of that.
		xNorthwestInt = xSoutheastInt;
		xSoutheastInt = this.getWidth() - quarterInt;
		xRct = new Rectangle(xNorthwestInt, 
								yNorthwestInt, 
								quarterInt, 
								this.getHeight());
		xArea.reset();
		xArea.add(new Area(xRct));
		xGp = new GradientPaint(xNorthwestInt, 
								yNorthwestInt, 
								this.insideClr, 
								xSoutheastInt, 
								ySoutheastInt, 
								this.outsideClr);
		xG2d.setPaint(xGp);
		xG2d.fill(xArea);
		// draw the line which starts at three-quarter width of the JComponent and ends at full width of that.
		xNorthwestInt = xSoutheastInt;
		xSoutheastInt = this.getWidth();
		xRct = new Rectangle(xNorthwestInt, 
								yNorthwestInt, 
								quarterInt, 
								this.getHeight());
		xArea.reset();
		xArea.add(new Area(xRct));
		xGp = new GradientPaint(xNorthwestInt, 
								yNorthwestInt, 
								this.outsideClr, 
								xSoutheastInt, 
								ySoutheastInt, 
								this.insideClr);
		xG2d.setPaint(xGp);
		xG2d.fill(xArea);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -