Results 1 to 3 of 3
- 05-07-2013, 02:22 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 6
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!
- 05-07-2013, 03:23 AM #2
Senior Member
- Join Date
- Nov 2012
- Posts
- 257
- Rep Power
- 6
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?
- 05-07-2013, 03:51 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,168
- Rep Power
- 12
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Noob question - Create objects using objects as parameters
By pantaloc in forum New To JavaReplies: 12Last Post: 04-29-2012, 02:55 PM -
Class Not Found Exception when 1st two of four parameters passed Constructor
By mwr1976 in forum New To JavaReplies: 1Last Post: 02-10-2012, 09:17 PM -
URL parameters not getting passed during javascript submit
By fierof2 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 10-01-2011, 08:26 AM -
hiding passed parameters using window.location.href = 'params' method
By kulangotski in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-01-2011, 12:49 PM -
hi guys , please help me in telling about how Objects are passed by reference. :(
By funkygarzon in forum New To JavaReplies: 3Last Post: 06-14-2011, 09:35 PM
Bookmarks