Color & Font - 使用颜色和字体

来源:http://blog.csdn.net/rexih/article/details/46453527

Color & Font都是java.awt.*;下的类

颜色:

1.Color类预定义的13种颜色

Color.BLACK 

Color.BLUE  

Color.CYAN 

Color.DARK_GRAY 

Color.GRAY 

Color.GREEN 

Color.LIGHT_GRAY 

Color.MAGENTA 

Color.ORANGE 

Color.PINK 

Color.RED 

Color.WHITE 

Color.YELLOW


2.通过构造函数创建颜色对象

Color(int r,intg,int b)//rgb是0-255的int值,e.g.

Color c=new Color(128,0,255);


3.使颜色对象变亮/暗

c.brighter();/c.darker();

效果不是特别明显,需要连续调用才能达到耀眼的效果:

c.brighter().brighter().brighter();

brighter方法对预定义的13种颜色效果不好,也许是因为到颜色的值是极值


设置颜色:

1.设置之后绘制的图形都使用的颜色 Graphics2D

setPaint(Color c)

2.填充封闭图形的颜色,用fill替代draw Graphics2D

fill(Shape s)

3.设置组件的背景色 Component

setBackground(Color c)

4.设置组件默认前景色 Component

setForeground(Color c)


字体

AWT定义的五个逻辑字体名

SansSerif //无衬线字体(黑体)
Serif //有衬线字体(宋体)
Monospaced //等宽字体
Dialog //对话框字体
DialogInput //对话框输入字体

字体风格:

Font.PLAIN

Font.BOLD

Font.ITALIC

Font.BOLD+Font.ITALIC


获取系统可用字体的字符串数组

1.获取描述用户系统图形环境的对象

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();

2.通过此对象获取系统所有可用字体家族名的字符串数组

String[] fontnames=ge.getAvailableFontFamilyNames();


创建字体对象

1.通过构造函数创建一个字体对象

Font myFont=new Font("SansSerif",Font.ITALIC,14);


2.载入一个TrueType字体或者Type 1字体,使用静态方法createFont()。

参数1是int类型指示字体类型是TrueType字体或者Type 1字体:Font.TRUETYPE_FONT/Font.TYPE1_FONT

参数2是字体载入源

static FontcreateFont(int fontFormat, File fontFile) 
          返回一个使用指定字体类型和指定字体文件的新 Font。
static Font  createFont(int fontFormat, InputStream fontStream) 
          返回一个使用指定字体类型和输入数据的新 Font。

通过createFont()载入的字体默认是PLAIN风格,字体大小为1

需要调整属性则在创建后使用derive方法,这是重载的方法,参数为float时表示大小,int时表示风格

myFont.derive(float size);

myFont.derive(int style);

myFont.derive(int style,float size)

=====

自定义字体的创建未完待续



使用字体:

设置后绘制的文本都使用此字体 Graphics2D:

setFont(Font f)

setFont()方法多次使用,不论代码顺序,显示的文本的字体都是以最后一次setFont设置的字体显示,即使drawString语句在最后一次setFont语句之前。


获取使用某种字体的文本的相关位置和大小属性:

1.获取屏幕设备字体属性的描述对象Graphics2D:(render是渲染的意思)

返回这个图形文本中,指定字体特征的字体绘制环境:通常在使用前此组件的Graphics2D对象已经使用了setFont(),所以此方法返回的是指定为setFont设置的字体的绘制环境

FontRenderContext context=g2d.getFontRenderContext();//FontRenderContext位于java.awt.font.*;

2.获取包围使用某种字体的文本的矩形对象Font:

Rectangle2D rectBounds=myFont.getStringBounds(msg,context);

3.获取使用某种字体的文本的宽高上坡度

double msgW=rectBounds.getWidth();

double msgH=rectBounds.getHeight();

double ascent=-rectBounds.getY();//获取到的包围文本的矩形的x,y坐标是相对的,x=0,y=-ascent。

4.获取使用某种字体的文本的下坡度和行距

LineMetrics是测定字符串宽度的线性metrics对象,除了能获取下坡度和行距,还可以获取上坡度和总高度,返回值都是float单精度

LineMetrics metrics=myFont.getLineMetrics(msg,context);

float descent=metrics.getDescent();

float leading=metrics.getLeading();

float ascent=metrics.getAscent();

float msgH=metrics.getHeight();


