How To Draw A Line Java
Draw a Line in Java
The Java.awt.Graphics form in Java forms the base for many such cartoon and graphics functions. Information technology is an incomprehensible class, as the actual cartoon activeness depends on the system and depends on the device. We will draw a line in Java in this tutorial.
We will start the plan by importing the necessary packages. We will import the java.applet.Applet, java.awt and coffee.awt.event package from the library.
The drawLine() method of the Graphics course is used to draw a line with the given color between two points.
Run across the code below.
import java.applet.Applet; import java.awt.*; import coffee.awt.event.*; public course DrawLine extends Applet { public static void main(String[] args) { Frame drawLineApplet = new Frame("Draw Line in Applet Window"); drawLineApplet.setSize(500, 450); Applet DrawLine = new DrawLine(); drawLineApplet.add(DrawLine); drawLineApplet.setVisible(truthful); drawLineApplet.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent east) {Arrangement.exit(0); } }); } public void paint(Graphics m) { g.setFont(new Font("Arial",Font.BOLD,12)); g.drawString("This is Draw Line Example", 100, 70); g.setColor(Color.blue); thou.drawLine(90, 135, xc, 180); yard.setColor(Color.light-green); one thousand.drawLine(lx, 4, 120, 120); } } 
In the above example, nosotros created ii lines and also displayed some text. Nosotros start declared a DrawLine class, which extends the Applet grade (Parent grade). Within the class, we alleged the master method. Hither the Frame drawLineApplet = new Frame() statement creates the applet window for the output.
The drawLineApplet.setSize() part is used to prepare the size of the applet window, and the drawLineApplet.setVisible(truthful) part is used to make the frame announced on the screen. We use the system.exit(0) command to exit the applet frame.
The pigment method hither is used to gear up the colour, font, and coordinates o the line to be fatigued. We alter the font using the setFont() role. The drawString() function here displays some text on the output frame. Nosotros change the colour of the start line using setColor() and and then the x and y coordinates of the line in the drawLine() role. Similarly, nosotros provide the coordinates and color for the second line.
Write for united states of america
DelftStack articles are written by software geeks like you. If you besides would like to contribute to DelftStack by writing paid articles, you can cheque the write for us page.
Related Article - Java GUI
Source: https://www.delftstack.com/howto/java/draw-line-in-java/
Posted by: packerangem1981.blogspot.com

0 Response to "How To Draw A Line Java"
Post a Comment