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

Thread: Help on an assignment

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help on an assignment

    Hello Everyone,

    I am working on a project for a class where we just need to make a very generic PAPI . I almost have it done, but I am having trouble fulfilling the last requirement. We need to have a method "+ PAPICompontent(runway : int)" runway is an int variable. The part I am not understanding is it says to "Create a constructor that takes the runway number and assigns it to the runway instance variable. The code that I have is:

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import javax.swing.JComponent;
    import java.awt.Color;
    import java.awt.geom.Ellipse2D;
    import java.awt.Font;
     
    public class PAPIComponent extends JComponent
    {
     
       private int runway;
       private Color light1;
       private Color light2;
       private Color light3;
       private Color light4;
     
       public PAPIComponent(int runway)
       {
          light1 = Color.WHITE;
          light2 = Color.WHITE;
          light3 = Color.RED;
          light4 = Color.RED; 
       }
     
       public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;
          String rw = String.valueOf(runway);
     
          g2.setColor(Color.WHITE);
          g2.setFont(new Font("TimesRoman", Font.PLAIN, 72)); 
          g2.drawString(rw, 70, 300);
     
          g2.setColor(Color.BLACK);
          Rectangle background = new Rectangle(0, 0, 400, 650);
          g2.fill(background);
     
          g2.setColor(Color.WHITE);
          Rectangle median = new Rectangle(190, 0, 8, 100);
          g2.fill(median);
          median.translate(0,150);
          g2.fill(median);
          median.translate(0,150);
          g2.fill(median);
          median.translate(0,150);
          g2.fill(median);
     
          Rectangle threshold = new Rectangle(20, 550, 35, 80);
          g2.fill(threshold);
          threshold.translate(40, 0);
          g2.fill(threshold);
          threshold.translate(40, 0);
          g2.fill(threshold);
          threshold.translate(40, 0);
          g2.fill(threshold);
          threshold.translate(72, 0);
          g2.fill(threshold);
          threshold.translate(40, 0);
          g2.fill(threshold);
          threshold.translate(40, 0);
          g2.fill(threshold);
          threshold.translate(40, 0);
          g2.fill(threshold);
     
          Rectangle bottom = new Rectangle(0, 650, 400, 150);
          g2.fill(bottom);
     
          Ellipse2D.Double circle1 = new Ellipse2D.Double(50, 665, 50, 50);
          g2.setColor(light1);
          g2.fill(circle1);
          g2.setColor(Color.BLACK);
          g2.draw(circle1);
     
     
          Ellipse2D.Double circle2 = new Ellipse2D.Double(125, 665, 50, 50);
          g2.setColor(light2);
          g2.fill(circle2);
          g2.setColor(Color.BLACK);
          g2.draw(circle2);
     
          Ellipse2D.Double circle3 = new Ellipse2D.Double(200, 665, 50, 50);
          g2.setColor(light3);
          g2.fill(circle3);
          g2.setColor(Color.BLACK);
          g2.draw(circle3);
     
     
          Ellipse2D.Double circle4 = new Ellipse2D.Double(275, 665, 50, 50);
          g2.setColor(light4);
          g2.fill(circle4);
          g2.setColor(Color.BLACK);
          g2.draw(circle4);
       }
     
       public void glidePathOn()
       {
          light1 = Color.WHITE;
          light2 = Color.WHITE;
          light3 = Color.RED;
          light4 = Color.RED;
       }
     
       public void glidePathSlightlyHigh()
       {
          light1 = Color.WHITE;
          light2 = Color.WHITE;
          light3 = Color.WHITE;
          light4 = Color.RED;
       }
     
       public void glidePathTooHigh()
       {
          light1 = Color.WHITE;
          light2 = Color.WHITE;
          light3 = Color.WHITE;
          light4 = Color.WHITE;
       }
     
       public void glidePathSlightlyLow()
       {
          light1 = Color.WHITE;
          light2 = Color.RED;
          light3 = Color.RED;
          light4 = Color.RED; 
       }
     
       public void glidePathTooLow()
       {
          light1 = Color.RED;
          light2 = Color.RED;
          light3 = Color.RED;
          light4 = Color.RED;  
       }
    }

    And the viewer:

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import java.awt.Color;
    public class PAPIViewer
    {
       public static void main(String[] args)
       {
          JFrame frame = new JFrame();
          frame.setSize(400, 800);
          frame.setTitle("PAPI");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
          PAPIComponent component = new PAPIComponent();
          frame.add(component);      
     
          component.glidePathOn();      
          frame.setVisible(true);
          JOptionPane.showMessageDialog(frame, "On glide path");
     
          component.glidePathSlightlyHigh();      
          frame.repaint();
          JOptionPane.showMessageDialog(frame,  "Glide path slightly high.");
     
          component.glidePathTooHigh();      
          frame.repaint();
          JOptionPane.showMessageDialog(frame,  "Glide path too high.");
     
          component.glidePathSlightlyLow();      
          frame.repaint();
          JOptionPane.showMessageDialog(frame,  "Glide path slightly low.");
     
          component.glidePathTooLow();      
          frame.repaint();
          JOptionPane.showMessageDialog(frame,  "Glide path too low.");
     
     
       }
    }

    I am also having an issue with the viewer. After I added the (int runway) to PAPIComponent I can't get the viewer to compile anymore. Also I cannot get the runway variable to actually draw and appear.

    If you need any more info let me know. Any help is very much appreciated!
    Last edited by wabbitshark; September 26th, 2014 at 01:43 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Help on an assignment

    For your first question, they mean to do something like:
       public PAPIComponent(int runway)
       {
          light1 = Color.WHITE;
          light2 = Color.WHITE;
          light3 = Color.RED;
          light4 = Color.RED; 
     
          // assign the instance variable runway to the value passed to the constructor
          this.runway = runway;
       }
    I can't get the viewer to compile anymore
    If you're getting errors, post the entire text of the error message. Do the constructor signature and the call to the constructor match?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    wabbitshark (September 26th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help on an assignment

    The error I am getting is

     
    PAPIViewer.java:14: error: constructor PAPIComponent in class PAPIComponent cannot be applied to given types;
          PAPIComponent component = new PAPIComponent();
                                    ^
      required: int
      found: no arguments
      reason: actual and formal argument lists differ in length
    1 error

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Help on an assignment

    The error message is pretty clear. What don't you understand about it?

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    wabbitshark (September 26th, 2014)

  7. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help on an assignment

    I am not sure what i need to add to " new PAPIComponent(); ". From reading the error I am getting that I need to add an argument to it so they have the same amount, but I don't know what that argument should be.

    UPDATE:
    I got it compiling and running again with "new PAPIComponent(32); " would this be correct?
    Last edited by wabbitshark; September 26th, 2014 at 11:35 AM.

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Help on an assignment

    Looking at the assignment info you've given and code so far, I'm not sure if it's correct. I assume the runway number is a two digit number from 01 - 36, representing the "name" of the runway (see this reference). If you know the runway is runway 32, then I guess it's correct.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    wabbitshark (September 26th, 2014)

  10. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help on an assignment

    Yes it was. I would like to thank you very much for the help. I was able to answer my second question after consulting the API further. Again thank you very much.

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Help on an assignment

    You're welcome. Glad you figured it out and (hopefully) learned that the API should usually be the first place to look.

Similar Threads

  1. Behaviour of shortcut assignment and normal assignment for float
    By rakeshkr2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2014, 02:54 AM
  2. New assignment
    By clarky2006 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 11th, 2013, 06:20 PM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. Assignment please Help :(
    By pagalas07 in forum Collections and Generics
    Replies: 3
    Last Post: March 30th, 2011, 08:52 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM