Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default I don't understand why objects are passed as parameters.

    I understand that an object can be passed as a parameter, and get that you would do it if you wanted to USE an object when you use your method. What I don't understand, however, is why you would pass an object for a parameter when you won't use it. For example, the method paint uses Graphics object, but you never call it with an object. That is so confusing! Please help me!

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: I don't understand why objects are passed as parameters.

    you need to pass Graphics as the object for the paint method because it allows us to draw shapes and such.

    The reason for this is Graphics is a Class in the Java libraries that holds all the methods that we need to draw. It allows you to references the Graphics Class without having to instantiate it like you would normally.

    see: Graphics (Java 2 Platform SE v1.4.2)

    This means we can do as follows:

    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawRect(100,100,10,10);
    g.fillRect(50,50,10,10);
    }

    You see? Using the libraries provided with Java you can get stuff done!

    Do you understand graphics now?

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: I don't understand why objects are passed as parameters.

    In your example, paint, you aren't responsible for obtaining the graphics context which paint uses. That is done behind the scenes for you. In order to invoke paint you simply need to call repaint() which tells the JRE to queue up a paint request. The system will repaint as soon as possible. If more than one paint request is pending the system may collapse them as one.

    The only exception is when you override paintComponent. You need to invoke the super classes method by doing a super.paintComponent(g). Also, you should not override paint in Swing components, only paintComponent.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  2. Replies: 1
    Last Post: 02-10-2012, 09:17 PM
  3. URL parameters not getting passed during javascript submit
    By fierof2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 10-01-2011, 08:26 AM
  4. hiding passed parameters using window.location.href = 'params' method
    By kulangotski in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-01-2011, 12:49 PM
  5. Replies: 3
    Last Post: 06-14-2011, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •