0

Hello I have to finish a project but I can't get further. The project is about having jlabels with icons generated dynamically on two sides (left and right) .

The icons are in different order on each side and there is a one to one icon relationship.

As an example one picture : link

Now you can draw lines from the left to the right between those pictures and after clicking on the check button it should tell you if its true or false :

Again one picture : link

My question is how can I compare the icons to know which combination is the right one and after comparing the drawings?

I already created two lists to know the component combination of the drawings.

Here is the code so far -

   import java.awt.BasicStroke;
   import java.awt.Color;
   import java.awt.Component;
   import java.awt.Graphics;
   import java.awt.Graphics2D;
   import java.awt.Point;
   import java.awt.Shape;
   import java.awt.geom.Line2D;
   import java.util.ArrayList;
   import java.util.List;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JButton;
   import java.awt.event.ActionListener;
   import java.awt.event.ActionEvent;
   import javax.swing.SwingConstants;
   import javax.swing.ImageIcon;
public class CatSameCoins{

 public static JLabel label;
 public ActionListener btn1Listener;
 public ActionListener btn2Listener;
 public ActionListener btn3Listener;
 public ActionListener btn4Listener;
 public ActionListener btn5Listener;
 public ActionListener btn6Listener;
 public JButton btn1;
 public JButton btn2;
 public JButton btn3;
 public JButton btn4;
 public JButton btn5;
 public JButton btn6;
 public JButton btnCheck;
 public ArrayList<String> listto = new ArrayList<String>();
 public ArrayList<String> listfrom = new ArrayList<String>();
 public static boolean drawing = false;
 public static List<Shape> shapes = new ArrayList<Shape> ();
 public static Shape currentShape = null;
 static Component fromComponent;
 private JLabel lblCheck;

 public void drawLine(Component from , Component to){
        listfrom.add(from.getName()); //first list with component names from where the drawing start
        listto.add(to.getName());    //second list with component names where the drawing ends

        Point fromPoint = new Point();
        Point toPoint = new Point();

        fromPoint.x = from.getX()+from.getWidth()/2; //get middle 
        fromPoint.y = from.getY()+from.getHeight()/2; //get middle

        toPoint.x = to.getX()+to.getWidth()/2;
        toPoint.y = to.getY()+to.getHeight()/2;

        currentShape = new Line2D.Double (fromPoint, toPoint);
        shapes.add (currentShape);
        label.repaint();
        drawing = false;
 }

 public CatSameCoins(){
    JFrame frame = new JFrame ();
    frame.getContentPane().setLayout(null);

    JButton btnremove = new JButton("remove-drawings");
    btnremove.setBounds(139, 39, 216, 23);
    btnremove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            shapes.removeAll(shapes);
            listfrom.removeAll(listfrom);
            drawing= false;
            btn1.addActionListener(btn1Listener);
            btn2.addActionListener(btn2Listener);
            btn3.addActionListener(btn3Listener);
            btn4.addActionListener(btn4Listener);
            btn5.addActionListener(btn5Listener);
            btn6.addActionListener(btn6Listener);    
        }
     });
    frame.getContentPane().add(btnremove);

    btn1 = new JButton("1");
    btn1.setName("btn1");
    btn1.setBounds(21, 88, 45, 97);
    btn1Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawing = true;
            fromComponent = btn1;
            btn1.removeActionListener(this);
        }
    };
    btn1.addActionListener(btn1Listener);
    frame.getContentPane().add(btn1);

    btn2 = new JButton("2");
    btn2.setName("btn2");
    btn2Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawing = true;
            fromComponent = btn2;
            btn2.removeActionListener(this);
        }
    };
    btn2.addActionListener(btn2Listener);
    btn2.setBounds(21, 196, 45, 97);
    frame.getContentPane().add(btn2);

    btn3 = new JButton("3");
    btn3.setName("btn3");
    btn3Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawing = true;
            fromComponent = btn3;
            btn3.removeActionListener(this);
        }
    };
    btn3.addActionListener(btn3Listener);
    btn3.setBounds(21, 307, 45, 97);
    frame.getContentPane().add(btn3);

    btn4 = new JButton("4");
    btn4.setName("btn4");
    btn4Listener = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             if(drawing == true){
                 drawLine(fromComponent,btn4);
                 drawing = false;
                 btn4.removeActionListener(this);
                }   
            }
        };
    btn4.addActionListener(btn4Listener);
    btn4.setBounds(407, 88, 45, 97);
    frame.getContentPane().add(btn4);

    btn5 = new JButton("5");
    btn5.setName("btn5");
    btn5Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(drawing == true){
                drawLine(fromComponent,btn5);
                drawing = false;
                btn5.removeActionListener(this);
            }
        }
    };
    btn5.addActionListener(btn5Listener);
    btn5.setBounds(407, 202, 45, 91);
    frame.getContentPane().add(btn5);

    btn6 = new JButton("6");
    btn6.setName("btn6");
    btn6.setBounds(407, 307, 45, 91);
    btn6Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(drawing == true){
                drawLine(fromComponent,btn6);
                drawing = false;
                btn6.removeActionListener(this);
            }
        }

    };
    btn6.addActionListener(btn6Listener);    
    frame.getContentPane().add(btn6);

    btnCheck = new JButton("check");
    btnCheck.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i< listfrom.size();i++){
                System.out.println(listfrom.get(i)+" - "+listto.get(i));
            }
            lblCheck.setText("False");
            //if().... lblCheck.setText("True")
            //else lblCheck.setText("False")

            /*if(btn6.getIcon().toString().equals(btn1.getIcon().toString())){
                System.out.println("yes");
            }
            else{
                System.out.println("neah");
                }*/
        }
    });
    btnCheck.setBounds(171, 405, 143, 46);
    frame.getContentPane().add(btnCheck);

    lblCheck = new JLabel("");
    lblCheck.setHorizontalAlignment(SwingConstants.CENTER);
    lblCheck.setBounds(84, 405, 80, 46);
    frame.getContentPane().add(lblCheck);

    label = new JLabel (){
        protected void paintComponent ( Graphics g )
        {
        Graphics2D g2d = ( Graphics2D ) g;
        g2d.setPaint ( Color.black);
        g2d.setStroke(new BasicStroke(5));
        for ( Shape shape : shapes )
        {
            g2d.draw ( shape );
            repaint();
        }
        }
    } ;
   label.setSize(500,500);
   frame.getContentPane().add (label);

   frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
   frame.setSize ( 500, 500 );
   frame.setLocationRelativeTo ( null );
   frame.setVisible ( true );
 }

