Desired functionality: Listen for line-delineated barcode scans (a string of characters). For each pair (always scanned in the same order, one is longer than the other), compare the two for equality. If they are the same, display an image. If they are different, display another image. Repeat infinitely until window is closed.
In my head, there are two modules to this program. One is constantly looping, taking in the strings, comparing, and sending out a result (== or !=). The other just waits for the result, displays the appropriate image, and waits for the next comparison result.
The code I have is below, and I can't wrap my head around how to get repaint() to work properly at the end of each loop (once the result of the comparison is known). I have tried push out the result to the MyFrame Class and have it repaint() there and I have tried to call repaint() in the loop, but that won't work either.
The ScannerCompare Class works by itself in the console, but that won't be very useful for my intended implementation. If only the System.out.println("OK") and ("NG") were pictures!
Many thanks!
package BarcodeVerification;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class MyFrame extends JFrame {
private Image ngImage = null;
private Image okImage = null;
public MyFrame(String ngFilename, String okFilename) {
setTitle("MyWindow");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800, 600);
this.ngImage = new ImageIcon(ngFilename).getImage();
this.okImage = new ImageIcon(okFilename).getImage();
Container container = getContentPane();
container.setLayout(new BorderLayout());
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (m.getResult()) {
g.drawImage(okImage, 0, 0, okImage.getWidth(null), okImage.getHeight(null), null);
}
else {
g.drawImage(ngImage, 0, 0, ngImage.getWidth(null), ngImage.getHeight(null), null);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyFrame frame = new MyFrame("src/NG.png","src/OK/png");
ScannerCompare m = new ScannerCompare();
frame.setVisible(true);
}
});
}
}
package BarcodeVerification;
import java.util.Scanner;
public class ScannerCompare {
public Boolean ok;
public String scan1, scan2, injectorExtract;
public ScannerCompare (){
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Scan the paper");
scan1 = in.nextLine();
System.out.println("Scan the Injector QR Code");
scan2 = in.nextLine();
injectorExtract = scan2.substring(19);
if (scan1.compareTo(injectorExtract) != 0) {
System.out.println("NG");
ok = false;
repaint();
} else {
System.out.println("OK");
ok = true;
repaint()
}
}
}
public boolean getResult(){
return ok;
}
}