I have written simple code in Java and now I'm learning Android and trying to convert my code.
What I'm trying to do are making objects with Image.
Got thousands of objects (separated in different classes like "boxes" "cars" "main" etc.)
And I want to add each object Image, so I could use it later, like for example. BMW_e30.Image();
I will try to show all my code readable and understandable, If it's not please comment and tell me how to improve my Code.
First I will show you my code how it was running in simple Java(All Java code worked, but it was messy and hard to understand)
BoxesHolder.class (Object Lists)
public class BoxesHolder {
public Integer boxes[] = new Integer[20];
public BoxesHolder(){
MakeBoxes();
}
public void BoxesMaker(int k, int a1, int k1, int a2){
boxes[a1] = k;
boxes[a2] = k1;
}
public void MakeBoxes(){
int i =0; int j =1;
BoxesMaker(10, i, 0, j); // Boxes Level 1
i +=2; j +=2;
BoxesMaker(20, i, 0, j); // Boxes Level 2
i +=2; j +=2;
BoxesMaker(30, i, 0, j); // Boxes Level 3
i +=2; j +=2;
BoxesMaker(40, i, 0, j); // Boxes Level 4
i +=2; j +=2;
BoxesMaker(50, i, 0, j); // Boxes Level 5
i +=2; j +=2;
BoxesMaker(60, i, 0, j); // Boxes Level 6
i +=2; j +=2;
BoxesMaker(70, i, 0, j); // Boxes Level 7
i +=2; j +=2;
BoxesMaker(80, i, 0, j); // Boxes Level 8
i +=2; j +=2;
BoxesMaker(90, i, 0, j); // Boxes Level 9
i +=2; j +=2;
BoxesMaker(100, i, 0, j); // Boxes Level 10
i +=2; j +=2;
}
}
ImagesHolder.class
public class ImagesHolder {
URL BoxLv1_2Icon = ImagesHolder.class.getResource("/BoxLv1_2.png");
URL BoxLv3_4Icon = ImagesHolder.class.getResource("/BoxLv3_4.png");
URL BoxLv5_6Icon = ImagesHolder.class.getResource("/BoxLv5_6.png");
URL BoxLv7_8Icon = ImagesHolder.class.getResource("/BoxLv7_8.png");
URL BoxLv9_10Icon = ImagesHolder.class.getResource("/BoxLv9_10.png");
}
Labels for Displaying Images in JPanel:
Labels.BoxesLVL1Label.setIcon(new ImageIcon(ImagesHolder.BoxLv1_2Icon));
Labels.BoxesLVL2Label.setIcon(new ImageIcon(ImagesHolder.BoxLv1_2Icon));
Labels.BoxesLVL3Label.setIcon(new ImageIcon(ImagesHolder.BoxLv3_4Icon));
Labels.BoxesLVL4Label.setIcon(new ImageIcon(ImagesHolder.BoxLv3_4Icon));
Labels.BoxesLVL5Label.setIcon(new ImageIcon(ImagesHolder.BoxLv5_6Icon));
Labels.BoxesLVL6Label.setIcon(new ImageIcon(ImagesHolder.BoxLv5_6Icon));
Labels.BoxesLVL7Label.setIcon(new ImageIcon(ImagesHolder.BoxLv7_8Icon));
Labels.BoxesLVL8Label.setIcon(new ImageIcon(ImagesHolder.BoxLv7_8Icon));
Labels.BoxesLVL9Label.setIcon(new ImageIcon(ImagesHolder.BoxLv9_10Icon));
Labels.BoxesLVL10Label.setIcon(new ImageIcon(ImagesHolder.BoxLv9_10Icon));
OnButtonClick(Function to add new Boxes)
if(ae.getSource() == jbtnBoxes){
CarMain.main[2] += 1;
CarMain.main[3] += 1;
Statistic.statistic[1] +=1;
Statistic.statistic[2] +=1;
if(CarMain.main[2] == 5){
CarMain.main[2] -= 5;
if(CarMain.main[4] == 1){
Box.boxes[1] = Box.boxes[1] + 1;
Statistic.statistic[13] +=1;
JOptionPane.showMessageDialog (null, "You have gained 1 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}
if(CarMain.main[4] == 2){
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(2)+1;
if (randomInt == 1){
Box.boxes[1] = Box.boxes[1] + 1;
Statistic.statistic[13] +=1;
JOptionPane.showMessageDialog (null, "You have gained 1 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}
if (randomInt == 2){
Box.boxes[3] = Box.boxes[3] + 1;
Statistic.statistic[14] +=1;
JOptionPane.showMessageDialog (null, "You have gained 2 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}
}
Lines for Adding values(Count) :
Box.boxes[1] = Box.boxes[1] + 1;
Box.boxes[3] = Box.boxes[3] + 1;
Line for Displaying Images:
JOptionPane.showMessageDialog (null, "You have gained 1 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}
And now what I'm trying to do in Android Studio
BoxHolder.class
package com.crelix.crelix;
public class BoxHolder {
int id;
String name;
int level;
int price;
int sellprice;
int count;
public void id(int id) {
}
public BoxHolder(String name) {
}
public void level(int level) {
}
public void price(int price) {
}
public void sellprice(int sellprice) {
}
public void count(int count) {
}
public static void main(String args[]) {
BoxHolder AccessoriesBoxOne = new BoxHolder("Accessories Pack LV-1");
BoxHolder AccessoriesBoxTwo = new BoxHolder("Accessories Pack LV-2");
BoxHolder AccessoriesBoxThree = new BoxHolder("Accessories Pack LV-3");
BoxHolder AccessoriesBoxFour = new BoxHolder("Accessories Pack LV-4");
BoxHolder AccessoriesBoxFive = new BoxHolder("Accessories Pack LV-5");
BoxHolder AccessoriesBoxSix = new BoxHolder("Accessories Pack LV-6");
BoxHolder AccessoriesBoxSeven = new BoxHolder("Accessories Pack LV-7");
BoxHolder AccessoriesBoxEight = new BoxHolder("Accessories Pack LV-8");
BoxHolder AccessoriesBoxNine = new BoxHolder("Accessories Pack LV-9");
BoxHolder AccessoriesBoxTen = new BoxHolder("Accessories Pack LV-10");
AccessoriesBoxOne.id(1);
AccessoriesBoxOne.level(1);
AccessoriesBoxOne.price(10);
AccessoriesBoxOne.sellprice(5);
AccessoriesBoxOne.count(0);
AccessoriesBoxTwo.id(2);
AccessoriesBoxTwo.level(2);
AccessoriesBoxTwo.price(20);
AccessoriesBoxTwo.sellprice(10);
AccessoriesBoxTwo.count(0);
AccessoriesBoxThree.id(3);
AccessoriesBoxThree.level(3);
AccessoriesBoxThree.price(30);
AccessoriesBoxThree.sellprice(15);
AccessoriesBoxThree.count(0);
AccessoriesBoxFour.id(4);
AccessoriesBoxFour.level(4);
AccessoriesBoxFour.price(40);
AccessoriesBoxFour.sellprice(20);
AccessoriesBoxFour.count(0);
AccessoriesBoxFive.id(5);
AccessoriesBoxFive.level(5);
AccessoriesBoxFive.price(50);
AccessoriesBoxFive.sellprice(25);
AccessoriesBoxFive.count(0);
AccessoriesBoxSix.id(6);
AccessoriesBoxSix.level(6);
AccessoriesBoxSix.price(60);
AccessoriesBoxSix.sellprice(30);
AccessoriesBoxSix.count(0);
AccessoriesBoxSeven.id(7);
AccessoriesBoxSeven.level(7);
AccessoriesBoxSeven.price(70);
AccessoriesBoxSeven.sellprice(35);
AccessoriesBoxSeven.count(0);
AccessoriesBoxEight.id(8);
AccessoriesBoxEight.level(8);
AccessoriesBoxEight.price(80);
AccessoriesBoxEight.sellprice(40);
AccessoriesBoxEight.count(0);
AccessoriesBoxNine.id(9);
AccessoriesBoxNine.level(9);
AccessoriesBoxNine.price(90);
AccessoriesBoxNine.sellprice(45);
AccessoriesBoxNine.count(0);
AccessoriesBoxTen.id(10);
AccessoriesBoxTen.level(10);
AccessoriesBoxTen.price(100);
AccessoriesBoxTen.sellprice(50);
AccessoriesBoxTen.count(0);
And I want to add Images like AccessoriesBoxOne.Image(ImageUrl); How can I do that?
How can I access objects now? Can't understand becouse these lines wont work anymore:
Box.boxes[1] = Box.boxes[1] + 1;
And I was thinking that I will do it in this way:
Box.AccessoriesBoxOne.count = +1;
But it doesn't work.
I hope that you will understand what I'm trying to say and trying to do, with hope, Crelix.