Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Drawing a Star of David

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing a Star of David

    Hello!

    I am trying to draw a Star of David using drawLine.

    The assignment calls for the creation of four quadrants, with each quadrant containing a specified shape. In the lower right quadrant, I need to create the star.

    Now I know that the star consists of two triangles - one pointed upwards and the other downwards. I have created triangles pointed in opposite directions, but when I view them, it definitely does NOT look like the star.

    Can anybody try to help me figure out this likely simple mistake?
    import javax.swing .*;
    import java .awt .*;
    import java .applet.*;
     
    public class JApplet extends Applet
    {
    public void paint(Graphics Sketch)
    {
    int Row,Col,k,Side,Height ;
    int PointAx,PointBx,PointCx,PointDx,PointEx,PointFx,Po intGx ;
    int PointAy,PointBy,PointCy,PointDy,PointEy,PointFy,Po intGy ;
    int CenterAx,CenterAy,CenterBx,CenterBy,CenterCx,Cente rCy,CenterDx,CenterDy,Offset;
     
    Dimension RowCol;
     
    RowCol = getSize() ;
    Row = RowCol.width - 1 ;
    Col = RowCol.height - 1;
     
    Sketch.setColor(Color.red );
    Sketch.drawLine(0, 0,Row, 0);
    Sketch.drawLine(0,Col,Row,Col);
    Sketch.drawLine(0, 0, 0,Col);
    Sketch.drawLine(Row,0,Row,Col);
     
    Sketch.setColor(Color.blue );
    Sketch.drawLine(Row/2, 0,Row/2,Col );
    Sketch.drawLine(0 ,Col/2,Row ,Col/2);
     
    Offset = Col/16 ;
    CenterAx = Row/4 ;
    CenterAy = Col/4 ;
    CenterBx = 3*Row/4;
    CenterBy = Col/4 ;
    CenterCx = Row/4 ;
    CenterCy = 3*Col/4;
    CenterDx = 3*Row/4;
    CenterDy = 3*Col/4;
     
    // STAR OF DAVID
     
    Height = (Col - 2 * Offset) / 2 ;
    Side = (int) (2 * Math.sqrt(3) * Height / 3);
     
    PointAx = CenterDx ;
    PointAy = Col /2 + Offset / 2 ;
    PointBx = CenterDx + Side / 2 ;
    PointBy = Col - Offset /2 ;
    PointCx = CenterDx - Side / 2 ;
    PointCy = PointBy ;
    PointDx = CenterDx + Side / 2 ;
    PointDy = Col / 2 + Offset / 2;
    PointEx = PointAx ;
    PointEy = Col - Offset / 2 ;
    PointFx = CenterDx - Side / 2 ;
    PointFy = PointDy ;
     
    Sketch.setColor(Color.orange);
    Sketch.drawLine(PointAx,PointAy,PointBx,PointBy);
    Sketch.drawLine(PointBx,PointBy,PointCx,PointCy);
    Sketch.drawLine(PointCx,PointCy,PointAx,PointAy);
    Sketch.drawLine(PointDx,PointDy,PointEx,PointEy);
    Sketch.drawLine(PointEx,PointEy,PointFx,PointFy);
    Sketch.drawLine(PointFx,PointFy,PointDx,PointDy);
     
     
    }
    }
    Last edited by copeg; July 31st, 2012 at 07:00 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Drawing a Star of David

    Try taking a piece of paper, draw the shape on it, number the lines that are needed to draw the shape, label each line end with its x,y values and then for each line, record the x,y values for ends of the line.
    There should be 6 line with each line having two pairs of x,y values (one for each end)
    Then code the drawLine() method calls for those 6 lines using the x,y values.

    BTW JApplet is the name of a Java SE class.

    Also posted at:
    http://forums.devshed.com/java-help-...ml#post2808400
    Last edited by Norm; July 31st, 2012 at 03:14 PM. Reason: added link
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Drawing a Star of David

    @tryingtolearn, don't delete your threads. I have reverted your post.

Similar Threads

  1. David
    By dkhorne in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 22nd, 2012, 09:45 PM
  2. drawing is in the same postion :(
    By kisokiso in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 20th, 2011, 07:02 AM
  3. star design programs
    By prasansani in forum Java Theory & Questions
    Replies: 6
    Last Post: September 20th, 2011, 12:42 PM
  4. A Star Pathfinding algorithm optimization
    By randomcracker in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 18th, 2011, 11:11 PM
  5. Drawing
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:43 PM