[java]  view plain  copy
  1. import java.awt.*;  
  2. import java.awt.font.*;  
  3. import java.awt.geom.*;  
  4. import javax.swing.*;  
  5.   
  6. public class ColorAndFont {  
  7.     public static void main(String[] args) {  
  8.         EventQueue.invokeLater(new Runnable(){  
  9.             public void run() {  
  10.                 CFFrame f=new CFFrame();                  
  11.             }             
  12.         });  
  13.     }  
  14. }  
  15. class CFFrame extends JFrame{     
  16.     CFFrame(){  
  17.         setLocationByPlatform(true);  
  18.         setTitle("color and font test");  
  19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  20.         getContentPane().add(new CFComponent());  
  21.         pack();       
  22.         setVisible(true);  
  23.     }     
  24. }  
  25.   
  26. class CFComponent extends JComponent{  
  27.     private static final int DEFAULT_W=800;  
  28.     private static final int DEFAULT_H=800;  
  29.     CFComponent(){  
  30.         setPreferredSize(new Dimension(DEFAULT_W,DEFAULT_H));  
  31.         setForeground(Color.CYAN);  
  32.     }  
  33.     public void paintComponent(Graphics g){  
  34.         Graphics2D g2d=(Graphics2D)g;  
  35.           
  36.         Line2D line=new Line2D.Double(0,0,100,100);  
  37.         g2d.draw(line);//使用了默认前景色CYAN  
  38.           
  39.         Color c2=new Color(128,0,255);//Color.RED;//brighter方法对预定义的13种颜色效果不好,也许是因为到颜色的值是极值  
  40.         Color c1=c2.darker();         
  41.         Color c3=c2.brighter();  
  42.           
  43.         Rectangle2D rect1=new Rectangle2D.Double(100,100,200,200);  
  44.           
  45.         Rectangle2D rect2=new Rectangle2D.Double(100,300,200,200);  
  46.           
  47.         Rectangle2D rect3=new Rectangle2D.Double(100,500,200,200);  
  48.           
  49.         g2d.setColor(c1);  
  50.         g2d.draw(rect1);  
  51.         g2d.fill(rect1);  
  52.           
  53.         g2d.setColor(c2);  
  54.         g2d.draw(rect2);  
  55.         g2d.fill(rect2);  
  56.           
  57.         g2d.setColor(c3);  
  58.         g2d.draw(rect3);  
  59.         g2d.fill(rect3);  
  60.         //============================  
  61.         g2d.setColor(Color.RED);  
  62.         g2d.drawString("qtyipdfghjklb"100100);//100,100是rect1的左上角的点,但是绘制结果字符串不在矩形内,而在矩形上,说明了字符串的纵坐标是按照基线来设置的。  
  63.           
  64.         Font myFont=new Font("SansSerif",Font.BOLD,14);  
  65.         setFont(myFont);  
  66.         g2d.drawString("中午\tnoon"200200);     
  67.         //repaint();  
  68.         setFont(new Font("Serif",Font.BOLD,24));//字体覆盖了之前的设置。  
  69.         g2d.drawString("中午\tnoon"200230);  
  70.         //============================  
  71.           
  72.         String msg="practise";  
  73.         myFont=new Font("SansSerif",Font.PLAIN,36);  
  74.         setFont(myFont);          
  75.         g2d.drawString(msg, 300300);  
  76.           
  77.         FontRenderContext context=g2d.getFontRenderContext();//屏幕设备字体属性对象  
  78.           
  79.         Rectangle2D msgBound=myFont.getStringBounds(msg, context);//得到的矩形不能直接用于绘图,其x,y是相对于基线的,x=0,y=-ascend  
  80.         g2d.draw(msgBound);  
  81.           
  82.           
  83.         double msgW=msgBound.getWidth();//文本宽度  
  84.         double msgH=msgBound.getHeight();//文本高度  
  85.         double ascent=-msgBound.getY();//文本上坡度  
  86.         LineMetrics metrics=myFont.getLineMetrics(msg, context);  
  87.         float descent=metrics.getDescent();//文本下坡度  
  88.         float leading=metrics.getLeading();//文本行距  
  89.           
  90.           
  91.         g2d.setPaint(Color.BLUE);  
  92.         Rectangle2D msgRect=new Rectangle2D.Double(300,300-ascent,msgW,msgH);//包围文本的矩形  
  93.         g2d.draw(msgRect);  
  94.           
  95.         g2d.setPaint(Color.MAGENTA);  
  96.         g2d.draw(new Line2D.Double(300,300,300+msgW,300));//文本的基线  
  97.           
  98.         g2d.setPaint(Color.ORANGE);  
  99.         g2d.draw(new Line2D.Double(300,300+descent,300+msgW,300+descent));//文本下坡度的下沿  
  100.           
  101.         g2d.setPaint(Color.GREEN);  
  102.         g2d.draw(new Line2D.Double(300,300+descent+leading,300+msgW,300+descent+leading));//文本行距的下沿  
  103.     }     
  104. }  

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值