2017-08-10 29 views
0

我幾乎沒有使用java編程的經驗,但我知道我的方法有點。我想要選擇一個被其他人留下的項目。我在這裏和那裏糾正了其他錯誤,但是這一個難倒了我。這是它:如何修復java.lang.ArrayIndexOutOfBoundsException圖像調整大小錯誤?

javax.imageio.IIOException: Can't read input file! 
    at javax.imageio.ImageIO.read(Unknown Source) 
    at Replacer.main(Replacer.java:19) 

令我驚訝的是,程序仍然打開。然而,當我試圖打開圖片,發生這種情況,並顯示的0 X 0個像素的圖像:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 
2147483647 
     at ImageEditor.resize(ImageEditor.java:384) 
     at ImageEditor.resize(ImageEditor.java:308) 
     at ImageFrame.setImage(ImageFrame.java:438) 
     at ImageFrame.actionPerformed(ImageFrame.java:765) 
     at java.awt.Button.processActionEvent(Unknown Source) 
     at java.awt.Button.processEvent(Unknown Source) 
     at java.awt.Component.dispatchEventImpl(Unknown Source) 
     at java.awt.Component.dispatchEvent(Unknown Source) 
     at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
     at java.awt.EventQueue.access$500(Unknown Source) 
     at java.awt.EventQueue$3.run(Unknown Source) 
     at java.awt.EventQueue$3.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP 
rivilege(Unknown Source) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP 
rivilege(Unknown Source) 
     at java.awt.EventQueue$4.run(Unknown Source) 
     at java.awt.EventQueue$4.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP 
rivilege(Unknown Source) 
     at java.awt.EventQueue.dispatchEvent(Unknown Source) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
     at java.awt.EventDispatchThread.run(Unknown Source) 

代用品:

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.PrintStream; 
import javax.imageio.ImageIO; 

