i have this piece of code
ImageIcon[] Image = {
new ImageIcon("../KingGame/src/game/img/1.gif"),
new ImageIcon("../KingGame/src/game/img/2.gif"),
new ImageIcon("../KingGame/src/game/img/3.gif"),
new ImageIcon("../KingGame/src/game/img/4.gif"),
new ImageIcon("../KingGame/src/game/img/5.gif"),
new ImageIcon("../KingGame/src/game/img/6.gif"),
new ImageIcon("../KingGame/src/game/img/7.gif"),
new ImageIcon("../KingGame/src/game/img/8.gif"),
new ImageIcon("../KingGame/src/game/img/9.gif"),
};
an d i tried with the code below replace the script above
ImageIcon image[] = new ImageIcon[9];
for (int i = 1; i < image.length; i++) {
new ImageIcon("../KingGame/src/game/img/"+i+".gif");
}
but the result is...any image is loaded. what is the error?
thanks
You forgot to put new images into array:
image[i] = new ImageIcon("../KingGame/src/game/img/"+i+".gif");
Now it does the same thing as your old code.
It should be
for (int i = 0; i < image.length; i++) {
image[i] =new ImageIcon("../KingGame/src/game/img/"+(i+1)+".gif");
}
Related
So Im doing JUnit Tests. While Im getting the parameters for my test method in a text document I counter an issue that is after the parameter object is not working well. All my parameter pass to the test method at once but I want them to pass separately.
Image below is what I want(Red line means the second set of parameters). The image is a sample run of my test method.
https://ibb.co/nFLhOc
This is my code:
String fileName = "ChargeData.txt";
try
{
scan = new Scanner(new File(fileName));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<Object[]> Params = new ArrayList<Object[]>();
List listOfLists = new ArrayList();
ArrayList <Integer> quantityList;
ArrayList <Boolean> highQualityList;
ArrayList <Boolean> designEffectList;
ArrayList <Double> expectedResultList;
while(scan.hasNext())
{
quantityList = new ArrayList<Integer>();
highQualityList = new ArrayList<Boolean>();
designEffectList = new ArrayList<Boolean>();
expectedResultList = new ArrayList<Double>();
while(scan.hasNextInt())
{
quantityList.add(new Integer(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
highQualityList.add(new Boolean(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
designEffectList.add(new Boolean(scan.next()));
}
expectedResultList.add(new Double(scan.next()));
int[] quantity = new int[quantityList.size()];
boolean[] highQuality = new boolean[quantityList.size()];
boolean[] designEffect = new boolean[quantityList.size()];
double[] expectedResult = new double[1];
for (int i=0; i < quantity.length; i++)
{
quantity[i] = quantityList.get(i).intValue();
highQuality[i] = highQualityList.get(i).booleanValue();
designEffect[i] = designEffectList.get(i).booleanValue();
}
expectedResult[0] = expectedResultList.get(0).doubleValue();
listOfLists.add(quantity);
listOfLists.add(highQuality);
listOfLists.add(designEffect);
listOfLists.add(expectedResult);
}
Params.add(listOfLists.toArray());
scan.close();
return Params.toArray();
}
Code Logic problem, just move the Params.add(listOfLists.toArray()); inside while loop fixed it.
while(scan.hasNext())
{
quantityList = new ArrayList<Integer>();
highQualityList = new ArrayList<Boolean>();
designEffectList = new ArrayList<Boolean>();
expectedResultList = new ArrayList<Double>();
while(scan.hasNextInt())
{
quantityList.add(new Integer(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
highQualityList.add(new Boolean(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
designEffectList.add(new Boolean(scan.next()));
}
expectedResultList.add(new Double(scan.next()));
int[] quantity = new int[quantityList.size()];
boolean[] highQuality = new boolean[quantityList.size()];
boolean[] designEffect = new boolean[quantityList.size()];
double[] expectedResult = new double[1];
for (int i=0; i < quantity.length; i++)
{
quantity[i] = quantityList.get(i).intValue();
highQuality[i] = highQualityList.get(i).booleanValue();
designEffect[i] = designEffectList.get(i).booleanValue();
}
expectedResult[0] = expectedResultList.get(0).doubleValue();
listOfLists.add(quantity);
listOfLists.add(highQuality);
listOfLists.add(designEffect);
listOfLists.add(expectedResult);
Params.add(listOfLists.toArray());
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Im getting this strange NullpointerException whilst adding a JLabel to a JPanel:
loadoutAdvWeaponPanels = new JPanel[4][4];
loadoutAdvWeaponButtons = new JButton[4];
loadoutAdvPistolLabels = new JLabel[4][8];
//Init loadoutAdvPanels[0]
loadoutAdvWeaponButtons[0] = new JButton("Pistols");
loadoutAdvPistolLabels[0][0] = new JLabel("USP-S");
loadoutAdvPistolLabels[0][1] = new JLabel("P2000");
loadoutAdvPistolLabels[0][2] = new JLabel("Dual Berettas");
loadoutAdvPistolLabels[0][3] = new JLabel("P250");
loadoutAdvPistolLabels[0][4] = new JLabel("Five-SeveN");
loadoutAdvPistolLabels[0][5] = new JLabel("CZ75-Auto");
loadoutAdvPistolLabels[0][6] = new JLabel("Desert Eagle");
loadoutAdvPistolLabels[0][7] = new JLabel("R8 Revolver");
loadoutAdvWeaponPanels[0][0].add(loadoutAdvPistolLabels[0][0]);
The error occurs in the last line, but i dont know why.
Simple as I see!
You do not initialisized loadoutAdvWeaponPanels[0][0]
I prefer to use this:
for(int i = 0; i < loadoutAdvWeaponPanels.length; i++) {
for(int j = 0; loadoutAdvWeaponPanels[i].length; j++) {
{
loadoutAdvWeaponPanels[i][j] = new JPanel();
}
}
you should initialize the array loadoutAdvWeaponPanels
loadoutAdvWeaponPanels = new JPanel[4][4];
for(int i = 0; i < loadoutAdvWeaponPanels.length; i++)
for(int j = 0; j < loadoutAdvWeaponPanels[i].length; j++)
loadoutAdvWeaponPanels[i][j] = new JPanel();
or just initialize what you need
loadoutAdvWeaponPanels[0][0] = new JPanel();
loadoutAdvWeaponPanels[0][0].add(loadoutAdvPistolLabels[0][0]);
How am I able to insert the i from the for loop in the imageview below, not the i to replace 12 in the file name so that it can become;
ImageView Moon_img12 = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/i.png")));
Code:
for(int i=0; i<=total_donnation; i++)
{
ImageView Moon_img12 = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/12.png")));
}
Try
ImageView[] moon_images = //init array
//loop
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/"+i+".png
moon_images[i]= Moon_img;
Thanks you all, i have solved it now using your help.
GridPane grid = new GridPane();
int max_columns = 5;
int current_row = 0;
int current_column = 0;
int total_donnation = 7;
for(int i=1; i<=total_donnation; i++)
{
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/"+i+".png")));
grid.add(Moon_img, current_column,current_row);
current_column = current_column+1;
if (current_column == max_columns )
{
current_row = current_row+1;
current_column = 0;
}
}
return grid;
I am trying to take multiple multi-page .tif files and combine them into a single multi-page tif file.
I found some code in this question, but it only seems to take the first page of each individual .tif file and create the new multi-page .tif with those first pages.
Is there a small change I'm not seeing that would cause this same code to grab every page from the source .tif files and put them all into the combined .tif?
To clarify, I would like the source files:
SourceA.tif (3 pages)
SourceB.tif (4 pages)
SourceC.tif (1 page)
to be combined into
combined.tif (8 pages)
I would also like to be able to specify a resolution and compression of the .tif, but I'm not sure if JAI supports that and it's not a necessity for a correct answer.
The code from the referenced question, modified by me to load all the .tif files in a directory, is below for easy answering:
public static void main(String[] args) {
String inputDir = "C:\\tifSources";
File sourceDirectory = new File(inputDir);
File file[] = sourceDirectory.listFiles();
int numImages = file.length;
BufferedImage image[] = new BufferedImage[numImages];
try
{
for (int i = 0; i < numImages; i++)
{
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(0), null, null, OpImage.OP_IO_BOUND);
image[i] = op.getAsBufferedImage();
}
TIFFEncodeParam params = new TIFFEncodeParam();
OutputStream out = new FileOutputStream(inputDir + "\\combined.tif");
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 0; i < numImages; i++)
{
imageList.add(image[i]);
}
params.setExtraImages(imageList.iterator());
encoder.encode(image[0]);
out.close();
}
catch (Exception e)
{
System.out.println("Exception " + e);
}
}
I knew I was just missing some little part about iterating over the pages in a single .tif, I just wasn't sure where it was.
More searching on the internet led me to find that rather than doing:
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(0), null, null, OpImage.OP_IO_BOUND);
I wanted to iterate over every page in the current document with something like:
int numPages = decoder.getNumPages();
for(int j = 0; j < numPages; j++)
{
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(j), null, null, OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
This adds every page of every .tif into the images List. One final trap was that the final call to
encoder.encode(images.get(0));
Would cause the first page to be in the new .tif twice, so I added an intermediate loop and List population that doesn't add the first page in the call to:
params.setExtraImages(imageList.iterator());
which keeps the first page out of the "ExtraImages" and it gets added with the call to encode.
Final updated code is:
public static void main(String[] args) {
String inputDir = "C:\\tifSources";
File faxSource = new File(inputDir);
File file[] = faxSource.listFiles();
System.out.println("files are " + Arrays.toString(file));
int numImages = file.length;
List<BufferedImage> images = new ArrayList<BufferedImage>();
try
{
for (int i = 0; i < numImages; i++)
{
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
int numPages = decoder.getNumPages();
for(int j = 0; j < numPages; j++)
{
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(j), null, null, OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
}
TIFFEncodeParam params = new TIFFEncodeParam();
OutputStream out = new FileOutputStream(inputDir + "\\combined.tif");
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 1; i < images.size(); i++)
{
imageList.add(images.get(i));
}
params.setExtraImages(imageList.iterator());
encoder.encode(images.get(0));
out.close();
}
catch (Exception e)
{
System.out.println("Exception " + e);
}
}
And now a very easy java question....
Object[] objectList={
new Object(name[0], description[0], R.drawable.creep_0),
new Object(name[1], description[1], R.drawable.creep_1),
new Object(name[2], description[2], R.drawable.creep_2),
new Object(name[3], description[3], R.drawable.creep_3),
};
How can i do this dynamically with a for cycle? Thanks!
Creep[] creeps = new Creep[]
{ R.drawable.creep_0, R.drawable.creep_1, R.drawable.creep_2,
R.drawable.creep_3 };
Object[] objectList = new Object[4];
for (int i = 0; i < 4; i++) {
objectList[i] = new Object(name[i], description[i], creeps[i]);
}
ArrayList objectList = new ArrayList();
for (int i = 0; i < 4; i++)
{
Object o = new Object(name[i], description[i], R.drawable.creep_i);
objectList.Add(o);
}