Jump to content

  • Log In with Google      Sign In   
  • Create Account

Java Class Constructor Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Matthew Melendez   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 February 2012 - 02:36 PM

Can anyone find any problems that make arise with this code?
Will there be a problem with an empty constructor like this?

package foo;


import java.awt.Image.*;
import java.awt.image.BufferedImage;
public class foo
{
   BufferedImage image = new BufferedImage(64,32,BufferedImage.TYPE_INT_ARGB);
  

	public foo()
	{
		//no code here
	}
	public void setRgb(int x, int y, int rgb)
	{
		image.setRGB(x,y,rgb);
	}
}

I want to be able to create a new foo object like so:
foo object = new foo();

and be able to do this:
object.setRgb(5,5,0x00000000)


Sponsor:

#2 IceBreaker23   Members   -  Reputation: 618

Like
0Likes
Like

Posted 02 February 2012 - 02:56 PM

There won´t be an error. Although it´s not a good programming style.
The better way is to call the constructor of the image in your own constructor like this:
package foo;

import java.awt.Image.*;
import java.awt.image.BufferedImage;
public class foo
{
   BufferedImage image;
 
	    public foo()
	    {
			    image = new BufferedImage(64,32,BufferedImage.TYPE_INT_ARGB)
	    }
	    public void setRgb(int x, int y, int rgb)
	    {
			    image.setRGB(x,y,rgb);
	    }
}

Hope I could help you!

#3 Matthew Melendez   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 February 2012 - 03:20 PM

There won´t be an error. Although it´s not a good programming style.
The better way is to call the constructor of the image in your own constructor like this:

package foo;

import java.awt.Image.*;
import java.awt.image.BufferedImage;
public class foo
{
   BufferedImage image;

		public foo()
		{
				image = new BufferedImage(64,32,BufferedImage.TYPE_INT_ARGB)
		}
		public void setRgb(int x, int y, int rgb)
		{
				image.setRGB(x,y,rgb);
		}
}

Hope I could help you!


I tired that and the setRgb() method can't find the image object. I think it might be out of scope of the setRgb() method.

#4 WavyVirus   Members   -  Reputation: 735

Like
0Likes
Like

Posted 02 February 2012 - 04:44 PM

I haven't used the AWT classes, but in general the setRgb method should be able to use the image object. Can you post any error message which is generated?

#5 rip-off   Moderators   -  Reputation: 8727

Like
1Likes
Like

Posted 02 February 2012 - 05:53 PM

The better way is to call the constructor of the image in your own constructor like this:

It is about the same. This is stylistic really.

I tired that and the setRgb() method can't find the image object. I think it might be out of scope of the setRgb() method.

Did you leave "image" as a field or did you accidentally make it local to the constructor?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS