Graphics 绘图
类声明:public abstract class Graphics extend Object
注:因为是从个人有道云笔记粘贴过来,图片无法显示。
Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制。
构造函数摘要
因为 Graphics 是一个抽象类,所以应用程序不能直接调用此构造方法。图形上下文从其他图形上下文获取,或者通过在组件上调用 getGraphics() 来创建。
如何获取 Graphics 对象
注:获取 Graphics 对象方式有不少,但是能的不多。
一、重写 paint(Graphics g) 方法
public class MyPanel extends JPanel {
@Override
protected void paint(Graphics g) {
super.paint(g);
Graphics myGraphics = g;
}
}
二、Component.getGraphics()
public class MyFram extend JFrame{
private Graphics graphics;
private JPanel panel;
public MyFram(){
setTitle("获取Graphics 对象");
setSize(300,300);
setLocation(nwe Point(300,200));
setVisible(true);
/**
TODO: 获取 Graphics 对象
因为 JPanel 继承了 Component 类所以可以调用 getGraphics() 方法。
*/
graphics = panel.getGraphics();
graphics.drawLine(3,5,200,300);
add(panel);
}
}
此时会出现 NullPointerException
原因: 因为 JPanel、JFrame等类没有实现 getGraphics() 方法。
所以想要使用 getGraphics() 来获取对象,使用前确认调用 getGraphics() 方法的对象有没有重写 getGraphics() 方法。此外,还有别的方法我就不在演示,自行查找。
方法摘要
1、画线、画点
// TODO: 方法声明
public abstarct void drawLine(int x1,int y1,int x2,int y2);
参数解释
x1、y1 第一个点的坐标;x2、y2 第二个点的坐标
示例
// TODO: 画一条线段
g.drawLine(3,3,50,50);
// TODO: 画一个点。两点重合既是画点。
g.drawLine(100,100,100,100);
2、画矩形
// 绘制指定矩形边框,即只是线条矩形
public void drawRect(int x, int y, int width, int height)
// 绘制填充矩形
public abstract void fillRect(int x, int y, int width, int height)
参数解释
x, y 为矩形左上角的坐标点,即位置。 width、height 矩形的宽高。
示例
// TODO: 绘制线条矩形
g.drawRect(80,100,40,25);
// TODO: 设置颜色
g.setColor(Color.yellow);
// TODO: 绘制填充
g.fillRect(20,70,20,30);
效果
3、画圆角矩形
// 绘制圆角线条矩形
public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
// 绘制圆角填充矩形
public abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
参数解释
x,y --> 矩形左上角位置坐标点
width,height --> 矩形宽高
arcWidth --> 4 个角弧度的水平直径
arcHeight --> 4 个角弧度的垂直直径
示例
// TODO: 绘制圆角线条矩形
g.drawRoundRect(10,10,150,70,40,25);
// TODO: 设置颜色
g.setColor(Color.blue);
// TODO: 绘制圆角填充矩形
g.fillRoundRect(80,100,100,100,60,40);
/**
TODO: 用绘制圆角矩形的方法绘制 圆形
当 width、height、arcWidth、arcHeight 相等,即为 圆。
*/
g.setColor(Color.red);
g.fillRoundRect(80,90,100,100,100,100);
效果
4、画 3-D 高亮显示矩形
// 绘制圆角线条矩形
public void draw3DRect(int x, int y, int width, int height, boolean raised)
// 绘制圆角填充矩形
public void fill3DRect(int x, int y, int width, int height, boolean raised)
参数解释
x,y --> 矩形左上角位置坐标点
width,height --> 矩形宽高
raised --> 一个用于确定矩形是凸出平面显示还是凹入平面显示的 boolean 值
示例
g.setColor(Color.red);
// TODO: 绘制三维线条矩形
g.draw3DRect(80,100,40,25,true);
g.setColor(Color.yellow);
// TODO: 绘制三维填充矩形
g.fill3DRect(20,70,20,30,false);
效果
5、画圆弧
// 绘制圆角线条矩形
public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
// 绘制圆角填充矩形
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
参数解释
x,y --> 矩形左上角位置坐标点
width,height --> 矩形宽高
startAngle --> 开始角度
arcAngle --> 相对于开始角度而言,弧跨越的角度。
示例
// TODO: 绘制圆弧线
g.drawArc(10,40,90,50,0,180);
g.setColor(Color.yellow);
// TODO: 绘制填充缺右上角的四分之三的椭圆
g.fillArc(10,100,40,40,0,-270);
效果
6、画椭圆
// 绘制线条椭圆
public abstract void drawOval(int x, int y, int width, int height)
// 绘制填充椭圆
public abstract void fillOval(int x, int y, int width, int height)
参数解释
x,y --> 矩形左上角位置坐标点
width,height --> 矩形宽高
示例
// TODO: 绘制线条椭圆
g.drawOval(50,50,130,80);
g.setColor(Color.blue);
// TODO: 绘制填充椭圆
g.fillOval(180,180,50,80);
效果
7、画折线图
// 绘制 折线图
public abstract void drawPolyline(int[] xPoints,int[] yPoints,int nPoints)
参数解释
xPoints - x 坐标数组。 yPoints - y 坐标数组。 nPoints - 点的总数。
示例
// TODO: 横坐标集
int x[] = {50,80,120,180};
// TODO: 纵坐标集
int y[] = {150,100,80,70};
// 绘制 折线图
g.drawPolyline(x,y,4);
效果
8、画 图片
// 绘制 指定路径图片
public abstract boolean drawImage(Image img,int x,int y, int width,int height,ImageObserver observer)
参数解释
img --> 要绘制的指定图像。 x - x 坐标。
y - y 坐标。
width - 矩形的宽度。
height - 矩形的高度。
observer - 转换了更多图像时要通知的对象。
示例
// TODO: 此路径为相对路径,并不是获取该系统文件夹下路径图片。
ImageIcon image = new ImageIcon(this.getClass().getResource("bigdata.jpg"));
// TODO: 根据 icon 获取 image
Image myImage = image.getImage();
// TODO: 绘制 图片
g.drawImage(myImage, 0, 0, this.getWidth(), this.getHeight(),this);
效果