我有一个方法,它接受一个字符串和一个整数作为参数。该方法基本上返回原始图像的较小裁剪部分。字符串是路径和我在switch语句中使用的整数,我需要的特定部分,我已经尝试打印输出但是无论出于什么原因x和y保持在0,即使我知道我得到的高度值和宽度。
private Icon extractIcon(String path, int i){
// reads the image into a BufferedImage object
BufferedImage image=null;
try{
image = ImageIO.read(new File(path));
}
catch(IOException e){
System.err.println("Image not found");
System.exit(1);
}
height = image.getHeight();
width = image.getWidth();
// allocates another BufferedImage object whose size is
// the same as the one of the wanted icon
BufferedImage part = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
switch(i){
case 1:
x = 0;
y = 0;
break;
case 2:
x = width*(1/3);
y = 0;
break;
case 3:
x = width*(2/3);
y = 0;
break;
case 4:
x = 0;
y = height*(1/3);
break;
case 5:
x=width*(1/3);
y=height*(1/3);
break;
case 6:
x=width*(2/3);
y=height*(1/3);
break;
case 7:
x = 0;
y=height*(2/3);
break;
case 8:
x = width*(1/3);
y = height*(2/3);
break;
}
//Intializes leftTopX and leftTopY for each button after switch
leftTopX = width*(1/3)+x;
leftTopY = height*(1/3)+y;
// copies the data from "image" to "part"
System.out.println(x + " xy " + y);
System.out.println(leftTopY + " leftxy " + leftTopX);
for(;x<width;x++){
for(;y<height;y++){
part.setRGB(x,y, image.getRGB(x+leftTopX, y+leftTopY));
}
}
// creates an icon whose content is already in "part"
ImageIcon icon = new ImageIcon();
icon.setImage(part);
// returns to the caller
return icon;
}
答案 0 :(得分:0)
您的交换机案例中缺少中断。
另请记住为您的帖子添加标签,用于您正在使用的语言。
答案 1 :(得分:0)
你做整数除法......
x = width*(1/3);
y = 0;
1/3
等于0
您应尝试使用float
或double
作为除数......
x = width*(1/3f);
或
x = (int)(width*(1/3.0));
例如......