How to draw a triangle

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-17-2012, 03:43 PM
Lexaras
How to draw a triangle
Hey guys I need to draw a triangle. I know how to draw Rectungle and Circle and I know what to write in brackects. But I have problem with triangle. Ive posted two programs from my package shapes and I hope its enough to fix this error :)

Code:

```package shapes; import javax.swing.*; import java.awt.*; public class ShapeComponent extends JComponent {     int w = 640, h = 320;  // set size of component     Circle[] ca;        // array of circles to paint     Square[] sa;        // array of squares to paint     EQTriangle[] za;    //aray of triangles to paint     public ShapeComponent(Circle[] ca, Square[] sa, EQTriangle[] za) {         this.ca = ca;         this.sa = sa;         this.za = za;     }     public void paintComponent(Graphics g) {         g.setColor(Color.green);         g.fillRect(0, 0, getWidth(), getHeight());         for (Circle c : ca) {             g.fillOval(c.x - c.r, c.y - c.r, c.r * 2, c.r * 2);         }         g.setColor(Color.white);         for (Square s : sa) {             g.fillRect(s.x - s.s / 2, s.y - s.s / 2, s.s * 2, s.s * 2);         }         g.setColor(Color.blue);         for (EQTriangle z : za) {             g.fillTriangle(z.x - z.b / 2, z.y - z.b / 2, z.b * 2, z.b * 2);                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Error is in brackets         }     }     public Dimension getPreferredSize() {         return new Dimension(w, h);     } } Triangle draw class. package shapes; public class EQTriangle {     // need one of these per Circle     int x,y;     int b;     // but only one PI, keep it static     // would use Math.PI in real code     static double HALF = 0.5;     public EQTriangle(int x, int y, int b) {         this.x = x;         this.y = y;         this.b = b;     }     public double area() {         return b * b * HALF;     }     public String toString() {         return "Triangle of area: " + area();     } }```
• 02-17-2012, 03:57 PM
Tolls
Re: How to draw a triangle
Please put your code in code tags when posting here so it retains formatting.

ALso, when you have an error or exception post the full error message here.

Graphics does not have a drawTriangle method, does it?
API.
• 02-17-2012, 06:03 PM
Lexaras
Re: How to draw a triangle
I quess not :S Maybe u can tell me what has this method then?
• 02-17-2012, 06:13 PM
Norm
Re: How to draw a triangle
You could use the Graphics class's drawLine method and call it three times, once for each side.
• 02-17-2012, 06:24 PM
Lexaras
Re: How to draw a triangle
Mby now it will be easier to understand all my code. From class ComponentTest I got 3 numbers(parameters). 2 of them give me place where the figure should stand and last one number is length. Using this number I should find plot and create figuere which is equal to that plot.
• 02-17-2012, 06:43 PM
Norm
Re: How to draw a triangle
Can you make a simple program that compiles, executes and shows your problem?
You posted code that is not related to the problem. Create a program with the minimum classes, leave out what is not part of the problem.

Also this package will be a problem:
import utilities.JEasyFrame;
• 02-17-2012, 06:57 PM
Lexaras
Re: How to draw a triangle
Code:

```package utilities; import javax.swing.*; import java.awt.*; public class JEasyFrame extends JFrame {     public Component comp;     public JEasyFrame(Component comp, String title) {         super(title);         this.comp = comp;         getContentPane().add(BorderLayout.CENTER, comp);         pack();         this.setVisible(true);         setDefaultCloseOperation(EXIT_ON_CLOSE);         repaint();     } }```
Sorry, here it is.
• 02-17-2012, 07:00 PM
Norm
Re: How to draw a triangle
Can you make a small simple program that compiles and executes and shows the problem?

Take the 3 values, create a polygon and use the fillPolygon method
• 02-17-2012, 07:09 PM
Lexaras
Re: How to draw a triangle
Now u can see how all my code works. The problem is in this part
Code:

```package shapes;   import javax.swing.*; import java.awt.*;   public class ShapeComponent extends JComponent {     int w = 640, h = 320;  // set size of component     Circle[] ca;        // array of circles to paint     Square[] sa;        // array of squares to paint     EQTriangle[] za;    //aray of triangles to paint       public ShapeComponent(Circle[] ca, Square[] sa, EQTriangle[] za) {         this.ca = ca;         this.sa = sa;         this.za = za;     }       public void paintComponent(Graphics g) {         g.setColor(Color.green);         g.fillRect(0, 0, getWidth(), getHeight());         for (Circle c : ca) {             g.fillOval(c.x - c.r, c.y - c.r, c.r * 2, c.r * 2);         }         g.setColor(Color.white);         for (Square s : sa) {             g.fillRect(s.x - s.s / 2, s.y - s.s / 2, s.s * 2, s.s * 2);         }         g.setColor(Color.blue);         for (EQTriangle z : za) {             g.fillTriangle(z.x - z.b / 2, z.y - z.b / 2, z.b * 2, z.b * 2);    <------- Error         }       }       public Dimension getPreferredSize() {         return new Dimension(w, h);     } }```
Error: cannot find symbol method fillTriangle(int,int,int,int). I though Ive made mistakes in brackets, mby it should count in different way, unless guy in this forum said that graphics doesnt have this method fillTriangle. Know I would like to get information how I should draw it with three parameters ShapeComponentTest. As i said before first two numbers are coordinates and the last one is lenght...Triangle plot= lenght*length*1/2. But I dont know how to draw it:S
• 02-17-2012, 07:17 PM
Norm
Re: How to draw a triangle
Quote:

graphics doesnt have this method fillTriangle
This can be answered easily by looking at the API doc for the Graphics class.
Quote:

how I should draw it
One way would be to create a polygon and use the fillPolygon() method.

You will have to figure out: How to take three numbers and convert them into 3 x,y points.
This kind of problem is often solved by taking a piece of graph paper and doing some drawing.

For easier testing, write a simple program that takes the 3 numbers, creates a polygon and fills it.
Leave out all the rest of the code that is working and is not part of the problem. When you get the solution, you can replace this code:
Code:

```for (EQTriangle z : za) {             g.fillTriangle(z.x - z.b / 2, z.y - z.b / 2, z.b * 2, z.b * 2```
with the technique you work out in the simple testing program
• 02-17-2012, 07:42 PM
Lexaras
Re: How to draw a triangle
Ive tried with polygon in this method:
Quote:

for (EQTriangle z : za) {
Point p1 = new Point(z.y/3 , (2 * z.b) / 3);
Point p2 = new Point(z.y/2 , z.b / 3);
Point p3 = new Point(( 2*z.y) / 3, (2 * z.b) / 3);

int[] xs = { p1.x, p2.x, p3.x };
int[] ys = { p1.y, p2.y, p3.y };
Polygon triangle = new Polygon(xs, ys, xs.length);

g.fillPolygon(triangle);
I can create triangle, but I have some mistakes with coordinates, which im trying to solve:) But I have one more q...
E.X: import java.awt.*; with this u can write colors directly and my assignment teacher will check with these values my code :
new Circle(250, 150, 100, red)
new EQTriangle(250, 150, 80, black)
new Square(200, 280, 20, white) instead of this...
Quote:

Circle[] ca = new Circle[]{
new Circle(250, 150, 100),
new Circle(250, 150, 70),
So I though mby I should create one more parameter in class'es Triange,Square and Circle (String Color) and it will works, but it didnt help:/
• 02-17-2012, 07:50 PM
Norm
Re: How to draw a triangle
Sorry, I don't see what your question is.

Color is a class. It has some static members that you can reference: Color.WHITE for example.
Read the API doc for the list of colors that are defined in the class.
• 02-17-2012, 07:55 PM
Lexaras
Re: How to draw a triangle
Exactly, now colors are static. All figures are the same colors due to these sentences in code:
g.setColor(Color.green);
g.setColor(Color.white);

I would like to get figures in different colours. I think I wanna make string array and colour should be like 4th parameter. When array goes then it checks not only 3 ints(coordinates and length) but also and color. So I could create two circles in different colors. I hope u understand now. English is not my native language so I've problems with explanation :) Btw, thank u for your patient.

I would like to give these parameters.
new Circle(250, 150, 100, red)
new Circle(250, 150, 80, black)
• 02-17-2012, 08:01 PM
Norm
Re: How to draw a triangle
Quote:

I would like to get figures in different colours.
Many ways to set the colors. One problem is who decides? Can the colors be random? Can they be chosen from a list of colors one at at time starting with the first and going through the list to the end and then wrapping around to the first again?
What code should decide what color each drawn shape should be?
• 02-17-2012, 08:05 PM
Lexaras
Re: How to draw a triangle
new Circle(250, 150, 100, red)
new Circle(250, 150, 70, yellow)
new EQTriangle(250, 150, 80, black)
new Square(200, 280, 20, white)
new Square(250, 280, 20, blue)
new Square(300, 280, 20, white)

These lines will decide each drawn colour.
• 02-17-2012, 08:09 PM
Norm
Re: How to draw a triangle
How have you defined the variables: red, yellow, etc?
Why not use a Color object: Color.RED?
• 02-17-2012, 08:12 PM
Lexaras
Re: How to draw a triangle
import static java.awt.Color.* this lets to use red,black without Color.Red... Main proble is how to bring that color type to ShapeComponent class.
• 02-17-2012, 08:19 PM
Norm
Re: How to draw a triangle
Quote:

this lets to use red,black without Color.Red.
Very poor technique. It makes the code hard to understand. Are you really that lazy?
• 02-17-2012, 08:25 PM
Lexaras
Re: How to draw a triangle
;DDD It was not my choice:) My teacher included note, that we can use this technique and I tried:) Btw I cant see the huge difference between these techniques, but the problem still exsists because ShapeComponent tooks only int parameters and Color.RED doesnt mean anything to it.
• 02-17-2012, 08:29 PM
Norm
Re: How to draw a triangle
Quote:

ShapeComponent tooks only int parameters
Then something needs to be rewritten. How much can you change?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last