Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: How to draw a triangle

  1. #1
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default 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 :)


    Java 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();
        }
    }
    Last edited by Norm; 02-17-2012 at 06:13 PM. Reason: added code tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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.

  3. #3
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: How to draw a triangle

    I quess not :S Maybe u can tell me what has this method then?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: How to draw a triangle

    You could use the Graphics class's drawLine method and call it three times, once for each side.
    Or how about a polygon?

  5. #5
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default 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.
    Last edited by Lexaras; 02-17-2012 at 08:45 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default 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;
    Last edited by Norm; 02-17-2012 at 06:47 PM.

  7. #7
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: How to draw a triangle

    XML 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default 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
    Last edited by Norm; 02-17-2012 at 07:04 PM.

  9. #9
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: How to draw a triangle

    Now u can see how all my code works. The problem is in this part
    XML 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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: How to draw a triangle

    graphics doesnt have this method fillTriangle
    This can be answered easily by looking at the API doc for the Graphics class.
    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:
    Java 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
    Lexaras likes this.

  11. #11
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: How to draw a triangle

    Ive tried with polygon in this method:
    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...
    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:/

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default 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.

  13. #13
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default 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)
    Last edited by Lexaras; 02-17-2012 at 08:02 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: How to draw a triangle

    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?

  15. #15
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default 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.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: How to draw a triangle

    How have you defined the variables: red, yellow, etc?
    Why not use a Color object: Color.RED?

  17. #17
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default 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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: How to draw a triangle

    this lets to use red,black without Color.Red.
    Very poor technique. It makes the code hard to understand. Are you really that lazy?

  19. #19
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default 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.
    Last edited by Lexaras; 02-17-2012 at 08:28 PM.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: How to draw a triangle

    ShapeComponent tooks only int parameters
    Then something needs to be rewritten. How much can you change?

Page 1 of 2 12 LastLast

Similar Threads

  1. triangle
    By dinn in forum New To Java
    Replies: 6
    Last Post: 12-07-2011, 06:20 PM
  2. Upside Down Triangle
    By Interista in forum New To Java
    Replies: 13
    Last Post: 12-02-2011, 05:09 PM
  3. making triangle using *
    By Bravid2001 in forum New To Java
    Replies: 1
    Last Post: 09-13-2011, 10:05 AM
  4. triangle
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 11-07-2010, 07:12 PM
  5. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •