用java2D,Graphics2D; BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
g2.setStroke(b);
g2.drawLine(20, y, w-20, y);
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
g2.setStroke(b);
g2.drawLine(20, y, w-20, y);
比如如下的drawNewLine()import java.awt.*;
import javax.swing.*;public class DrawLine extends JApplet
{
public void init(){}
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.drawLine(100,100,300,100);
drawNewLine(100,120,300,120,2,2,g);
drawNewLine(100,140,300,140,5,2,g);
drawNewLine(100,160,300,160,5,5,g);
drawNewLine(100,180,300,180,2,5,g);
drawNewLine(100,200,300,200,20,2,g);
}
//绘制虚线
public void drawNewLine(int x,int y,int ex,int ey,
int seeWidth,int emptyWidth,Graphics g)
{
g.drawLine(x,y,ex,ey);
g.setColor(Color.WHITE);
for(int dx=x+seeWidth;dx<ex;dx+=seeWidth+emptyWidth)
g.drawLine(dx,y,dx+emptyWidth,y);
g.setColor(Color.BLACK);
}
}你做个HTML把这个APPLET嵌入看看
当然我做的是只可以绘制水平方向的虚线,你也可以做个全功能的,然后一劳永逸
float TypeArray[] = {3,2,1}
stroke = new BasicStroke(1 , 1 , 1 , 1 , TypeArray , 1) ;
g2d.setStroke(stroke) ;
tyepeArray的意思是画一条虚线格式是 3, 2,1,3代表三个像素,2代表2个
按这个例子画出来的样子是
--- -- - --- -- - 。。
衷心地感谢你的帮助,循环调用drawLine函数的方法,在每次调用时都会产生时间消耗,我想毕竟不如Java自己的库函数只调用五次(1条实线和5条不同的虚线)效率高些.