简体   繁体   中英

Buffered Images in java

static BufferedImage img1[];
for(int i=0;i<60;i++)
   {
     img1[i] = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);

    }

It shows an error on the line above this code : Syntax error on token ";", { expected after this token

and below this code as: Multiple markers at this line - Method breakpoint:Video [entry] - main(String[]) - Syntax error on token ")", ; expected - Syntax error on token "(", ; expected

One problem here is that you can't declare a variable which is in the scope of a method to be static. (Or, alternatively, you can't write a for loop which is neither in a method nor a static block.) Fix one of these.

This is a significant difference between C/C++ and Java: In C/C++, you can declare static variables inside functions, and those variables will retain their values across function calls. Java doesn't have that. If you want a variable to retain its value this way, you need to make it a (possibly static) member of a class.

I assume you've got an error in some of your other code. You've also got an error in this code -- you need to declare the length of the img1 array before using it...

BufferedImage img1[] = new BufferedImage[60];

Setting aside questions of style and encapsulation, I suspect the problem is related to where you're doing this in a class.

To instantiate the array in a method, you could do something like this:

class MyClass1 {
  public void initImages(int width, int height) {
    BufferedImage img1[] = new BufferedImage[60];
    for (int i = 0; i < img1.length; i++) {
      img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
  }
}

The array doesn't escape the method, so it will die there.

To instantiate the array as a static member, you could something like this:

class MyClass2 {
  static int width = 100;
  static int height = 100;
  static BufferedImage img1[] = new BufferedImage[60];
  static {
    for (int i = 0; i < img1.length; i++) {
      img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
  }
}

This uses a static initialization block.

...or this:

class MyClass3 {
  static BufferedImage img1[] = initImages(100, 100);

  public static BufferedImage[] initImages(int width, int height) {
    BufferedImage img1[] = new BufferedImage[60];
    for (int i = 0; i < img1.length; i++) {
      img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
    return img1;
  }
}

This uses a static method to initialize a static member.

Your syntax is incorrect. You have to declare an array right after the type.

static BufferedImage[] img1 = new BufferedImage[2];

is the correct syntax.

Here's the edited code from eclipse. This does compile.

import java.awt.image.BufferedImage;


public class Test {
    static BufferedImage[] img1 = new BufferedImage[60];
    static{
        for(int i=0;i<60;i++)
        {
        int width = 20;
        int height = 20;
        img1[i] = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);

        }
    }
}

The following problems we're also found:

  1. width and height undeclared. I initialized them to meaningless numbers
  2. put the for loop in a static block. Code can't exist in the class unless it's in a method or a static block of a class.
  3. This also necessitated the creation of a class Test.

I'm sure that most of this was due to the fact you only submitted a code snippet. I've included it for completeness. This code will create 60 Buffered Images of size 20x20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM