所需功能:侦听行描述的条形码扫描(一串字符)。对于每对(总是以相同的顺序扫描,一个比另一个长),比较两者是否相等。如果它们相同,则显示图像。如果它们不同,则显示另一个图像。无限重复,直到窗口关闭。
在我看来,这个程序有两个模块。一个是不断循环,接受字符串,比较和发送结果(==或!=)。另一个只是等待结果,显示适当的图像,并等待下一个比较结果。
我的代码如下所示,我无法理解如何让repaint()在每个循环结束时正常工作(一旦比较结果已知)。我已经尝试将结果推出到MyFrame类并在那里重绘()并尝试在循环中调用repaint(),但这也无效。
ScannerCompare类在控制台中单独工作,但这对我的预期实现不是很有用。如果只有System.out.println(“OK”)和(“NG”)是图片!
非常感谢!
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;
}
}
答案 0 :(得分:1)
您可以使用Observer Pattern来解决基本问题。
"扫描仪"检查扫描结果并为注册/感兴趣的观察者/听众生成某种事件/通知
Swing是一个单线程框架,因此您不能在代码中放置while (true)
,因为它可能会阻止事件调度线程,从而阻止它处理任何事件,包括绘制事件。有关详细信息,请参阅Concurrency in Swing。
您可以使用SwingWorker
来克服此限制,允许doInBackground
方法处理扫描,并使用publish
/ process
方法触发通知安全的上下文,请参阅Worker Threads and SwingWorker了解更多详情
作为概念证明......
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Scanny {
public static void main(String[] args) {
new Scanny();
}
public Scanny() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
try {
BufferedImage empty = ImageIO.read(...);
BufferedImage good = ImageIO.read(...);
BufferedImage bad = ImageIO.read(...);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ScannerPane(new ImageIcon(empty), new ImageIcon(good), new ImageIcon(bad)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public class ScannerPane extends JPanel {
private JLabel label;
private Icon empty;
private Icon good;
private Icon bad;
public ScannerPane(Icon empty, Icon good, Icon bad) {
setLayout(new BorderLayout());
this.empty = empty;
this.good = good;
this.bad = bad;
label = new JLabel(empty, JLabel.CENTER);
add(label);
ScannerWorker worker = new ScannerWorker(new ScannerObserver() {
@Override
public void scanWasGood() {
label.setIcon(good);
}
@Override
public void scanWasBad() {
label.setIcon(bad);
}
});
worker.execute();
}
}
public interface ScannerObserver {
public void scanWasGood();
public void scanWasBad();
}
public class ScannerWorker extends SwingWorker<Void, Boolean> {
private ScannerObserver observer;
public ScannerWorker(ScannerObserver observer) {
this.observer = observer;
}
@Override
protected Void doInBackground() throws Exception {
Scanner in = new Scanner(System.in);
while (true) {
String scan1 = in.nextLine();
String scan2 = in.nextLine();
String injectorExtract = scan2.substring(19);
if (scan1.compareTo(injectorExtract) != 0) {
publish(Boolean.TRUE);
} else {
publish(Boolean.FALSE);
}
}
}
@Override
protected void process(List<Boolean> chunks) {
boolean last = chunks.get(chunks.size() - 1);
if (last) {
observer.scanWasGood();
} else {
observer.scanWasBad();
}
}
}
}
有关ImageIO