how to draw a line in java

Solutions on MaxInterview for how to draw a line in java by the best coders in the world

showing results for - "how to draw a line in java"
Henri
24 Mar 2016
1import javax.swing.*;
2import java.awt.*;
3
4class Scratch extends JComponent{
5    public static void main(String[] args) {
6        JFrame window = new JFrame();   // creates a JFrame
7        window.setTitle("Example");     //sets the title
8        window.setSize(600, 600);   //sets the size
9        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // sets the default close operation
10        window.setVisible(true);    //sets the JFrame visible
11        Scratch drawing = new Scratch();    //indirectly calls the paint method
12        window.add(drawing);    //adds the paint method onto the JFrame
13    }
14
15    public void paint(Graphics g){
16        Graphics2D g2 = (Graphics2D) g; //casting
17        g2.drawLine(100, 200, 300, 400);    //Draws a line from (100, -200) -> (300, -400) on the x and y plane
18        g2.draw(new Rectangle(10, 10, 10, 10));     //draw a rectangle
19        g2.setColor(Color.GREEN);   //Changes colour to Green
20        g2.fill(new Rectangle(100, 100, 100, 100)); //fills a rectangle
21    }
22}
queries leading to this page
how to add new line in javadrwa line java awtdraw line in javajava applet draw linedraw a line javadraw lines in javausing line in javag draw linejava gui to draw lines with coordinates using line2ddraw line java awtdrawline method java code line in javajava graphics draw up straight linedraw line using javahow to draw a line in konvadraw line with java guihow to draw a line in java with jcomponenteasy way draw line in javawhat is the code behind drawline in javahow to draw a line in java swingjava swing draw line examplejava draw a line in paneldraw line in java swingjava swing how to draw a linehow to use draw line javanew line in javajava gui to draw lines with coordinatesjava how to draw a lineg drawline parametersdrawline method javajava swing draw linedraw a line in gui javadraw 5 line in javajava draw a line on a guidrawline method in javajava swing drawlinejava how to make a line graphhow to draw lines in javahow to create a line graph in javajava awt draw linejava drawing linesgraphics g drawline javadraw star java applet using drawline functiondrawline in javahow to draw a line in java using guijava drawlinedraw line javajava draw a linehow to draw a line in javadraw a line on image javahow to draw a line in runtime in java processingjava console draw linesjava swing drawline parametersjava framework draw linedrawline javajava draw linehow to draw a line in htmlhow to draw line in java graphicsdraw a line in javahow to draw a line with jquery draw lines javahow to draw line in javajava draw line algorithmhow to draw a line in java