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

Thread: Objects as parameters, and implicit calling?i

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Objects as parameters, and implicit calling?i

    Hey guys! I had a question on two things. First, can you explain the parameters part of public void paint(Graphics g){} ? I don't understand graphics g? Is it an object? It doesn't have an instance created... Also, for the next question I need to show my code. The question is, I don't understand how the method paint is called. Does the method implicitly execute? I've seen where theres multiple methods and they are never called in the main method... Also, why is it necessary to say "new Carz"? Why must we create an instance of the Carz constructer? Thanks to all.




    import javax.swing.*;
    import java.awt.*;
    class Carz extends JFrame {
          Carz() {
       super("Cars");
    setSize(1000,1000);
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
       }
    Car Ferrari = new Car(20, 200, "Red");
    Car Lexus = new Car(30, 160, "White");
    Car Honda = new Car(25, 150, "Blue");
    public void paint(Graphics m) {
    m.setFont(new Font( "Helvetica", Font.PLAIN, 16));
    m.drawRect(20, 50, 960, 460);
    m.setColor(Color.BLACK);
    m.fillRect(20, 50, 960, 460);
    m.setColor(Color.WHITE);
    m.drawString("We have a " + Ferrari.color + "that gets " + Ferrari.mpg + "and can go as fast as " + Ferrari.mph + ".", 30,70);
    m.drawString("We have a " + Honda.color + "that gets " + Honda.mpg + "and can go as fast as " + Honda.mph + ".", 30,100);
    m.drawString("We have a " + Lexus.color + "that gets " + Lexus.mpg + "and can go as fast as " + Lexus.mph + ".", 30,130);
      }
    public static void main(String[] args) {
    new Carz();
       }
    }
     
    class Car{
    int mpg;
     int mph;
        String color;
            Car(int mpg, int mph, String color){
        this.mpg = mpg;
      this.mph = mph;
    this.color = color;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Objects as parameters, and implicit calling?i

    The paint() for AWT and paintComponent() for Swing are methods that you override to do custom painting.
    They are called by the jvm when the component needs to be repainted. See the tutorial about coding:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    and this:
    Painting in AWT and Swing

    why is it necessary to say "new Carz"?
    See the tutorial:
    http://docs.oracle.com/javase/tutori...mber/ctor.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects as parameters, and implicit calling?i

    Quote Originally Posted by Norm View Post
    The paint() for AWT and paintComponent() for Swing are methods that you override to do custom painting.
    They are called by the jvm when the component needs to be repainted. See the tutorial about coding:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    and this:
    Painting in AWT and Swing


    See the tutorial:
    Constructors (The Java™ Tutorials > The Reflection API > Members)
    Hi Norm. I know what constructors are, and realize that doing new Carz() is just calling the constructor. What I don't get is how paint method is executed without being called. Thank you.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Objects as parameters, and implicit calling?i

    how paint method is executed
    From my last post:
    They are called by the jvm when the component needs to be repainted.

    Did you read the pages I gave links to?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects as parameters, and implicit calling?i

    Quote Originally Posted by Norm View Post
    From my last post:
    They are called by the jvm when the component needs to be repainted.

    Did you read the pages I gave links to?
    Oh, ok. Does this only apply to the paint method though? For all others you have to explicitly call them in the main method? Thanks Norm, and i read a part

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Objects as parameters, and implicit calling?i

    It depends on the method. Read the API doc for each class and method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is the use of "this." and implicit parameters?
    By jean28 in forum Java Theory & Questions
    Replies: 3
    Last Post: December 18th, 2012, 03:41 AM
  2. Problem with calling objects from same-class methods in main
    By thekbon in forum Object Oriented Programming
    Replies: 12
    Last Post: December 18th, 2012, 01:10 AM
  3. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  4. Calling method cant get the parameters right
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 02:23 PM
  5. calling objects in a differnt class
    By jack_nutt in forum Object Oriented Programming
    Replies: 12
    Last Post: July 8th, 2011, 01:57 PM

Tags for this Thread