How to Display Shapes in Java

Shapes in Java are drawn on an x and y axis. You use the points on the x and y axis to draw simple shapes in your Java applications. For instance, if you want to draw a straight line, you define two x and y points, and the Java application automatically draws the line from point one to point two. This feature is a convenient way to dynamically add shapes to your application.

Instructions

    • 1

      Open the Java Eclipse software on your desktop and open the Java project you want to use to draw the shape.

    • 2

      Double-click the Java source code file you want to use to draw the shape. Create the variables with the values you need to draw the shape. For instance, to draw a straight line, you need four points: two for the first point and two for the second point. The following code creates the four variables for a straight line:

      int x1= 0;
      int y1 = 0;
      int x2= 5;
      int y2 = 5;

      The points above set up the line variables to draw a link from the 0, 0 mark on the x and y axis to the 5, 5 mark on the same axis.

    • 3

      Draw the shape on your Java form. After the shape variables are set up, you use the internal Java classes to draw the shape on the form. The following code draws the line on the form:

      Shape drawline = new Line2D.Float(x1, y1, x2, y2);

Related Searches:

References

Comments

Related Ads

Featured