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 11 of 11

Thread: help me draw a triangle....

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me draw a triangle....

    so here is my code, i have to draw an entire scene but i figure if someone can show me how to add a triangle so that it prints out WITH the code provided, then i will be able to continue from there:

     
     
    import acm.graphics.*; // The graphics objects
    import acm.program.*; // Provides program structure for the objects
    import java.awt.*; // This gives us colors
     
     
    public class car extends GraphicsProgram {
    	/**
    	 * magic ACM library stuff. When you call start, it calls run in your class.
    	 * @param args
    	 */
    	public static void main(String[] args) {
    	    new car().start(args);
    	}
    	/**
    	 * Build the car and make it move
    	 */
    	public void run() {
    	    body camero = new body();
    	    // Move it 10 times 
    	    for(int i = 0; i < 10; i++) {
    	    	pause(1000);
    	    	camero.move(20, 50);
    	    }
     
    	}
    	/**
    	 * The body also has wheels
    	 * @author clement
    	 *
    	 */
    	class body {
    		private GRect rect;
    		private wheel rightWheel, leftWheel;
    		/**
    		 * Create a rectangle for the body and two wheels
    		 */
    		body() {
    			rect = new GRect(100, 100, 100, 50);
    			rect.setFilled(true);
    		    rect.setColor(Color.RED);
    		    add(rect);
    		    rightWheel = new wheel(115,150);
    		    leftWheel = new wheel(175,150);
    		}
    		/**
    		 * move the body in the x and y direction
    		 * @param xdir
    		 * @param ydir
    		 */
    		void move(int xdir, int ydir) {
    			rect.move(xdir, ydir);
    			rightWheel.move(xdir, ydir);
    			leftWheel.move(xdir, ydir);
    		}
     
    	}
     
    	class wheel {
    		private GOval circle;
    		/**
    		 * Create a wheel with x and y coordinates of the upper left corner
    		 * @param xval
    		 * @param yval
    		 */
    		wheel(int xval, int yval) {
    			circle = new GOval(xval, yval, 20, 20);
    			circle.setFilled(true);
    		    circle.setColor(Color.BLUE); // We like blue wheels
    		    add(circle);
    		}
    		/**
    		 * Move the wheel in the given direction
    		 * @param xdir
    		 * @param ydir
    		 */
    		void move(int xdir, int ydir){
    			circle.move(xdir, ydir);
    		}
    	}
    }

    please show where and how to insert a triangle so that it will print out with this code (doesnt matter where or how big the triangle is because im going to probably move and resize it anyway)


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: help me draw a triangle....


  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me draw a triangle....

    Quote Originally Posted by Darryl.Burke View Post

    yeah thats right. i posted it there first but didnt even get 1 person to look at my post.... let alone a reply. i just joined that forum today so i didnt know what to expect from that site. so instead of posting your rude reply, go ahead and post a question as to why i did so.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: help me draw a triangle....

    The only reason you would find my reply rude is if you knew it was wrong to cross post without referring to the other post.

    So if you know it's wrong, why did you do it?

    go ahead and post a question as to why i did so.
    Quite frankly, I'm not interested.

    db

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me draw a triangle....

    Quote Originally Posted by Darryl.Burke View Post
    The only reason you would find my reply rude is if you knew it was wrong to cross post without referring to the other post.

    So if you know it's wrong, why did you do it?


    Quite frankly, I'm not interested.

    db

    look, im just trying to get some help, so if youre not the one to do that, then please leave my thread and stop posting here

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: help me draw a triangle....

    Um, no.

    db

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me draw a triangle....

    okay Darryl, since you refuse to leave, maybe you can help me.... do you know how to add a triangle to my code? and if so can you please show me

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help me draw a triangle....

    Hello beandip408,

    Is that your entire code? Please post something I can compile..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help me draw a triangle....

    beandip, cross-posting is not against the rules of this forum, but providing links to the other forums you have posted in helps others know about the whole discussion going on - for instance the library you use in your code which I had a question about is key to your question - without knowing about your other post I would be asking the same questions already answered in the that forum, rehashing the same issues and in effect, wasting both of our time. So I encourage you, should you cross post, to provide links to other forums you have posted in to minimize the redundant questions. That all being said, I would presume many people are unfamiliar with this library so your question may be best answered by reading some of the API and tutorials on that site, for example:
    Chapter 2. Using the acm.graphics Package, most notable the polygon class.

  10. #10
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me draw a triangle....

    Quote Originally Posted by copeg View Post
    beandip, cross-posting is not against the rules of this forum, but providing links to the other forums you have posted in helps others know about the whole discussion going on - for instance the library you use in your code which I had a question about is key to your question - without knowing about your other post I would be asking the same questions already answered in the that forum, rehashing the same issues and in effect, wasting both of our time. So I encourage you, should you cross post, to provide links to other forums you have posted in to minimize the redundant questions. That all being said, I would presume many people are unfamiliar with this library so your question may be best answered by reading some of the API and tutorials on that site, for example:
    Chapter 2. Using the acm.graphics Package, most notable the polygon class.
    that was actually very helpful. so im temporarily redoing my code... however i cant seem to figure out the actual triangle part of it. here is what i have so far:

    /*
     * File: GFace.java
     * ----------------
     * This file defines a compound GFace class.
     */
     
    import acm.program.*;
    import acm.graphics.*;
     
    import java.awt.*;
     
     
    public class Gface extends GraphicsProgram {
     
    	/** Runs the program */
    	   public void run() {
    	      GRect rect = new GRect(100, 200, 50, 100 / PHI);
    	      rect.setFilled(true);
    	      rect.setColor(Color.BLACK);
    	      add(rect);
     
     
    	      //snowman body
    	      GOval ovalbottom = new GOval(300, 170 + 50 / PHI, 100, 100 / PHI);
    	      ovalbottom.setFilled(true);
    	      ovalbottom.setColor(Color.LIGHT_GRAY);
    	      add(ovalbottom);
     
    	      GOval ovalmiddle = new GOval (300, 120 + 50 / PHI, 100, 100 / PHI);
    	      ovalmiddle.setFilled(true);
    	      ovalmiddle.setColor(Color.LIGHT_GRAY);
    	      add(ovalmiddle);
     
     
    	      GOval ovaltop = new GOval (300, 70 + 50 / PHI, 100, 100 / PHI);
    	      ovaltop.setFilled(true);
    	      ovaltop.setColor(Color.LIGHT_GRAY);
    	      add(ovaltop);
    	      //end snowman body
     
     
    	      GOval Right_Eye = new GOval (330, 120, 5, 5);
    	      Right_Eye.setFilled(true);
    	      Right_Eye.setColor(Color.BLACK);
    	      add(Right_Eye);
     
    	      GOval Left_Eye = new GOval (360, 120, 5, 5);
    	      Left_Eye.setFilled(true);
    	      Left_Eye.setColor(Color.BLACK);
    	      add(Left_Eye);
     
    	      GPolygon poly = new GPolygon();
    	      poly.[COLOR="Yellow"]addPoint[/COLOR](0, 0);
    	      poly.[COLOR="Yellow"]addPoint[/COLOR](20, 0);
    	      poly.[COLOR="Yellow"]addPoint[/COLOR](10, 16);
    	      add(poly);
     
     
     
    	   }
     
    	/** Constant representing the golden ratio */
    	   public static final double PHI = 1.618;	
     
    }

    my addpoints arent working right. any advice on how to make this triangle print?


    nevermind i figured it out. it was .addEdge
    Last edited by beandip408; October 27th, 2010 at 09:55 PM.

  11. #11
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me draw a triangle....

    thanks for the help everyone... here is my final code (not pretty but it works.

    /*
     * File: GFace.java
     * ----------------
     * This file defines a compound GFace class.
     */
     
    import acm.program.*;
    import acm.graphics.*;
     
    import java.awt.*;
     
     
     
     
    public class Gface extends GraphicsProgram {
    	public static final double PHI = 1.618;
    	/** Runs the program */
    	public void run() {
    		setSize (800, 800);
    		Snowman frosty = new Snowman();
    		Tree smallTree = new Tree();
    		for(int i = 0; i < 10; i++) {
    			pause(1000);
    			frosty.move(20,50);
    		}
     
    	}
    	class Tree{
    		private Treetrunk trunk;
    		private PineNeedles needles;
     
    		public Tree(){
    			trunk = new Treetrunk();
    			needles = new PineNeedles();
    		}
    	}
    	class Snowman {   
    		private Snowbody body;
    		private Face face;
    		private Arms arms;
     
    		public Snowman() {
     
    			body = new Snowbody();
    			face = new Face();
    			arms = new Arms();
     
    		}
    		void move(int xdir, int ydir) {
    			body.move(xdir, ydir);
    			face.move(xdir, ydir);
    			arms.move(xdir, ydir);
    	}
    	}
    	class Treetrunk{
    		private GRect trunk;
     
    		Treetrunk(){
    			trunk = new GRect(100, 200, 50, 100 / PHI);
    			trunk.setFilled(true);
    			trunk.setColor(Color.BLACK);
    			add(trunk);
    		}
    	}
     
    	class PineNeedles{
    		private GPolygon treebottom, treemiddle, treetop;
     
    		PineNeedles(){
     
    			GPolygon treebottom = new GPolygon();
    			treebottom.addEdge(127, 175);
    			treebottom.addEdge(-80, 40);
    			treebottom.addEdge(150, 0);
    			treebottom.setFilled(true);
    			treebottom.setColor(Color.GREEN);
    			add(treebottom);
     
     
    			GPolygon treemiddle = new GPolygon();
    			treemiddle.addEdge(127, 150);
    			treemiddle.addEdge(-80, 40);
    			treemiddle.addEdge(150, 0);
    			treemiddle.setFilled(true);
    			treemiddle.setColor(Color.GREEN);
    			add(treemiddle);
     
    			GPolygon treetop = new GPolygon();
    			treetop.addEdge(127, 125);
    			treetop.addEdge(-80, 40);
    			treetop.addEdge(150, 0);
    			treetop.setFilled(true);
    			treetop.setColor(Color.GREEN);
    			add(treetop);
    		}
    	}
     
    	class Arms{
    		private GLine right_Bicept, left_Bicept, right_forarm, left_forarm;
     
    		Arms(){
    			right_Bicept = new GLine (300, 180, 280, 170);
    			right_Bicept.setColor(Color.BLACK);
    			add(right_Bicept);
     
    			left_Bicept = new GLine (400, 179, 420, 170);
    			left_Bicept.setColor(Color.BLACK);
    			add(left_Bicept);
     
     
     
    			right_forarm = new GLine (280, 170, 310, 150);
    			right_forarm.setColor(Color.BLACK);
    			add(right_forarm);
     
    			left_forarm = new GLine (420, 170, 395, 150);
    			left_forarm.setColor(Color.BLACK);
    			add(left_forarm);
    		}
    		void move(int xdir, int ydir) {
    			right_Bicept.move(xdir, ydir);
    			left_Bicept.move(xdir, ydir);
    			right_forarm.move(xdir, ydir);
    			left_forarm.move(xdir, ydir);
    		}
    	}
     
    	class Face{
    		private GOval Right_Eye, Left_Eye;
     
    		Face(){
    			Right_Eye = new GOval (330, 120, 5, 5);
    			Right_Eye.setFilled(true);
    			Right_Eye.setColor(Color.BLACK);
    			add(Right_Eye);
     
    			Left_Eye = new GOval (360, 120, 5, 5);
    			Left_Eye.setFilled(true);
    			Left_Eye.setColor(Color.BLACK);
    			add(Left_Eye);
    		}
    		void move(int xdir, int ydir) {
    			Right_Eye.move(xdir, ydir);
    			Left_Eye.move(xdir, ydir);
    		}
    	}
     
    	class Snowbody {
    		private GOval ovalbottom, ovalmiddle, ovaltop;
     
     
    		Snowbody() {
     
     
    			ovalbottom = new GOval(300, 170 + 50 / PHI, 100, 100 / PHI);
    			ovalbottom.setFilled(true);
    			ovalbottom.setColor(Color.LIGHT_GRAY);
    			add(ovalbottom);
     
    			ovalmiddle = new GOval (300, 120 + 50 / PHI, 100, 100 / PHI);
    			ovalmiddle.setFilled(true);
    			ovalmiddle.setColor(Color.LIGHT_GRAY);
    			add(ovalmiddle);
     
     
    			ovaltop = new GOval (300, 70 + 50 / PHI, 100, 100 / PHI);
    			ovaltop.setFilled(true);
    			ovaltop.setColor(Color.LIGHT_GRAY);
    			add(ovaltop);
    		}
    		void move(int xdir, int ydir) {
    			ovalbottom.move(xdir, ydir);
    			ovalmiddle.move(xdir, ydir);
    			ovaltop.move(xdir, ydir);
    		}
    	}
     
     
     
     
    }

Similar Threads

  1. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 06:15 PM
  2. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM
  3. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM
  4. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM