Color myColor = new Color(R,G,B);
为组件设置颜色
void setForegroud(Color) //设置前景色
void setBackground(Color) //设置背景色彩
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.Color;
public class b {
public static void main(String[] args) {
JFrame f1 = new JFrame();
Color c1 = new Color(0, 0, 255);
Color c2 = new Color(0, 0, 0);
JButton b1 = new JButton("this is button");
f1.add(b1, BorderLayout.CENTER);
// 设置按钮控件的前后景色
b1.setForeground(c1);
b1.setBackground(c2);
f1.setSize(100, 100);
f1.setVisible(true);
}
}
2. 字体Font
Font f1 = new Font(string"字体名称",字体风格,int字体大小);
风格:
Font.BOLD
Font.ITALIC
Font.PLAIN
Font.BOLD+Font.ITALIC
方法:
获取可用字体名列表
GraphicsEnvironment.getLoacalGraphicsEnvironment().getAvailableFontFamilyNames();
getAvailableFontFamilyNames()不是静态的所以需要实例化才能调用
java.awt.Component类
类中方法
paint(Graphics)// 绘图
repaint() // 重绘
paintComponent(Graphics) // 绘制组件,如果要在组件上绘制图形,只需要重写此方法
paintBorder(Graphics) // 绘制组件边框
paintChildren(Graphics) // 绘制组件中的子组件
Graphics类
方法:
沿着一个矩形画弧形,矩形的左上角为x,y,宽高width,height。
弧形起始位置:startAngle,画多长: arcAngle
drawArc(int x,int y,int width,int height,int startAngle,int arcAngle)
绘制一条直线,从(x1,y1)到(x2,y2)
drawLine(int x1,int y1,int x2,int y2);
绘制一个椭圆,左上角(x,y)矩形宽高(width,height)
drawOval(int x,int y,int width,int height)
绘制一系列的连线,注意图形是否闭合
drawPolygon(int [] xPoints,int [] y,int nPoints)
绘制矩形:左上坐标(x,y),宽高(width,height)
drawRect(int x,int y,int width,int height)
圆角矩形框:左上坐标(x,y),宽高(width,height)
drawRoudRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
绘制字符:在(x,y)绘制string,向右
drawString(String str,int x,int y)
setColor() // 设置画笔
上面的方法只绘制轮廓,下面的方法则是绘制实心图形
fillArc(int x,int y,int width, int height, int startAngle,int arcAngle)
fillOval(int x,int y,int width, int height)
fillPolygon(int [] xPoints,int [] yPoint,int nPoints)
fillRect(int x,int y,int width, int height)
fillRoundRect(int x,int y,int width, int height,int arcWidth,int arcHeight)
drawPolygon(int [] x,int [] y,int point)
如果图形不封闭等同于画折线
drawPolyline(nt [] x,int [] y,int point)
fillPolygon(int [] x,int [] y,int point)
填充的必须为封闭图形
构造
Polygon() // 创建空的多边形
Polygon(int [] x,int [] y,int point)// 创建多边形
addPoint(int x,int y) // 加入点
contains(int x,int y) // 该点是否处于多边形中?
getBounds() // 获取多边形的外界矩形
translate(int x,int y) // 将多边形移动,x轴偏移x,y轴偏移y