public static void main ( String[] args )
{
   new CatSameCoins();
}

}

Would really appreciate for any help ;) .

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • You would (compare) the images. You would use some kind of List, which contained, in order the same images. When you display the icons, one from each side, you would maintain information about the index of the displayed images, allowing you to map left to right and back again – MadProgrammer Feb 08 '15 at 07:20
  • 1) `protected void paintComponent ( Graphics g ) {` should be `protected void paintComponent ( Graphics g ) { super.paintComponent(g);` 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). ... – Andrew Thompson Feb 08 '15 at 07:40
  • *"The frame needs to be created with the absolute layout .."* No it doesn't. *"however its not a big deal i just use eclipse's windowbuilder"* LOL! This **entire question** would suggest otherwise. ;) – Andrew Thompson Feb 08 '15 at 12:21
  • thanks for this , i will try and post afterwards my solution – Predatorliner Feb 08 '15 at 12:32

1 Answers1

0

Its done, Solution : you can try it just change icon names for the second hashmap I know this is not the best solution but at least i tried. I will try to improve it.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

public class Hashmap {
    private JLabel label;
    private ActionListener btn1Listener;
    private ActionListener btn2Listener;
    private ActionListener btn3Listener;
    private ActionListener btn4Listener;
    private ActionListener btn5Listener;
    private ActionListener btn6Listener;
    private JButton btn1;
    private JButton btn2;
    private JButton btn3;
    private JButton btn4;
    private JButton btn5;
    private JButton btn6;
    private JButton btnCheck;
    private JButton btnNext;
    private boolean drawing = false;
    private Shape currentShape = null;
    private Component fromComponent;
    private JLabel lblCheck;
    private List<Shape> shapes = new ArrayList<Shape>();
    private HashMap<String, String> userLines = new HashMap<String, String>();
    private HashMap<String, String> resultLines = new HashMap<String, String>();
    private HashMap<String, String> icons = new HashMap<String, String>();

public void drawLine(Component from, Component to) {
    userLines.put(from.getName(), to.getName());

    Point fromPoint = new Point();
    Point toPoint = new Point();

    fromPoint.x = from.getX() + from.getWidth() / 2; // get middle
    fromPoint.y = from.getY() + from.getHeight() / 2; // get middle

    toPoint.x = to.getX() + to.getWidth() / 2;
    toPoint.y = to.getY() + to.getHeight() / 2;

    currentShape = new Line2D.Double(fromPoint, toPoint);
    shapes.add(currentShape);
    label.repaint();
    drawing = false;
}

@SuppressWarnings("serial")
public Hashmap() {
    loadIcons();

    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);

