Results 1 to 7 of 7
  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default How to fill color on triangle

    Hi everyone...
    I draw a triangle using Lines.
    I want to fill color to the triangle. But I am not successful. So far I can only color the line but not fill it.

    Java Code:
    	
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        int k=0;
        for (j=0 ; j < numOfLines; j++){	// the values of numOfLines retrieved from other method.
        g.setColor(Color.green);
        g.drawLine(x[k], x[k+1], x[k+2], x[k+3]);
        k = k+4;  //index files
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd look into using the Graphics method fillPolygon(...). Either that or perhaps create a Path2D.Float object and render it with a Graphics2D object.

  3. #3
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Thanks a lot.

  4. #4
    Nes_java is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    almost you solved this problem by using the knowledge related to ARRAY.
    who can help me to write code to fill yellow on a triangle on a panel?

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Nes_java I can understand that you want to draw a triangle on a DrawingPanel ? if that is the case you must use:
    1) new Polygon()
    2) by addPoint(x,y) determine the three points of the polygon
    3) change or setColor to draw with
    4) fillPolygon(poly) to fill it with the color you want

    and don't forget to getGraphincs() from the DrawingPanel class or it wont work
    Hope I helped you
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Nes_java View Post
    almost you solved this problem by using the knowledge related to ARRAY.
    who can help me to write code to fill yellow on a triangle on a panel?
    Please don't hijack another's old thread. If you have a question, start your own new thread, and please don't ask folks for the code. Do the code yourself and ask for help with your code.

  7. #7
    Nes_java is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by baf06 View Post
    Nes_java I can understand that you want to draw a triangle on a DrawingPanel ? if that is the case you must use:
    1) new Polygon()
    2) by addPoint(x,y) determine the three points of the polygon
    3) change or setColor to draw with
    4) fillPolygon(poly) to fill it with the color you want

    and don't forget to getGraphincs() from the DrawingPanel class or it wont work
    Hope I helped you
    Ok, thanks you.
    ^^

    Quote Originally Posted by Fubarable's Avatar
    Fubarable
    Please don't hijack another's old thread. If you have a question, start your own new thread, and please don't ask folks for the code. Do the code yourself and ask for help with your code.
    Yes, I will do so
    Thanks for your reminder.

Similar Threads

  1. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM
  2. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 09:39 PM
  3. How to Fill Arc in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:14 AM
  4. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 08:51 PM
  5. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 12:23 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
  •