public class Replacer 
{ 
    public static void main(String[] args) 
    { 
    BufferedImage i = null; 
    BufferedImage i2 = null; 

    Color[][] blockColors = new Color[16][16]; 

    ImageFrame b = new ImageFrame(i); 
    try 
    { 
     i2 = ImageIO.read(new File("terrain.png")); 
     int blockSize = i2.getWidth(b)/16; 
     System.out.println("Analyzing terrain.png"); 
     int[] buffer = ImageEditor.returnBuffer(i2, b); 
     int width = i2.getWidth(b); 
     for (int j = 0; j < 16; j++) { 
     for (int k = 0; k < 16; k++) { 
      if ((j <= 2) || (k <= 8)) 
      { 
      int[] i3 = ImageEditor.crop(buffer, width, width, b, j * blockSize, k * blockSize, blockSize, blockSize); 
      blockColors[j][k] = ImageEditor.getAverageColor(i3, b); 
      } 
     } 
     } 
     Color[] c2 = new Color[100]; 
     c2[0] = blockColors[1][8]; 
     c2[1] = blockColors[2][10]; 
     c2[2] = blockColors[1][11]; 
     c2[3] = blockColors[1][9]; 
     c2[4] = blockColors[1][7]; 
     c2[5] = blockColors[0][4]; 
     c2[6] = blockColors[2][13]; 
     c2[7] = blockColors[1][13]; 
     c2[8] = blockColors[1][12]; 
     c2[9] = blockColors[2][7]; 
     c2[10] = blockColors[2][11]; 
     c2[11] = blockColors[2][8]; 
     c2[12] = blockColors[2][9]; 
     c2[13] = blockColors[2][12]; 
     c2[14] = blockColors[1][14]; 
     c2[15] = blockColors[1][10]; 

     c2[16] = blockColors[1][0]; 
     c2[17] = blockColors[2][0]; 
     c2[18] = blockColors[2][1]; 
     c2[19] = blockColors[3][1]; 
     c2[20] = blockColors[8][4]; 
     c2[21] = blockColors[5][2]; 
     c2[22] = blockColors[4][2]; 
     c2[23] = blockColors[6][7]; 
     c2[24] = blockColors[4][0]; 
     c2[25] = blockColors[0][1]; 
     c2[26] = blockColors[0][11]; 
     c2[27] = blockColors[7][0]; 
     c2[28] = blockColors[6][1]; 
     c2[29] = blockColors[7][1]; 
     c2[30] = blockColors[8][1]; 
     c2[31] = blockColors[0][9]; 
     c2[32] = blockColors[10][4]; 
     c2[33] = blockColors[9][6]; 
     c2[34] = blockColors[7][6]; 
     c2[35] = blockColors[8][6]; 
     c2[36] = blockColors[2][4]; 

     c2[37] = blockColors[6][3]; 
     c2[38] = blockColors[1][1]; 
     c2[39] = blockColors[5][1]; 
     c2[40] = blockColors[4][1]; 
     c2[41] = blockColors[4][7]; 
     c2[42] = blockColors[5][7]; 
     c2[43] = blockColors[0][3]; 
     c2[44] = blockColors[3][4]; 
     c2[45] = blockColors[8][8]; 
     c2[46] = blockColors[8][9]; 
     c2[47] = blockColors[8][10]; 
     c2[48] = blockColors[8][11]; 
     c2[49] = blockColors[8][12]; 

     for (int j = 0; j < c2.length/2; j++) 
     { 
     double shadowRed = 0.892D * c2[j].getRed() + 0.5D; 
     double shadowGreen = 0.892D * c2[j].getGreen() + 0.5D; 
     double shadowBlue = 0.892D * c2[j].getBlue() + 0.5D; 

     c2[(50 + j)] = new Color((int)shadowRed, (int)shadowGreen, (int)shadowBlue); 
     } 
     for (int j = 0; j < c2.length; j++) { 
     System.out.println("colors[" + j + "] = new Color(" + c2[j].getRed() + "," + c2[j].getGreen() + "," + c2[j].getBlue() + ");"); 
     } 
     b.setColors(c2); 
     System.out.println("Done"); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    b.repaint(); 
    } 
} 

ImageEditor:

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Image; 
import java.awt.MediaTracker; 
import java.awt.image.MemoryImageSource; 
import java.awt.image.PixelGrabber; 
import java.io.PrintStream; 

public class ImageEditor 
{ 
    public static int[] returnBuffer(Image i, Component c) 
    { 
    MediaTracker tracker = new MediaTracker(c); 
    tracker.addImage(i, 0); 
    try 
    { 
     tracker.waitForAll(); 
    } 
    catch (Exception e) 
    { 
     System.out.println("Image loading interrupted"); 
    } 
    int width = i.getWidth(c); 
    int height = i.getHeight(c); 

    int[] buffer = new int[width * height]; 

    PixelGrabber grabber = new PixelGrabber(i, 0, 0, width, height, buffer, 0, width); 
    try 
    { 
     grabber.grabPixels(); 
    } 
    catch (InterruptedException e) 
    { 
     e.printStackTrace(); 
    } 
    return buffer; 
    } 

    public static Image simplifyColors(int width, int height, int[] buffer, Component c, Color[] colors) 
    { 
    double[] w = new double[colors.length]; 
    for (int k = 0; k < w.length; k++) { 
     w[k] = 1.0D; 
    } 
    return simplifyColors(width, height, buffer, c, colors, w); 
    } 