    JButton btnremove = new JButton("remove-drawings");
    btnremove.setBounds(139, 39, 216, 23);
    btnremove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            shapes.removeAll(shapes);
            drawing = false;
            userLines.clear();
            btn1.addActionListener(btn1Listener);
            btn2.addActionListener(btn2Listener);
            btn3.addActionListener(btn3Listener);
            btn4.addActionListener(btn4Listener);
            btn5.addActionListener(btn5Listener);
            btn6.addActionListener(btn6Listener);
        }
    });
    frame.getContentPane().add(btnremove);

    btn1 = new JButton("1");
    btn1.setName("1");
    btn1Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawing = true;
            fromComponent = btn1;
        }
    };
    btn1.addActionListener(btn1Listener);
    btn1.setBounds(21, 88, 45, 97);
    frame.getContentPane().add(btn1);

    btn2 = new JButton("2");
    btn2.setName("2");
    btn2Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawing = true;
            fromComponent = btn2;
        }
    };
    btn2.addActionListener(btn2Listener);
    btn2.setBounds(21, 196, 45, 97);
    frame.getContentPane().add(btn2);

    btn3 = new JButton("3");
    btn3.setName("3");
    btn3Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawing = true;
            fromComponent = btn3;
        }
    };
    btn3.addActionListener(btn3Listener);
    btn3.setBounds(21, 307, 45, 97);
    frame.getContentPane().add(btn3);

    btn4 = new JButton("4");
    btn4.setName("4");
    btn4Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (drawing == true) {
                drawLine(fromComponent, btn4);
                drawing = false;
                btn4.removeActionListener(this);
                if (fromComponent.equals(btn1)) {
                    btn1.removeActionListener(btn1Listener);
                }
                if (fromComponent.equals(btn2)) {
                    btn2.removeActionListener(btn2Listener);
                }
                if (fromComponent.equals(btn3)) {
                    btn3.removeActionListener(btn3Listener);
                }
            }
        }
    };
    btn4.addActionListener(btn4Listener);
    btn4.setBounds(407, 88, 45, 97);
    frame.getContentPane().add(btn4);

    btn5 = new JButton("5");
    btn5.setName("5");
    btn5Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (drawing == true) {
                drawLine(fromComponent, btn5);
                drawing = false;
                btn5.removeActionListener(this);
                if (fromComponent.equals(btn1)) {
                    btn1.removeActionListener(btn1Listener);
                }
                if (fromComponent.equals(btn2)) {
                    btn2.removeActionListener(btn2Listener);
                }
                if (fromComponent.equals(btn3)) {
                    btn3.removeActionListener(btn3Listener);
                }
            }
        }
    };
    btn5.addActionListener(btn5Listener);
    btn5.setBounds(407, 202, 45, 91);
    frame.getContentPane().add(btn5);

    btn6 = new JButton("6");
    btn6.setName("6");
    btn6Listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (drawing == true) {
                drawLine(fromComponent, btn6);
                drawing = false;
                btn6.removeActionListener(this);
                if (fromComponent.equals(btn1)) {
                    btn1.removeActionListener(btn1Listener);
                }
                if (fromComponent.equals(btn2)) {
                    btn2.removeActionListener(btn2Listener);
                }
                if (fromComponent.equals(btn3)) {
                    btn3.removeActionListener(btn3Listener);
                }
            }
        }

    };
    btn6.addActionListener(btn6Listener);
    btn6.setBounds(407, 307, 45, 91);
    frame.getContentPane().add(btn6);

    btnCheck = new JButton("check");
    btnCheck.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (resultLines.equals(userLines) && !userLines.isEmpty()) {
                lblCheck.setText("true");
            } else {
                lblCheck.setText("false");
            }

        }
    });
    btnCheck.setBounds(171, 405, 143, 46);
    frame.getContentPane().add(btnCheck);

    btnNext = new JButton("next");
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            userLines.clear();
            resultLines.clear();
            shapes.removeAll(shapes);
            drawing = false;
            changeCombination();
            btn1.addActionListener(btn1Listener);
            btn2.addActionListener(btn2Listener);
            btn3.addActionListener(btn3Listener);
            btn4.addActionListener(btn4Listener);
            btn5.addActionListener(btn5Listener);
            btn6.addActionListener(btn6Listener);

        }
    });
    btnNext.setBounds(335, 435, 117, 25);
    frame.getContentPane().add(btnNext);

    lblCheck = new JLabel("");
    lblCheck.setHorizontalAlignment(SwingConstants.CENTER);
    lblCheck.setBounds(84, 405, 80, 46);
    frame.getContentPane().add(lblCheck);

    label = new JLabel() {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(Color.black);
            g2d.setStroke(new BasicStroke(3));
            for (Shape shape : shapes) {
                g2d.draw(shape);
                repaint();
            }
        }
    };
    label.setSize(frame.getWidth(), frame.getHeight());
    frame.getContentPane().add(label);
    frame.setVisible(true);
}

