Show a triangle in java
Java provides the Graphics class, which allows you to draw ellipses, squares, lines, display text and also has many other drawing methods. For any programmer, an understanding of the Graphics class is essential before getting into drawing in Java.
In order to be able to paint, a program needs a valid graphics context, represented by an instance of the Graphics class. But this class cannot be instantiated directly; so we must create a component and pass it to the program as an argument to the paint() method.
In addition, the Graphics class maintains a graphic context: a current drawing area, a Background and a Foreground drawing color, a Font with all its properties, etc. The axes are located in the upper left corner. The coordinates are always measured in pixels.
The paint method is executed every time the JFrame must be redrawn and arrives as parameter an object of the Graphics class. This object allows us to access the JFrame background and using the graphics primitives to draw lines, rectangles, ellipses, etc.
Drawing a triangle with java
Result:Drawing a circle using Shape and draw() in JavaThe draw() function is another function of the graphics2D class that takes a Shape as an argument. Shape is an interface that provides the properties of a geometric shape. In this case we want a circle, so we will use Ellipse2D.Double() which defines the height and width of the ellipse, as well as the x and y coordinates of the rectangle that frames it.In the following example, we pass the arguments to Ellipse2D.double(), and a created Shape object to the draw() method. This method will finally draw a circle in the window we created with JFrame.import javax.swing.*;
Draw rectangle java
The possibility of closing the application (EXIT_ON_CLOSE) by pressing the cross (x) in the upper right corner. In Linea01d.jav deploying “private void initComponents() {“, is a code that is generated by default.
b) It has an applet that gives a transparency effect so that the background of the screen or the application that we have open can be seen and also it is overprinted the result that we see in the picture that in fact is a clean background although it is observed a kind of box that is not explained how it is constructed and its utility.
In this application there are examples of how to draw lines, squares and how to round its vertices, other geometric figures (circle, oval, slope to find triangles, pentagons …), how to give thickness and color to its border, color its background and in awt mode and swing with NetBeans.
It is an Aplet. Curious example that leaves the transparent background, pending to see it with more detail in case of being a utility to use in some application. It also draws two very short thick lines and a text (word)
The area of a java triangle
1. Anna is flying a kite with a string that is 24 m long. At a given moment the kite is right over a pole that is 10 m from Ana. Knowing that Ana, at that moment, has the hand holding the kite 1, 5 m above the ground how high above the ground is the kite?
3. Antonio crosses from one corner of a rectangular park to another on a bike path that follows the diagonal of the rectangle. Knowing that the dimensions of the park are 300m x 400m, what distance does he travel? How many meters does he save by not going around the park?