Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查jlabel图标是否相同,并与绘图线的组件进行比较_Java_Swing_Awt_Jlabel - Fatal编程技术网

Java 检查jlabel图标是否相同,并与绘图线的组件进行比较

Java 检查jlabel图标是否相同,并与绘图线的组件进行比较,java,swing,awt,jlabel,Java,Swing,Awt,Jlabel,你好,我必须完成一个项目,但我不能再继续下去了。该项目的目的是让jlabel在左右两侧动态生成图标 图标在每一侧的顺序不同,并且存在一对一的图标关系 例如,一张图片: 现在,您可以在这些图片之间从左到右画线,单击“检查”按钮后,它会告诉您其正确与否: 还有一张照片: 我的问题是,在比较了图形之后,如何比较图标以知道哪个组合是正确的 我已经创建了两个列表来了解图形的组件组合 以下是迄今为止的代码- import java.awt.BasicStroke; import java.awt

你好,我必须完成一个项目,但我不能再继续下去了。该项目的目的是让jlabel在左右两侧动态生成图标

图标在每一侧的顺序不同,并且存在一对一的图标关系

例如,一张图片:

现在,您可以在这些图片之间从左到右画线,单击“检查”按钮后,它会告诉您其正确与否:

还有一张照片:

我的问题是,在比较了图形之后,如何比较图标以知道哪个组合是正确的

我已经创建了两个列表来了解图形的组件组合

以下是迄今为止的代码-

   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();
}
}

非常感谢您的帮助

完成了, 解决方案:您可以尝试更改第二个hashmap的图标名称 我知道这不是最好的解决办法,但至少我试过了。我会努力改进的

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();
}
}

你会比较这些图像。您可以使用某种类型的列表,其中包含相同的图像。当您显示图标时(每侧一个),您将保留有关显示图像索引的信息,允许您从左到右再映射1受保护的void paintComponent Graphics g{应受保护的void paintComponent Graphics g{super.paintcomponent;2 Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美的布局。相反,使用布局管理器,或与布局填充和边框一起使用…框架需要使用绝对布局创建..不,没有。但是这不是什么大问题必须使用eclipse的windowbuilder LOL!整个问题都会提出相反的建议。。谢谢你,我会试着在之后发布我的解决方案编辑:我知道对图标使用hashmap是没有用的…更改为private List icons=new ArrayList;…等等