    public static Image simplifyColors(int width, int height, int[] buffer, Component c, Color[] colors, double[] weights) 
    { 
    int[] simple = new int[buffer.length]; 
    for (int j = 0; j < buffer.length; j++) 
    { 
     int minDiff = 10000000; 
     Color current = new Color(buffer[j], true); 
     Color col = Color.black; 
     for (int k = 0; k < colors.length; k++) 
     { 
     Color test = colors[k]; 
     double w = weights[k]; 
     if (test != null) 
     { 
      int diff = (int)((Math.pow(test.getRed() - current.getRed(), 2.0D) + Math.pow(test.getGreen() - current.getGreen(), 2.0D) + Math.pow(test.getBlue() - current.getBlue(), 2.0D))/w); 
      if (diff < minDiff) 
      { 
      col = test; 
      minDiff = diff; 
      } 
     } 
     } 
     if (current.getAlpha() >= 128) { 
     simple[j] = col.getRGB(); 
     } else { 
     simple[j] = new Color(255, 255, 255, 0).getRGB(); 
     } 
    } 
    return c.createImage(new MemoryImageSource(width, height, simple, 0, width)); 
    } 

    public static Image simplifyColors2(Image i, int[] buffer, Component c, Color[] colors) 
    { 
    double[] w = new double[colors.length]; 
    for (int k = 0; k < w.length; k++) { 
     w[k] = 1.0D; 
    } 
    return simplifyColors2(i, buffer, c, colors, w); 
    } 

    public static Image simplifyColors2(Image i, int[] buffer, Component c, Color[] colors, double[] weights) 
    { 
    int height = i.getHeight(c); 
    int width = i.getWidth(c); 
    int[] simple = new int[buffer.length]; 
    float[] hsb1 = new float[3]; 
    float[] hsb2 = new float[3]; 
    for (int j = 0; j < buffer.length; j++) 
    { 
     int minDiff = 10000000; 
     Color current = new Color(buffer[j], true); 
     hsb1 = Color.RGBtoHSB(current.getRed(), current.getGreen(), current.getBlue(), null); 
     Color col = Color.black; 
     for (int k = 0; k < colors.length; k++) 
     { 
     Color test = colors[k]; 
     if (test != null) 
     { 
      hsb2 = Color.RGBtoHSB(test.getRed(), test.getGreen(), test.getBlue(), null); 
      int diff = (int)(Math.pow(hsb1[0] - hsb2[0], 2.0D) + Math.pow(hsb1[1] - hsb2[1], 2.0D) + Math.pow(hsb1[2] - hsb2[2], 2.0D)); 
      if (diff < minDiff) 
      { 
      col = test; 
      minDiff = diff; 
      } 
     } 
     } 
     if (current.getAlpha() >= 128) { 
     simple[j] = col.getRGB(); 
     } else { 
     simple[j] = new Color(255, 255, 255, 0).getRGB(); 
     } 
    } 
    return c.createImage(new MemoryImageSource(width, height, simple, 0, width)); 
    } 

