图形用户界面(四)——图形处理

设置字体

设置字体
java.awt.Font类

设置文本的字体(包括字型和字号)

构造方法
 public Font(String name, int style int size)

例:

 Font f = new Font(“Dialog”, Font.PLAIN, 14); 

1)字体名称: Dialog、DialogInput、Monospaced、Serif、SansSerif

2)字体的风格:Font.BOLD、Font.ITALIC、Font.PLAIN、Font.BOLD+Font.ITALIC

3)字体的大小

setFont(Font f)方法对组件中文本的字体进行设定

设置颜色

java.awt.Color类控制颜色,Color类已包含13个颜色常量

构造方法:
 public Color(int r, int g, int b)
 public Color(float r1, float g1, float b1)

例:

 int r = 255, g = 255, b = 0;
 Color myColor = new Color(r, g, b) 
1)颜色

设置组件的前景色和背景色

 public void setForeground(Color c)——设置前景色
 public void setBackground(Color c)——设置背景色 
2)绘图
  • 绘制图形调用 java.awt.Component类中定义paint(Graphics g)方法
  • 每当需要重绘组件时,调用repaint()方法,该方法将自动调用paint(Graphics g)
  • javax.swing.JComponent继承java.awt.Component
  • paintComponent(Graphics g)
  • 在改写paintComponent(Graphics g)方法时,需要先调用基类的paintComponent(Graphics g)方法
3) Graphics类的方法

圆弧:

 drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) 
 fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 

直线

 drawLine(int x1, int y1, int x2, int y2) 

椭圆

 drawOval(int x, int y, int width, int height) 
 fillOval(int x, int y, int width, int height)

多边形

 drawPolygon(int[] xPoints, int[] yPoints, int nPoints) 
 fillPolygon(int[] xPoints, int[] yPoints, int nPoints)

矩形

 drawRect(int x, int y, int width, int height)
 fillRect(int x, int y, int width, int height)

圆角矩形

 drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 
 fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

字符串

 drawString(String str, int x, int y)

学习完了,是不是so easy!让我们一起练习巩固一下吧。戳这里

你可能感兴趣的:(组件技术,绘制图形)