How To Draw A Line In Java Gui
In this tutorial, we are going to encounter how to describe lines, rectangles and circles in JFrame. Java offers united states of america an easy way to draw graphics using Graphics class in AWT package which allows united states to draw primitive geometric types like lines, circles, etc… This tutorial explains the different functions of Graphics grade used to depict shapes.
Draw a line
Graphics class provides the Graphics.drawline(int x1, int y1, int x2, int y2) method to draw a line on the screen. While x1 is the x-coordinate of the kickoff point in the line and y1 is the y-coordinate of the showtime betoken in the line. Similarly, x2 and y2 are the coordinates of the second point in the line.
Here is the program that draws a line.
import java.awt.Graphics; import javax.swing.*; public course DrawMyLine extends JPanel { public void paint(Graphics thousand){ g.drawLine(20, 20, 200, 180); } public static void main(Cord[] args){ JFrame f = new JFrame("Depict a line"); f.getContentPane().add(new DrawMyLine()); f.setSize(250, 250); f.setVisible(true); f.setResizable(faux); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Output:
Draw a circle
You can depict a circle and oval using the Graphics.drawOval(int x, int y, int width, int summit) method. This function performs both functions. 'x' and 'y' are the starting point on the screen, and 'width' and 'height' are the parameters to set the width and elevation of the oval or circumvolve. For the circumvolve, set up the same width and acme.
Hither is the program that draws a circle on the screen.
import coffee.awt.Graphics; import javax.swing.*; public grade DrawMyCercle extends JPanel { public void pigment(Graphics g){ thousand.drawOval(50, twoscore, 150, 150); } public static void principal(Cord[] args){ JFrame f = new JFrame("Depict a circle"); f.getContentPane().add(new DrawMyCercle()); f.setSize(250, 250); f.setVisible(true); f.setResizable(simulated); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Output:
Depict a rectangle
Graphics class provides the Graphics.drawRect(int x, int y, int width, int meridian) method for drawing a rectangle or square. The starting time 2 parameters specify the starting point and the final two parameters specify the width and height of the rectangle or square. The width and height of the square must exist identical.
Here is the programme that draws a rectangle on the screen.
import java.awt.Graphics; import javax.swing.*; public class DrawMyRect extends JPanel { public void paint(Graphics one thousand){ yard.drawRect(fifty, 35, 150, 150); } public static void main(Cord[] args){ JFrame f = new JFrame("Depict a rectangle"); f.getContentPane().add(new DrawMyRect()); f.setSize(250, 250); f.setVisible(truthful); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Output:
[wbcr_php_snippet id="598″ title="GetPreviousPost" postid= "1796"] [wbcr_php_snippet id="125″ championship="GetPreviousPost" category_id= "5018"]
Source: https://stackhowto.com/how-to-draw-lines-rectangles-and-circles-in-jframe/
Posted by: rileywhemove.blogspot.com
0 Response to "How To Draw A Line In Java Gui"
Post a Comment