How to Draw a Triangle in Java

Java's Graphics2D class includes methods for drawing several primitive geometric shapes, such as ovals and rectangles. The class lacks a method for drawing a triangle, but you can define a triangle using the GeneralPath class. You can then use Graphics2D to draw the GeneralPath that defines the triangle. Before you can draw a triangle, you must initialize a frame and add a component with an overridden paint method on which to render your triangle.

Instructions

  1. Create a Drawing Surface

    • 1

      Create a new project in your preferred integrated development environment, or IDE.

    • 2

      Create a new class in your project and name it DrawingComponent. Open the new class and add "extends Component" (without quotes) immediately after "DrawingComponent" but before the class's opening bracket in the class declaration.

    • 3

      Import the necessary files into your project by entering the following lines above the DrawingComponent class's declaration:

      import java.awt.Component;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.geom.GeneralPath;

    • 4

      Override the component's paint method by adding the following code between the DrawingComponent's opening and closing brackets:

      public void paint(Graphics g) {

      }

    • 5

      Add the following code to your project's main method to create a new frame and add your custom drawing component to the frame:

      import javax.swing.JFrame frame = new import javax.swing.JFrame();
      int frameWidth = 300;
      int frameHeight = 300;
      frame.setSize(frameWidth, frameHeight);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(import javax.swing.JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new DrawingComponent());

    Drawing a Triangle on the Component

    • 6

      Create a new Graphics2D object inside the DrawingComponent's paint method with the following code:

      Graphics2D g2d = (Graphics2D) g;

    • 7

      Create a GeneralPath object with three sides with this code:

      GeneralPath triangle = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 3);

    • 8

      Direct the general path to the first coordinate of the triangle. Replace the numbers in the following code with the x and y coordinates of the first point of your triangle:

      triangle.moveTo(50, 25]);

    • 9

      Create a line from the first point to the second point and from the second point to the third. Replace the numbers in this code with the coordinates of the second and third points of your triangle:

      triangle.lineTo(75,50);
      triangle.lineTo(25,50);

    • 10

      Create the final line from the third point of the triangle back to the first point with this code:

      triangle.closePath();

    • 11

      Pass the triangle to Graphics2D's draw object to draw the object on your JFrame with the following code:

      g2d.draw(triangle);

Related Searches:

References

Comments

Related Ads

Featured