0

For some java homework we were told to create a program that makes collages, with one of the methods being that the one of the "fragments"(images) of the collage will be scaled by width and height. The pre-written class "fragment" already has a method to scale the image:

public void scaleFragment(int newWidth, int newHeight)
{
    width = newWidth;
    height = newHeight;
}

So in the main class I call up this method by doing this:

public void makeTiles(int tileWidth, int tileHeight)
{
  for (Fragment sm:collage){
    Fragment.scaleFragment(tileWidth, tileHeight);
  }
}

When I try to run this however, I come up with this error:
"non-static method scaleFragment (int,int) cannot be referencd from a static context"

After reading up on this website I found that you needed to crate an instance of the class?so I did this:

 Fragment fragment = new Fragment();
  for (Fragment sm:collage){
    Fragment.scaleFragment(tileWidth, tileHeight);
  }

This however comes up with the error "constuctor error in class fragment cannot be applied to given types"

The main class has declaces the arraylist collage like this:

  Fragment fragment = new Fragment();
  for (Fragment sm:collage){
    Fragment.scaleFragment(tileWidth, tileHeight);
  }

can anyone help me solve this problem?
Thanks in advance.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
SammyHD
  • 49
  • 2
  • 8

1 Answers1

0

The problem is that you cannot call scaleFragment using the class name as a qualifier (as in Fragment.scaleFragment(...)) — that only works for static methods. Try this instead:

public void makeTiles(int tileWidth, int tileHeight)
{
  for (Fragment sm:collage){
    sm.scaleFragment(tileWidth, tileHeight);
  }
}

This will call scaleFragment for each element of collage. (This suggestion assumes that collage is a collection or array already populated with Fragment instances. You don't show what collage is, so if the assumption is wrong, you'll have to adjust accordingly.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521