1

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.

10
  • Variable names which differ only by a number mean that you should use an array or List. Please look for tutorials about these fundamental programming concepts. Commented Oct 22, 2016 at 10:12
  • Also this is not an android question since you are using JPanel. Commented Oct 22, 2016 at 10:14
  • @Code-Apprentice I'm not using JPanel in new code(Code bellow), JPanel code is from simple Java coding in eclipse Commented Oct 22, 2016 at 10:59
  • I do not understand what you need help with. What is your question? Part of the difficult is that there is to much code here. Can you reduce it to a very simple example? Commented Oct 22, 2016 at 11:36
  • @Code-Apprentice Ok, I will do It now. Problem is there, that with new cod e (BoxHolder) not (BoxesHolder) wont work this " Box.boxes[1] = Box.boxes[1] + 1;" and I'm trying to to that with "Box.AccessoriesBoxOne.count = +1;" but it still don't work. Commented Oct 22, 2016 at 12:18

1 Answer 1

2

So, this code is of poor quality and hard to understand.

  1. Read about DRY, loops, arrays and collections.
  2. You should also use naming convention.
  3. Use meaningful names: k, k1, a, a2 - what it means?
  4. Use private fields and getters/setters.

If you can, use primitive int array, instead of Integer array:

int[] boxes = new int[20]; and then Box.boxes[1]++ (incrementation)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but if I understood right, these points are about "Simple Java Code" (I wont be working anymore with it), I need to know how to increment in Android code (Listed Bellow in topic).
And How I said Box.boxes[1] = Box.boxes[1] + 1 . Was working in simple Java, I just don't want to use that code.
Are you trying to use BoxHolder (last code in your question) in Android?
You have main() method, which in Android is not used. Do you have some Activity class and layout file in your Android project?
Yes, i have. I have mainActivity with activity_main.xml
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.