FWD:Befor drawing, set Stroke first. For example,float[] dash1 = {10.0f}; g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f));Then, i can draw a broken line.
java.awt Class BasicStroke java.lang.Object | +--java.awt.BasicStrokeAll Implemented Interfaces: Stroke setStroke public abstract void setStroke(Stroke s) Sets the Stroke for the Graphics2D context. Parameters: s - the Stroke object to be used to stroke a Shape during the rendering process See Also: BasicStroke, getStroke()
麻烦你写一段代码出来吧,我实在不知道如何使用,现在很急着用的,thx!
import java.awt.*; import java.applet.*; import javax.swing.*;public class wer extends JApplet { public void init() { //JButton jb = new JButton("sdaf"); //getContentPane().add(jb); }
一个画虚线的函数,s和r分别是实线部分和间隙的长度 public void drawDottedLine(Graphics g, int x1, int y1, int x2, int y2, int s, int r) { double d = Math.sqrt((double)(x1 - x2) * (x1 - x2) + (double)(y1 - y2) * (y1 - y2)); int x = x1, y = y1; int n = 0; boolean f = true; do { int dx = (int)((n * (s+r) + s - 1) * (x2 - x1) / d + x1 + 0.5); int dy = (int)((n * (s+r) + s - 1) * (y2 - y1) / d + y1 + 0.5); if (Math.abs(dx - x1) > Math.abs(x2 - x1)) { dx = x2; f = false; } if (Math.abs(dy - y1) > Math.abs(y2 - y1)) { dy = y2; f = false; } g.drawLine(x, y, dx, dy); n++; x = (int)(n * (s + r) * (x2 - x1) / d + x1 + 0.5); y = (int)(n * (s + r) * (y2 - y1) / d + y1 + 0.5); } while(f); }
g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f));Then, i can draw a broken line.
我不太明白你的意思,可以详细点吗?
g.setStroke 好像没有这样的方法吧
Class BasicStroke
java.lang.Object
|
+--java.awt.BasicStrokeAll Implemented Interfaces:
Stroke
setStroke
public abstract void setStroke(Stroke s)
Sets the Stroke for the Graphics2D context. Parameters:
s - the Stroke object to be used to stroke a Shape during the rendering process
See Also:
BasicStroke, getStroke()
import java.applet.*;
import javax.swing.*;public class wer extends JApplet
{ public void init()
{
//JButton jb = new JButton("sdaf");
//getContentPane().add(jb);
}
public void paint(Graphics g)
{
float[] dash1 = {10.0f}; Graphics2D gw = (Graphics2D)g;
//gw.setStroke(new BasicStroke(1.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
gw.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f));
gw.drawString("Hello World", 20, 20);
gw.drawLine(10,10,200,300);
}
}
你用到了Java2 的东西吧,你这个程序在JBuilder里是正确的,并能显示出来。
但在IE中就不行了,估计是因为Graphics2D是Java2 的扩充,但IE是不支持的。而为了兼容IE,做Applet通常是要使用早期的Java类库,请问有没有其它的方法,thanks!
public void drawDottedLine(Graphics g, int x1, int y1, int x2, int y2, int s, int r) {
double d = Math.sqrt((double)(x1 - x2) * (x1 - x2) + (double)(y1 - y2) * (y1 - y2));
int x = x1, y = y1;
int n = 0;
boolean f = true;
do {
int dx = (int)((n * (s+r) + s - 1) * (x2 - x1) / d + x1 + 0.5);
int dy = (int)((n * (s+r) + s - 1) * (y2 - y1) / d + y1 + 0.5);
if (Math.abs(dx - x1) > Math.abs(x2 - x1)) {
dx = x2;
f = false;
}
if (Math.abs(dy - y1) > Math.abs(y2 - y1)) {
dy = y2;
f = false;
}
g.drawLine(x, y, dx, dy);
n++;
x = (int)(n * (s + r) * (x2 - x1) / d + x1 + 0.5);
y = (int)(n * (s + r) * (y2 - y1) / d + y1 + 0.5);
} while(f);
}