画线????

yuercf 2001-12-13 03:56:42
在Applet中画线,如何确定线的颜色?线的粗线程度?
...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2001-12-13
  • 打赏
  • 举报
回复
Draw a line with a thickness
import java.awt.*;
import java.applet.*;

public class thickLine extends Applet {

public void init( ) { }

public void paint( Graphics g ) {
drawThickLine(g, 0, 0, getSize().width, getSize().height, 5, new Color(0).black);
drawThickLine(g, 0, getSize().height, getSize().width, 0, 5, new Color(0).red);

drawThickLine
(g, getSize().width/2, 0, getSize().width/2, getSize().height, 8, new Color(0).green);
drawThickLine
(g, 0, getSize().height/2, getSize().width, getSize().height/2, 12, new Color(0).blue);
}

public void drawThickLine(
Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {
// The thick line is in fact a filled polygon
g.setColor(c);
int dX = x2 - x1;
int dY = y2 - y1;
// line length
double lineLength = Math.sqrt(dX * dX + dY * dY);

double scale = (double)(thickness) / (2 * lineLength);

// The x and y increments from an endpoint needed to create a rectangle...
double ddx = -scale * (double)dY;
double ddy = scale * (double)dX;
ddx += (ddx > 0) ? 0.5 : -0.5;
ddy += (ddy > 0) ? 0.5 : -0.5;
int dx = (int)ddx;
int dy = (int)ddy;

// Now we can compute the corner points...
int xPoints[] = new int[4];
int yPoints[] = new int[4];

xPoints[0] = x1 + dx; yPoints[0] = y1 + dy;
xPoints[1] = x1 - dx; yPoints[1] = y1 - dy;
xPoints[2] = x2 - dx; yPoints[2] = y2 - dy;
xPoints[3] = x2 + dx; yPoints[3] = y2 + dy;

g.fillPolygon(xPoints, yPoints, 4);
}
}



Using JDK1.2 public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int width = 10;
g2d.setStroke(new BasicStroke(width));
g2d.drawLine(x1, y1, x2, y2);
}



On a Component int width = 10;
BasicStroke bs = new BasicStroke(width);
JLabel l = new JLabel();
l.getGraphics().setStroke(bs);
l.drawLine(0,0,100,100);




Using JDK1.3 import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class TestLine extends JFrame{
private MyPanel panel;
public TestLine() {
setSize(200, 200);
panel = new MyPanel();
getContentPane().add( panel, "Center" );
}

public static void main( String [] args ){
TestLine tl = new TestLine();
tl.setVisible( true );
}
}

class MyPanel extends JPanel {
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);

public MyPanel(){}

public void paintComponent( Graphics g ){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke( stroke );
g2.draw(new Line2D.Double(10.0, 10.0, 100.0, 10.0));
g2.setStroke( wideStroke );
g2.draw(new Line2D.Double(10.0, 50.0, 100.0, 50.0));
}
}

rinehart 2001-12-13
  • 打赏
  • 举报
回复
java.awt.Graphics
仔细看看
public abstract void setColor(Color c)

粗细比较麻烦!
一条线应当是1个像素宽吧!
你多画几条,或者干脆画矩形!
yuercf 2001-12-13
  • 打赏
  • 举报
回复
还有画实心点,如何给实心点加颜色,例如画一个红色的实心点?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