Friday, December 21, 2012

To Draw a Thick Line using JAVA GUI

To draw a Thick line using JAVA GUI :
Check following example:

Java Code:

import java.awt.*;
import javax.swing.*;

public class StrokeExample extends JPanel implements Runnable {

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();
        g.drawLine(0,0,w,h);    //default
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(3));
        g2.drawLine(0,h,w,0);   //thick

    }

    public void run() {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new StrokeExample());
        f.setSize(500,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

    }

No comments:

Post a Comment