This discussion is archived
7 Replies Latest reply: Jun 3, 2013 1:32 PM by TPD-Opitz-Consulting-com RSS

java graphics program

1012250 Newbie
Currently Being Moderated
Dear Sir,
I hereby post source codes of 3nos java files. 1. TrainCar.java 2. BoxCar.java 3. Train.java I compile and run the codes in the following way. I did not find any error both in compilation and running the program. Still, i did not get any output.

javac -classpath acm.jar Train.java BoxCar.java TrainCar.java

java -cp .;acm.jar Train BoxCar TrainCar

Edited by: 1009247 on Jun 2, 2013 8:24 AM
  import acm.graphics.*;

  import java.awt.*

  public abstract class TrainCar extends GCompound {

  public TrainCar(Color color) {

      double xLeft = CONNECTOR;

      double yBase = - CAR_BASELINE;

      GLine line = new GLine(0, yBase, CAR_WIDTH + 2 * CONNECTOR, yBase);

      add(line);

      addWheel(xLeft + WHEEL_INSERT, -WHEEL_RADIUS);

      addWheel(xLeft + CAR_WIDTH - WHEEL_INSERT, -WHEEL_RADIUS);

      double yTop = yBase - CAR_HEIGHT;

      GRect r = new GRect(xLeft, yTop, CAR_WIDTH, CAR_HEIGHT);

      r.setFilled(true);

      r.setFillColor(color);

      add(r);
    }

   private void addWheel(double x, double y) {

   GOval wheel = new GOval(x - WHEEL_RADIUS, y - WHEEL_RADIUS, 2 *    
     WHEEL_RADIUS, 2 * WHEEL_RADIUS);

      wheel.setFilled(true);
      wheel.setFillColor(Color.GRAY);
      add(wheel);
     }

    protected static final double CAR_WIDTH = 75;

    protected static final double CAR_HEIGHT = 36;

    protected static final double CAR_BASELINE = 10;

    protected static final double CONNECTOR = 6;

    protected static final double WHEEL_RADIUS = 8;

    protected static final double WHEEL_INSERT = 16;
  } 
  import acm.graphics.*;

   import java.awt.*;

   public class BoxCar extends TrainCar {

   public BoxCar(Color color) {
    super(color);

   double xRightDoor = CONNECTOR + CAR_WIDTH / 2;

   double xLeftDoor = xRightDoor - DOOR_WIDTH;

   double yDoor = - CAR_BASELINE - DOOR_HEIGHT;

   GRect rect = new GRect(xLeftDoor, yDoor, DOOR_WIDTH, DOOR_HEIGHT);

   GRect rect1 = new GRect(xRightDoor, yDoor, DOOR_WIDTH,   
   DOOR_HEIGHT);

   add(rect);

   add(rect1);
 }

  private static final double DOOR_WIDTH = 18;

  private static final double DOOR_HEIGHT = 32;
} 
  import acm.graphics.*;

  import acm.program.*;

  import java.awt.*;

  public class Train extends GCompound {

  public void run() {

       Train train = new Train();

       train.append(new BoxCar(Color.GREEN));
  }
   
   public void append(TrainCar car) {
      double width = getWidth();
      double x = (width == 0) ? 0 : width - TrainCar.CONNECTOR;
      add(car, x, 0);
   }

   public static void main(String args[]){

           new Train().start(args);
      }
    } 
