📄 exam11_6.java
字号:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Exam11_6 extends JFrame
{
public Exam11_6()
{
super("演示画笔应用示例");
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) //重写绘图方法paint()
{
Graphics2D g2d = (Graphics2D)g; // 强制转换为Graphics2D引用
int xp[]={50,10,90,50}; //定义x坐标点
int yp[]={30,70,70,30}; //定义y坐标点
GeneralPath path1=new GeneralPath(); // 构建GeneralPath类对象path1
path1.moveTo(xp[0],yp[0]); // 将起始点加入路径
for(int i=1; i<xp.length;i++) path1.lineTo(xp[i],yp[i]);//坐标点加入路径
GeneralPath path2=new GeneralPath(); // 构建GeneralPath类对象path2
path2.moveTo(xp[0]+100,yp[0]); // 将起始点加入路径
for(int i=1; i<xp.length;i++) path2.lineTo(xp[i]+100,yp[i]);//坐标点加入路径
BasicStroke wideLine=new BasicStroke(4,1,2);//线宽为4,末端圆形,两线交汇不修饰
g2d.setStroke(wideLine);//设置画笔
g2d.draw(path1); //绘制第1个三角形
float dash[]={8,4,2}; //定义虚线样式
BasicStroke dashLine=new BasicStroke(1,1,0,10,dash,0);//定义画笔
g2d.setStroke(dashLine); //设置画笔
g2d.draw(path2); //绘制第2个三角形
} //绘图方法结束
public static void main(String args[]) //主方法main()
{
new Exam11_6();
} //主方法main()结束
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -