• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Using AWT to draw a triangle pattern

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
home / study / engineering / computer science / computer science questions and answers / in java, i need to create a program that draws a design made up of 1 - 5 rows of equilateral ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: In Java, I need to create a program that draws a design made up of 1 - 5 rows of equilateral tria...
In Java, I need to create a program that draws a design made up of 1 - 5 rows of equilateral triangles. The length of each side of a triangle is 100 pixels. Each row will contain one more triangle than the row above it. The triangles are contiguous and the lower left/right points of each triangle form the uppermost points of the triangles in the row below. The (x, y) coordinates of the topmost point of the triangle in the first row must be (300, 50). The user will create a custom color for the design by specifying the amount of red, green, and blue used to draw it. The user will also specify the number of rows of triangles. The image of what the output is supposed to look like is given here: https://stackoverflow.com/questions/50886251/draw-equilateral-triangles-using-a-nested-for-loop/50886556?noredirect=1#comment88774796_50886556

The program must be named Triangle and use the DrawingPanel class provided by the textbook to create a drawing panel (canvas) with a width of 600 pixels and an height of 600 pixels and then draw the triangle design. Prompt the user for a number of rows between 1 and 5 for the triangle design. Convert numbers below 1 to 1 and numbers above 5 to 5. Then prompt the user for the Red, Green, and Blue values used to create the custom color for the design. These values should be between 0 and 255 – convert numbers below 0 to 0 and numbers above 255 to 255 (I am not sure how to convert to these specific values?). I know that I need to import the java.util and java.awt packages to run my program. The console should look like this:

$ java Triangle

Number of Rows(1-5): 5
Red value (0-255): 255
Green value (0-255): 5
Blue value (0-255): 5

*CLOSE the Drawing Panel to exit the program*

An example of what the output of the program should look like is also attached.

In addition, if the user enters something other than an integer value (such as a double or a string), the program will throw an exception.

So far, I have tried to use Pythagorean theorem to figure out the coordinates of the points of the triangle.
My coordinates are such that for an equilateral triangle:

Top point of triangle: (x, y)

Coordinates of lower left point: (x - len/2, y + len * sqrt(3)/2)

Coordinates of lower right point: (x + len/2, y + len * sqrt(3)/2)

I have attached what an individual triangle is set up as well.

I want to use the the method drawTriangle in addition the to the main method, such that it looks like this:

//Draws an equilateral triangle with topmost point at (x,y) with the given side length and color
public static void drawTriangle(Graphics g, Color color, int x, int y, int sideLength) {

}

The main method should use a nested for loop to draw triangles (using an iterative approach, not a recursive approach). I am only to use java.awt and java.util. The code I have so far is



I am having trouble with the nested for loop in the main method. I feel like I have mastered the scanner part of the code in the main method, however. Also, I am not confident in the drawTriangle method that I have. I believe the proper header should be drawTriangle(Graphics g, Color color, int x, int y, int sideLength).
 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am only to use java.awt and java.util.  


Strange requirements.
Normal simple drawing programs use a panel to hold the drawing code.  They override that class's paint method and put all the drawing statements there.  The panel is added to a Frame for displaying.
The values to be used in the drawing are computed in some method and made available to the paint method.  When the values are ready, the panel's repaint method is called.  That will cause the paint method to be called where the values are used to make the drawing.

See this http://www.oracle.com/technetwork/java/painting-140037.html for hints on how to draw using awt.

 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sameer Kan wrote:. . . . I am only to use java.awt and java.util.  . . . .

Please explain why. Have you picked up a very out of date course?

And welcome to the Ranch

The procedure for drawing a triangle is really simple: you work out the coordinates of its three vertices. Then you draw lines, or some sort of polygon, between those vertices. Maybe this tutorial will be helpful; look at the last section.
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can limit the number of vertices-to-be-calculated.

For instance, if the number of rows is 2, then calculate the three midpoints of the edges of the big triangle. You can make three pairs of vertices from these three midpoints, and draw three lines between the points of the three pairs. For more than just one midpoint, you can use util.List to store the in-between points.

Indeed, you are then not drawing triangles, and not using a nested loop, but it simplfies enormously.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic