package EXP;
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 ){
//Using JDK1.3
Graphics2D g2 = (Graphics2D)g;
//g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //set the border
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));
//Using JDK1.2
Graphics2D g2d = (Graphics2D)g;
int width = 10;
g.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(width));
g2d.drawLine(10, 70, 100, 70);
}
}
Java : how to draw a line in thickness
最新推荐文章于 2026-06-25 20:11:42 发布

1189

被折叠的 条评论
为什么被折叠?



