Java : how to draw a line in thickness

来源:互联网 发布:淘宝怎么看同行转化率 编辑:程序博客网 时间:2024/05/16 02:01

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);

       
        }
}

原创粉丝点击