package ch01.section7.ex;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class LineImage extends JFrame{
public LineImage(){
super("实线与虚线"); //调用父类构造函数
setSize(300,200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
Dimension dim=this.getSize();//获得当前窗口的大小
g2d.setColor(Color.white);
//勾勒出画图区域的大小
g2d.fillRect(0, 0, dim.width,dim.height);
g2d.setColor(Color.black);
//划线(都是实线)
g2d.drawLine(40,160,280,160);
g2d.drawLine(40, 40, 40, 160);
g2d.drawString("0", 30, 165);
g2d.drawString("100", 15, 50);
g2d.drawString("200", 270, 175);
//定义虚线
float []f={10f,10f};
//java.awt.BasicStroke用来定义图形轮廓的
BasicStroke bs=new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,10.0F,f,0f);
g2d.setStroke(bs);
g2d.drawLine(40, 160, 60,120 );
g2d.drawLine(60, 120, 170,60 );
g2d.drawLine(170, 60, 270,40 );
}
public static void main(String[] args){
new LineImage();
}
}