private void loadIcons() {
    icons.clear();
    icons.put("money/coin2/1_Cent.png", "money/coin2/1_Cent.png");
    icons.put("money/coin2/5_Cent.png", "money/coin2/5_Cent.png");
    icons.put("money/coin2/10_Cent.png", "money/coin2/10_Cent.png");
    icons.put("money/coin2/20_Cent.png", "money/coin2/20_Cent.png");
    icons.put("money/coin2/50_Cent.png", "money/coin2/50_Cent.png");
    icons.put("money/coin2/2_Euro.png", "money/coin2/2_Euro.png");
    icons.put("money/coin2/1_Euro.png", "money/coin2/1_Euro.png");
}

public void changeCombination() {
    Random randLeftSide = new Random();
    Random randRightSide = new Random();
    while (resultLines.size() < 3) {
        int left = randLeftSide.nextInt(3 - 1 + 1) + 1;
        int right = randRightSide.nextInt(6 - 4 + 1) + 4;
        // System.out.println("leftside: "+left+"rightside: "+y);
        while (resultLines.containsKey(String.valueOf(left))) {
            if (resultLines.size() > 3) {
                break;
            } else {
                left = randLeftSide.nextInt(3 - 1 + 1) + 1;
                // System.out.println("leftside : "+left);
            }
        }
        while (resultLines.containsValue(String.valueOf(right))) {
            if (resultLines.size() > 3) {
                break;
            } else {
                right = randRightSide.nextInt(6 - 4 + 1) + 4;
                // System.out.println("rightside: "+right);
            }
        }
        resultLines.put(String.valueOf(left), String.valueOf(right));
    }
    // System.out.println("TOTAL MAP: "+resultLines.toString());

    changeImages();

}

private void changeImages() {
    List<Integer> randomIcon = new ArrayList<Integer>(); //creating array list for the icon random numbers
    List<Integer> randomLines = new ArrayList<Integer>(); //creating array list for the lines random numbers
    for (int i = 0; i < resultLines.size(); i++) {
        int randomIndexIcon = new Random().nextInt(icons.size()); //generate random number for the icons from the hashmap

        while (randomIcon.contains(randomIndexIcon)) {  //while the list contains the random number generate a new one to prevent printing same pictures
                randomIndexIcon = new Random().nextInt(icons.size()); 
        }

        randomIcon.add(randomIndexIcon);  //add to the array list of the icon random numbers the generated random number

        int randomIndexResult = new Random().nextInt(resultLines.size()); //generate random number for the lines

        while (randomLines.contains(randomIndexResult)) {  //while for second list to prevent generating same numbers
                randomIndexResult = new Random().nextInt(resultLines.size());

        }
        randomLines.add(randomIndexResult); //add to the array list of the lines random numbers the generated random number

        Object iconValue = icons.values().toArray()[randomIndexIcon]; //this is to get the icon string

        Object valueLeft = resultLines.keySet().toArray()[randomIndexResult]; //this is to get the random key value from the resultLines map (left side)

        if (valueLeft.equals("1")) { //check conditions 
            btn1.setIcon(new ImageIcon(getClass().getResource(
                    iconValue.toString())));
        }
        if (valueLeft.equals("2")) {
            btn2.setIcon(new ImageIcon(getClass().getResource(
                    iconValue.toString())));
        }
        if (valueLeft.equals("3")) {
            btn3.setIcon(new ImageIcon(getClass().getResource(
                    iconValue.toString())));
        }

        Object valueRight = resultLines.values().toArray()[randomIndexResult];  //get the values from the hashmap (right side)

        if (valueRight.equals("4")) { //check conditions
            btn4.setIcon(new ImageIcon(getClass().getResource(
                    iconValue.toString())));
        }
        if (valueRight.equals("5")) {
            btn5.setIcon(new ImageIcon(getClass().getResource(
                    iconValue.toString())));
        }
        if (valueRight.equals("6")) {
            btn6.setIcon(new ImageIcon(getClass().getResource(
                    iconValue.toString())));
        }
    }
}

public static void main(String[] args) {
    new Hashmap();
}
}