. Advertisement .
..3..
. Advertisement .
..4..
Advanced graphics processing is available in the programming interface of Java. You can draw simple forms like triangles by setting the shape’s parameters within a single frame. Want to draw triangles in Java? This guide will explain how you can do it utilizing three different ways.
Use Java.Swing And Java.Awt With Path2D In Java To Draw Triangles
You can utilize JFrame in the class main to make a main window, including components like text boxes and buttons added to make a graphical user interface (or GUI for short).
JPanel, a compact container utilized to group GUI components, is an extension of the Draw_A_Triangle
class. Utilizing the frame.add()
approach, the JPanel class is added to JFrames.
The class Path2D, which offers a straightforward yet adaptable form that represents any geometric path, is extended by the class Triangle_Shape
. You can employ the moveTo()
function to position the first point, then the LineTo()
function to position subsequent points. The triangles are correctly closed using the close()
function.
When a panel is formed, or you perform an action in the interface that necessitates redrawing, paintComponent()
is immediately called. With Graphics2D, you can have more flexibility in terms of text arrangement, geometry, and color management.
Lastly, create a triangle by calling draw while using g2d.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
public class Draw_A_Triangle extends JPanel {
public void paintComponent(Graphics g) {
Triangle_Shape triangleShape = new Triangle_Shape(new Point2D.Double(50, 0),
new Point2D.Double(100, 100), new Point2D.Double(0, 100));
Graphics2D g2d = (Graphics2D) g.create();
g2d.draw(triangleShape);
}
public static void main (String [] args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(“Draw Triangle”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.white);
frame.setSize(200, 200);
Draw_A_Triangle panel = new Draw_A_Triangle();
frame.add(panel);
frame.setVisible(true);
}
}
class Triangle_Shape extends Path2D.Double {
public Triangle_Shape(Point2D... points) {
moveTo(points[0].getX(), points[0].getY());
lineTo(points[1].getX(), points[1].getY());
lineTo(points[2].getX(), points[2].getY());
closePath();
}
}
Output:
......... ADVERTISEMENT .........
..8..
Use Drawpolygon, Java.Swing, And Java.Awt To Draw Triangles In Java
First, utilize JFrame to build the top-level container. Next, insert a panel to it, which is the JPanel-extension class DrawATriangle.
As you can see in the example below, the drawPolygon function within the paintComponent is called to make triangles on Graphics element g.
Next, utilize the approach drawPolygon(int x[], int y[], int numOfPoints)
to create the outlined polygon in accordance with the coordinates supplied in the y[]
and x[]
arrays. In this case, the point number indicated by the variable numOfPoints is 3.
import java.awt.*;
import javax.swing.*;
public class DrawATriangle extends JPanel {
public void paintComponent(Graphics g) {
int [] x = {50,100,0};
int [] y = {0,100,100};
g.drawPolygon(x, y, 3);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Draw a Polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.white);
frame.setSize(300, 200);
DrawATriangle panel = new DrawATriangle();
frame.add(panel);
frame.setVisible(true);
}
}
Output:
......... ADVERTISEMENT .........
..8..
Use Drawline() To Draw Triangles In Java
Try enhancing JComponent in DrawTriangle to utilize Swing components such as JFrame and paintCompnent()
. You can override paintComponent(Graphics g)
and the Graphics argument g so that we may use it to invoke various draw routines.
Next, use the method drawLine()
to draw lines. Do this three times to create a complete triangle. Keep in mind the drawLine()
method takes four inputs, which are the y and x coordinates of the line’s primary and secondary points.
To specify the window’s size, the getPreferredSize()
function is used with a return Dimension’s type. Then, in the method main, you construct an object JFrame and add the object from the DrawTriangle class to it in order to display the triangle.
Notice that you must call for the feature jFrame.setVisible(true)
since the frames are originally hidden.
import javax.swing.*;
import java.awt.*;
public class DrawTriangle extends JComponent {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(120, 130, 280, 130);
g.drawLine(120, 130, 200, 65);
g.drawLine(200, 65, 280, 130);
}
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.add(new DrawTriangle());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
}
Output:
......... ADVERTISEMENT .........
..8..
The Bottom Line
Above are the ways you can apply to draw triangles in Java. Keep practicing, and you will soon become the experts in drawing shapes in Java. Java is a fascinating programming language intended to have as minimal implementation requirements as feasible. If you want to gain more Java skills, you can start learning how to remove characters from strings or basic math functions.
Leave a comment