    public static Image simplifyColors3(int width, int height, int[] buffer2, Component c, Color[] colors, double[] weights) 
    { 
    int[] buffer = (int[])buffer2.clone(); 
    int[] simple = new int[buffer.length]; 
    for (int j = 0; j < buffer.length; j++) 
    { 
     int minDiff = 10000000; 
     Color current = new Color(buffer[j], true); 
     Color col = Color.black; 
     for (int k = 0; k < colors.length; k++) 
     { 
     if (current.getAlpha() == 0) 
     { 
      col = new Color(255, 255, 255, 0); 
      break; 
     } 
     Color test = colors[k]; 
     double w = weights[k]; 
     if (test != null) 
     { 
      int diff = (int)((Math.pow(test.getRed() - current.getRed(), 2.0D) + Math.pow(test.getGreen() - current.getGreen(), 2.0D) + Math.pow(test.getBlue() - current.getBlue(), 2.0D))/w); 
      if (diff < minDiff) 
      { 
      col = test; 
      minDiff = diff; 
      } 
     } 
     } 
     int quantErrorR = current.getRed() - col.getRed(); 
     int quantErrorG = current.getGreen() - col.getGreen(); 
     int quantErrorB = current.getBlue() - col.getBlue(); 
     if ((j + 1) % width != 0) 
     { 
     Color x = new Color(buffer[(j + 1)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 7/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 7/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 7/16, 255)), x.getAlpha()); 
     buffer[(j + 1)] = y.getRGB(); 
     } 
     if (((j - 1) % width != 0) && (j - 1 + width < buffer.length)) 
     { 
     Color x = new Color(buffer[(j - 1 + width)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 3/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 3/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 3/16, 255)), x.getAlpha()); 
     buffer[(j - 1 + width)] = y.getRGB(); 
     } 
     if (j + width < buffer.length) 
     { 
     Color x = new Color(buffer[(j + width)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 5/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 5/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 5/16, 255)), x.getAlpha()); 
     buffer[(j + width)] = y.getRGB(); 
     } 
     if (((j + 1) % width != 0) && (j + 1 + width < buffer.length)) 
     { 
     Color x = new Color(buffer[(j + 1 + width)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 1/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 1/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 1/16, 255)), x.getAlpha()); 
     buffer[(j + 1 + width)] = y.getRGB(); 
     } 
     if (current.getAlpha() >= 128) { 
     simple[j] = col.getRGB(); 
     } else { 
     simple[j] = new Color(255, 255, 255, 0).getRGB(); 
     } 
    } 
    return c.createImage(new MemoryImageSource(width, height, simple, 0, width)); 
    } 

    public static Image shadowColors(int width, int height, int[] buffer, Component c, Color[] colors, double[] weights) 
    { 
    int[] simple = new int[buffer.length]; 
    double k2 = 0.0D; 
    for (int j = 0; j < buffer.length; j++) 
    { 
     int minDiff = 10000000; 
     Color current = new Color(buffer[j], true); 
     Color col = Color.black; 
     for (int k = 0; k < colors.length; k++) 
     { 
     Color test = colors[k]; 
     double w = weights[k]; 
     if (test != null) 
     { 
      int diff = (int)((Math.pow(test.getRed() - current.getRed(), 2.0D) + Math.pow(test.getGreen() - current.getGreen(), 2.0D) + Math.pow(test.getBlue() - current.getBlue(), 2.0D))/w); 
      if (diff < minDiff) 
      { 
      col = test; 
      k2 = k; 
      minDiff = diff; 
      } 
     } 
     } 
     if (current.getAlpha() >= 128) 
     { 
     if (k2 < colors.length/2) { 
      simple[j] = Color.WHITE.getRGB(); 
     } else { 
      simple[j] = Color.BLACK.getRGB(); 
     } 
     } 
     else { 
     simple[j] = new Color(255, 255, 255, 0).getRGB(); 
     } 
    } 
    return c.createImage(new MemoryImageSource(width, height, simple, 0, width)); 
    } 

    public static Image shadowColors3(int width, int height, int[] buffer2, Component c, Color[] colors, double[] weights) 
    { 
    int[] buffer = (int[])buffer2.clone(); 
    int[] simple = new int[buffer.length]; 

    double k2 = 0.0D; 
    for (int j = 0; j < buffer.length; j++) 
    { 
     int minDiff = 10000000; 
     Color current = new Color(buffer[j], true); 
     Color col = Color.black; 
     for (int k = 0; k < colors.length; k++) 
     { 
     if (current.getAlpha() == 0) 
     { 
      col = new Color(255, 255, 255, 0); 
      break; 
     } 
     Color test = colors[k]; 
     double w = weights[k]; 
     if (test != null) 
     { 
      int diff = (int)((Math.pow(test.getRed() - current.getRed(), 2.0D) + Math.pow(test.getGreen() - current.getGreen(), 2.0D) + Math.pow(test.getBlue() - current.getBlue(), 2.0D))/w); 
      if (diff < minDiff) 
      { 
      col = test; 
      minDiff = diff; 
      k2 = k; 
      } 
     } 
     } 
     int quantErrorR = current.getRed() - col.getRed(); 
     int quantErrorG = current.getGreen() - col.getGreen(); 
     int quantErrorB = current.getBlue() - col.getBlue(); 
     if ((j + 1) % width != 0) 
     { 
     Color x = new Color(buffer[(j + 1)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 7/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 7/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 7/16, 255)), x.getAlpha()); 
     buffer[(j + 1)] = y.getRGB(); 
     } 
     if (((j - 1) % width != 0) && (j - 1 + width < buffer.length)) 
     { 
     Color x = new Color(buffer[(j - 1 + width)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 3/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 3/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 3/16, 255)), x.getAlpha()); 
     buffer[(j - 1 + width)] = y.getRGB(); 
     } 
     if (j + width < buffer.length) 
     { 
     Color x = new Color(buffer[(j + width)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 5/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 5/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 5/16, 255)), x.getAlpha()); 
     buffer[(j + width)] = y.getRGB(); 
     } 
     if (((j + 1) % width != 0) && (j + 1 + width < buffer.length)) 
     { 
     Color x = new Color(buffer[(j + 1 + width)], true); 
     Color y = new Color(Math.max(0, Math.min(x.getRed() + col.getAlpha()/255 * quantErrorR * 1/16, 255)), Math.max(0, Math.min(x.getGreen() + col.getAlpha()/255 * quantErrorG * 1/16, 255)), Math.max(0, Math.min(x.getBlue() + col.getAlpha()/255 * quantErrorB * 1/16, 255)), x.getAlpha()); 
     buffer[(j + 1 + width)] = y.getRGB(); 
     } 
     if (current.getAlpha() >= 128) 
     { 
     if (k2 < colors.length/2) { 
      simple[j] = Color.WHITE.getRGB(); 
     } else { 
      simple[j] = Color.BLACK.getRGB(); 
     } 
     } 
     else { 
     simple[j] = new Color(255, 255, 255, 0).getRGB(); 
     } 
    } 
    return c.createImage(new MemoryImageSource(width, height, simple, 0, width)); 
    } 

    public static Image resize(Image i, int[] buffer, Component c, int newDim) 
    { 
    int h = i.getHeight(c); 
    int w = i.getWidth(c); 
    if (w < h) { 
     return resize(i, buffer, c, newDim, (int)(newDim * 1.0D * w/h)); 
    } 
    return resize(i, buffer, c, (int)(newDim * 1.0D * h/w), newDim); 
    } 

    public static int getResizedHeight(Image i, Component c, int newDim) 
    { 
    int h = i.getHeight(c); 
    int w = i.getWidth(c); 
    if (w < h) { 
     return newDim; 
    } 
    return (int)(newDim * 1.0D * h/w); 
    } 

    public static int getResizedWidth(Image i, Component c, int newDim) 
    { 
    int h = i.getHeight(c); 
    int w = i.getWidth(c); 
    if (w < h) { 
     return (int)(newDim * 1.0D * w/h); 
    } 
    return newDim; 
    } 

    public static int[] resizebuff(Image i, int[] buffer, Component c, int newDim) 
    { 
    int h = i.getHeight(c); 
    int w = i.getWidth(c); 
    if (w < h) { 
     return resizebuff(i, buffer, c, newDim, (int)(newDim * 1.0D * w/h)); 
    } 
    return resizebuff(i, buffer, c, (int)(newDim * 1.0D * h/w), newDim); 
    } 

    public static Image resize2(Image i, int[] buffer, Component c, int newDim) 
    { 
    int h = i.getHeight(c); 
    int w = i.getWidth(c); 
    if (w < h) { 
     return resize2(i, buffer, c, newDim, (int)(newDim * 1.0D * w/h)); 
    } 
    return resize2(i, buffer, c, (int)(newDim * 1.0D * h/w), newDim); 
    } 

    public static int[] resize2buff(Image i, int[] buffer, Component c, int newDim) 
    { 
    int h = i.getHeight(c); 
    int w = i.getWidth(c); 
    if (w < h) { 
     return resize2buff(i, buffer, c, newDim, (int)(newDim * 1.0D * w/h)); 
    } 
    return resize2buff(i, buffer, c, (int)(newDim * 1.0D * h/w), newDim); 
    } 

    public static Image resize(Image i, int[] buffer, Component c, int newHeight, int newWidth) 
    { 
    if (newHeight < 2) { 
     newHeight = 2; 
    } 
    if (newWidth < 2) { 
     newWidth = 2; 
    } 
    int height = i.getHeight(c); 
    int width = i.getWidth(c); 
    if ((height == newHeight) && (width == newWidth)) { 
     return i; 
    } 
    int[] resized = new int[newHeight * newWidth]; 

    double hRatio = newHeight/height; 
    double wRatio = newWidth/width; 
    for (int y = 0; y < newHeight; y++) { 
     for (int x = 0; x < newWidth; x++) 
     { 
     int oldX = (int)(x/wRatio); 
     int oldY = (int)(y/hRatio); 

     resized[(y * newWidth + x)] = buffer[(oldY * width + oldX)]; 
     } 
    } 
    return c.createImage(new MemoryImageSource(newWidth, newHeight, resized, 0, newWidth)); 
    } 

    public static int[] resizebuff(Image i, int[] buffer, Component c, int newHeight, int newWidth) 
    { 
    if (newHeight < 2) { 
     newHeight = 2; 
    } 
    if (newWidth < 2) { 
     newWidth = 2; 
    } 
    int height = i.getHeight(c); 
    int width = i.getWidth(c); 
    if ((height == newHeight) && (width == newWidth)) { 
     return buffer; 
    } 
    int[] resized = new int[newHeight * newWidth]; 

    double hRatio = newHeight/height; 
    double wRatio = newWidth/width; 
    for (int y = 0; y < newHeight; y++) { 
     for (int x = 0; x < newWidth; x++) 
     { 
     int oldX = (int)(x/wRatio); 
     int oldY = (int)(y/hRatio); 

     resized[(y * newWidth + x)] = buffer[(oldY * width + oldX)]; 
     } 
    } 
    return resized; 
    } 

    public static Image resize2(Image i, int[] buffer, Component c, int newHeight, int newWidth) 
    { 
    if (newHeight < 2) { 
     newHeight = 2; 
    } 
    if (newWidth < 2) { 
     newWidth = 2; 
    } 
    int height = i.getHeight(c); 
    int width = i.getWidth(c); 
    if ((height == newHeight) && (width == newWidth)) { 
     return i; 
    } 
    int[] resized = new int[newHeight * newWidth]; 

    double hRatio = newHeight/height; 
    double wRatio = newWidth/width; 
    if ((hRatio > 1.0D) || (wRatio > 1.0D)) { 
     return resize(i, buffer, c, newHeight, newWidth); 
    } 
    for (int y = 0; y < newHeight; y++) { 
     for (int x = 0; x < newWidth; x++) 
     { 
     int k = 0; 
     double oldC = 0.0D; 
     double oldRed = 0.0D; 
     double oldGreen = 0.0D; 
     double oldBlue = 0.0D; 
     double oldAlpha = 0.0D; 
     for (int q = (int)(x/wRatio); q < (int)((x + 1)/wRatio); q++) { 
      for (int j = (int)(y/hRatio); j < (int)((y + 1)/hRatio); j++) 
      { 
      Color oldColor = new Color(buffer[(j * width + q)], true); 
      oldRed = (oldRed * k + oldColor.getRed())/(k + 1); 
      oldGreen = (oldGreen * k + oldColor.getGreen())/(k + 1); 
      oldBlue = (oldBlue * k + oldColor.getBlue())/(k + 1); 
      oldAlpha = (oldAlpha * k + oldColor.getAlpha())/(k + 1); 

      k++; 
      } 
     } 
     resized[(y * newWidth + x)] = new Color((int)oldRed, (int)oldGreen, (int)oldBlue, (int)oldAlpha).getRGB(); 
     } 
    } 
    return c.createImage(new MemoryImageSource(newWidth, newHeight, resized, 0, newWidth)); 
    } 

    public static int[] resize2buff(Image i, int[] buffer, Component c, int newHeight, int newWidth) 
    { 
    if (newHeight < 2) { 
     newHeight = 2; 
    } 
    if (newWidth < 2) { 
     newWidth = 2; 
    } 
    int height = i.getHeight(c); 
    int width = i.getWidth(c); 
    if ((height == newHeight) && (width == newWidth)) { 
     return buffer; 
    } 
    int[] resized = new int[newHeight * newWidth]; 

    double hRatio = newHeight/height; 
    double wRatio = newWidth/width; 
    if ((hRatio > 1.0D) || (wRatio > 1.0D)) { 
     return resizebuff(i, buffer, c, newHeight, newWidth); 
    } 
    for (int y = 0; y < newHeight; y++) { 
     for (int x = 0; x < newWidth; x++) 
     { 
     int k = 0; 
     double oldC = 0.0D; 
     double oldRed = 0.0D; 
     double oldGreen = 0.0D; 
     double oldBlue = 0.0D; 
     double oldAlpha = 0.0D; 
     for (int q = (int)(x/wRatio); q < (int)((x + 1)/wRatio); q++) { 
      for (int j = (int)(y/hRatio); j < (int)((y + 1)/hRatio); j++) 
      { 
      Color oldColor = new Color(buffer[(j * width + q)], true); 
      oldRed = (oldRed * k + oldColor.getRed())/(k + 1); 
      oldGreen = (oldGreen * k + oldColor.getGreen())/(k + 1); 
      oldBlue = (oldBlue * k + oldColor.getBlue())/(k + 1); 
      oldAlpha = (oldAlpha * k + oldColor.getAlpha())/(k + 1); 

      k++; 
      } 
     } 
     resized[(y * newWidth + x)] = new Color((int)oldRed, (int)oldGreen, (int)oldBlue, (int)oldAlpha).getRGB(); 
     } 
    } 
    return resized; 
    } 

    public static int[] countColors(int[] buffer, Component c, Color[] colors) 
    { 
    int[] count = new int[colors.length]; 
    for (int k = 0; k < count.length; k++) { 
     count[k] = 0; 
    } 
    Color col = Color.BLACK; 
    for (int j = 0; j < buffer.length; j++) 
    { 
     col = new Color(buffer[j]); 
     for (int k = 0; k < colors.length; k++) { 
     if ((colors[k] != null) && (colors[k].getRGB() == col.getRGB())) { 
      count[k] += 1; 
     } 
     } 
    } 
    return count; 
    } 

(已有把它關掉字符限制的b/c)

編輯:原來第一個錯誤並不重要。該程序嘗試從單獨的文件運行紋理。如果找不到,則跳過它。

任何幫助表示讚賞。

+0

修復查找文件和其他可能的工作 –

回答

0

解決您的第二個問題與堆棧跟蹤:

這行是你的問題: resized[(y * newWidth + x)] = buffer[(oldY * width + oldX)];

具體這部分是因爲resized[(y * newWidth + x)]可以[y * newWidth + x]遠比什麼resized允許更大。


假設圖像是100x50(寬x高),這意味着int[] resized = new int[newHeight * newWidth];將創建一個新的數組,它是5000長。

然而for循環會創造一些高得多的:在這種情況下y

for (int y = 0; y < newHeight; y++)會高達newHeight或100

VFOR(INT X = 0; X < newWidth; X ++)in this instanceÿ will go as high as newWidth`或50

所以:

resized[(y * newWidth + x)] = something;會因爲問題可以大到10050,也就是遠大於5000.

解決方法是製作一個較大的resized數組,或重新考慮for循環。