Edited by: 1009247 on Jun 2, 2013 9:53 AM
  • 1. Re: java graphics program
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    please enclose your samples in
    -tags (use the edit function of this forum).
    
    what ist the expected output?
    
    bye
    TT                                                                                                                                                                                                                                                            
  • 2. Re: java graphics program
    1012250 Newbie
    Currently Being Moderated
    Did you now get my codes in right format? Out put will be one BoxCar which is subclass of TrainCar while TrainCar and BoxCar are all subclass of GCompound.

    Arindam Biswas
  • 3. Re: java graphics program
    rp0428 Guru
    Currently Being Moderated
    Did YOU get what TPD asked you to do?

    Edit your post and add \
     on the line before and on the line after the code to preserve formatting. Use the preview tab to see how it displays before posting it. See the FAQ for other formatting guidelines.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 4. Re: java graphics program
    rukbat Guru Moderator
    Currently Being Moderated
    O.P.,

    It seems that even though your have had two different people tell you how to post your code, you went back (the day after your original posting time) and just chose to erase it. You originally had many many lines of unreadable text that you inferred was your working code, but now you only have your command syntax.

    By doing that, you have completely destroyed any possibility of anyone helping you.
    Additionally, the better-skilled people that answer questions here have very long memories. You've probably lost any chances for future assistance, as well.


    .
    Edit:
    ... and now you have pasted code back in to your original post.

    Make up your mind whether you want to help others to help you.

    Edited by: rukbat on Jun 3, 2013 5:25 AM
  • 5. Re: java graphics program
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    1009247 wrote:
    [...]i did not get any output.
    This may be because your code never does output.
    Your code never creates a (J)Frame to show content withing and your graphics-elements are never added to a Graphics container.

    maybe you meight first walk your way though the Java-GUI tzutorials:
    http://download.oracle.com/javase/tutorial/uiswing/

    bye
    TPD
  • 6. Re: java graphics program
    1012250 Newbie
    Currently Being Moderated
    HI TPD!
    Do we need JFrame to add graphical objects to graphics container? Here are examples:
     import acm.program.*;
    import acm.graphics.*;
    
     public class HelloProgram extends GraphicsProgram {
    
        public void run() {
    
        GRect rect = new GRect(100, 50, 100, 100);
    
        add(rect, getWidth() / 2, getHeight() / 2);
      }
     } 
    import acm.graphics.*;
    
      public class GRedCross extends GPolygon {
    
          public GRedCross(double size) {
    
          double edge =  size / 3;
            
         
          addVertex(- size / 2, - edge / 2);
          addEdge(edge, 0);
          addEdge(0, -edge);
          addEdge(edge, 0);
          addEdge(0, edge);
          addEdge(edge, 0);
          addEdge(0, edge);
          addEdge(-edge, 0);
          addEdge(0, edge);
          addEdge(-edge, 0);
          addEdge(0, -edge);
          addEdge(-edge, 0);
          addEdge(0, -edge);
        }
       }  
     import acm.graphics.*;
      import acm.program.*;
      import java.awt.*;
    
      public class GRedCross extends GraphicsProgram {
      public void run() {
    
       double size = 600;
       
      /** Here,we declare and initialize size and pass it as arguement of 
       * constructor new GRedCross1(size);*/
    
       GRedCross cross = new GRedCross(size);
       cross.setFilled(true);
       cross.setColor(Color.RED);
       add(cross, getWidth() / 2, getHeight() / 2);
      }
     } 
    In the first case, we get the gobj rect with the help of add method defined in
    run method while in the second case where we use two source codes, we
    get gobj cross colored red.

    bye
    ARINDAM BISWAS.
  • 7. Re: java graphics program
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    1009247 wrote:
    HI TPD!
    Do we need JFrame to add graphical objects to graphics container? Here are examples:
     import acm.program.*;
    import acm.graphics.*;
    
    public class HelloProgram extends GraphicsProgram {
    
    public void run() {
    
    GRect rect = new GRect(100, 50, 100, 100);
    
    add(rect, getWidth() / 2, getHeight() / 2);
    }
    } 
    import acm.graphics.*;
    
    public class GRedCross extends GPolygon {
    
    public GRedCross(double size) {
    
    double edge =  size / 3;
    
    
    addVertex(- size / 2, - edge / 2);
    addEdge(edge, 0);
    addEdge(0, -edge);
    addEdge(edge, 0);
    addEdge(0, edge);
    addEdge(edge, 0);
    addEdge(0, edge);
    addEdge(-edge, 0);
    addEdge(0, edge);
    addEdge(-edge, 0);
    addEdge(0, -edge);
    addEdge(-edge, 0);
    addEdge(0, -edge);
    }
    }  
     import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
    
    public class GRedCross extends GraphicsProgram {
    public void run() {
    
    double size = 600;
    
    /** Here,we declare and initialize size and pass it as arguement of 
    * constructor new GRedCross1(size);*/
    
    GRedCross cross = new GRedCross(size);
    cross.setFilled(true);
    cross.setColor(Color.RED);
    add(cross, getWidth() / 2, getHeight() / 2);
    }
    } 
    In the first case, we get the gobj rect with the help of add method defined in
    run method while in the second case where we use two source codes, we
    get gobj cross colored red.

    bye
    ARINDAM BISWAS.
    it Loks like the class <TT>GraphicsProgram</TT> is part oft a non standard framework. I'm not able to help on this.

    currently you habe 2 options: ASK the Autors oft that framework for help or change your Programm to use standard JVM classes.

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points