cześć.
Udało mi się rozwiązać ten problem z pomocą forumowicza z angielskiego forum. Poniżej przedstawię kod realizujący zadanie dzielenia obrazka na części(zamieściłem już całą klasę). Może wygląda trochę nieelegancko, ale działa.
import java.awt.;
import java.io.;
import javax.imageio.;
import java.awt.image.;
public class Dzielenie_obrazka
{
static Pomoce pomoce;
static int row;
static int col;
static Image image;
static int ile;
static int w;
static int h;
public Dzielenie_obrazka(Image img, int row, int col)
{
image = img;
this.row = row;
this.col = col;
ile = row * col;
}
private static void setWidthHight(int wi, int hi){
w = wi; h = hi;
}
private static BufferedImage readImage()
{
BufferedImage source = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
try{
File plik = new File("5.gif");
source = ImageIO.read(plik);
}catch(IOException e){
System.err.println("Błąd otwarcia pliku");
}
setWidthHight(source.getWidth(), source.getHeight());
return source;
}
private static BufferedImage[] createBufferImage(int w, int h, int ile)
{
BufferedImage bufImag[] = new BufferedImage[ile];
for(int i = 0; i < bufImag.length; i++)
{
bufImag[i] = new BufferedImage(w/row, h/col, BufferedImage.TYPE_INT_RGB); }
return bufImag;
}
// funkcja podaje odpowiednią tablicę podziału obrazka
private static int[][] getTabShare(int wiersz, int kolumna)
{
int tab[][] = new int[3][];
if(wiersz == kolumna)
{
if(wiersz == 2) tab = tab_2x2;
else if(wiersz == 3) tab = tab_3x3;
else if(wiersz == 4) tab = tab_4x4;
}
return tab;
}
public static BufferedImage[] split()
{
BufferedImage source = readImage();
BufferedImage tab[] = new BufferedImage[rowcol];
int [] rgbArray = new int[(w/row)(h/col)];
int tab_1x1[][] = {};
int tab_2x2[][] = {{0,0}, {w/2,0}, {0,h/2}, {w/2,h/2}};
int tab_3x3[][] = {{0,0}, {w/3,0}, {(2w/3),0},
{0,h/3}, {w/3,h/3}, {(2w/3),h/3},
{0,2h/3},{w/3,(2h/3)},{(2w/3),(2h/3)}
};
int tab_4x4[][] = {{0,0}, {w/4,0}, {w/2,0}, {(3w/4),0},
{0,h/4}, {w/4,h/4}, {w/2,h/4}, {(3w/4),h/4},
{0,h/2}, {w/4,h/2}, {w/2,h/2}, {(3w/4),h/2},
{0,(3h/4)},{w/4,(3h/4)}, {w/2,(3h/4)},{(3w/4),(3h/4)}
};
int tab_2x3[][] = {{0,0}, {0,h/3}, {0,2w/h},
{w/3,0},{w/3,h/3},{w/3,2h/3}
};
for(int i = 0; i < ile; i++)
{
try
{
BufferedImage bufImag = createBufferImage(w, h, ile)[i];
source.getRGB((getTabShare(row,col))[i][0], (getTabShare(row,col))[i][1], row, /col, rgbArray, 0, w/row);
bufImag.setRGB( 0, 0, w/row, h/col, rgbArray, 0, w/row);
tab[i] = bufImag;
}
catch(RasterFormatException e)
{
System.out.println(e.getMessage());
}
}
return tab;
}