问题总结: 我使用Netbeans 7.2.1为在Windows 7 (Java版本1.8.0_40)上开发的Java Swing/AWT程序生成了一个. jar文件,它有助于从屏幕上收集用户的笔迹。它在Windows 7笔记本电脑上运行良好,但由于某些原因,只能在Windows 8.1平板电脑(Java版本1.8.0_45)上捕获屏幕特定区域的手写数据。有人能告诉我为什么会这样吗?
详细信息:我有收集在线手写样本的要求(即那些从电子设备,如使用笔/触控笔和书写表面的平板电脑获得)进行一些分析
作为开发这种性质的程序的新手,我在网上阅读了有关它的信息,并决定使用Java Swing/AWT工具包
一个人的笔迹是由笔画组成的,笔画又由点组成。我的目标是捕捉:-屏幕上点的X坐标和y坐标-该点创建的时间戳-笔画的开始时间,结束时间和颜色(颜色不太重要)
为此,我在Windows 7 Home Basic操作系统上使用Netbeans 7.2.1 IDE和Java 1.8.0_40编写了以下程序
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package handwritingsamplerawt;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HandwritingSamplerAWT {
static JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CreateAndShowGUI();
}
});
}
private static void CreateAndShowGUI() {
frame = new JFrame("Writing Surface v0.1");
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.LIGHT_GRAY);
frame.pack();
frame.add(new MyPanel());
frame.setVisible(true);
}
}
class MyPanel extends JPanel{
private int x,y;
static int strokeIndex;
private long reducedMillis;
private ArrayList<StrokeInfo> strokes;
private JButton btnSave;
public MyPanel() {
MyPanel.strokeIndex=0;
this.reducedMillis = 1435800000000L;
this.strokes = new ArrayList<>();
this.btnSave = new JButton("SAVE SAMPLE");
this.btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
WriteCoordinates();
}
});
this.add(this.btnSave);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x=e.getX();
y=e.getY();
SaveCoordinates(x,y,"PRESSED");
repaint();
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
x=e.getX();
y=e.getY();
SaveCoordinates(x,y,"DRAGGED");
repaint();
}
});
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
x=e.getX();
y=e.getY();
SaveCoordinates(x,y,"RELEASED");
repaint();
}
});
}
void SaveCoordinates(int xCoordinate, int yCoordinate, String actionIndicator){
try {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
double timeStamp=(double)(currDate.getTime()-reducedMillis);
PointInfo pointObj = new PointInfo(xCoordinate, yCoordinate, timeStamp);
switch (actionIndicator) {
case "PRESSED":
StrokeInfo newStroke = new StrokeInfo();
newStroke.points.add(pointObj);
strokes.add(newStroke);
break;
case "DRAGGED":
strokes.get(strokeIndex).points.add(pointObj);
break;
case "RELEASED":
strokeIndex+=1;
break;
}
} catch (Exception ex){
String errMsg = ex.getMessage();
System.out.println(errMsg);
}
}
void WriteCoordinates() {
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String currTimeString = dateFormat.format(cal.getTime());
DecimalFormat decFormat = new DecimalFormat("#");
decFormat.setMaximumFractionDigits(2);
FileWriter writer = new FileWriter("D:\HandwritingCaptures\HandwritingText\"+currTimeString+".txt");
SetStrokeAttributes(strokes);
ListIterator<PointInfo> pointItr;
if (strokes.isEmpty()==false) {
for (int index = 0; index < strokeIndex; index++) {
writer.write(strokes.get(index).colour);
writer.append('t');
writer.write(decFormat.format( strokes.get(index).startTime));
writer.append('t');
writer.write(decFormat.format( strokes.get(index).endTime));
writer.append('n');
pointItr = strokes.get(index).points.listIterator();
while (pointItr.hasNext()) {
PointInfo currPoint = pointItr.next();
writer.write(String.valueOf(currPoint.x));
writer.append('t');
writer.write(String.valueOf(currPoint.y));
writer.append('t');
writer.write(decFormat.format(currPoint.timestamp));
writer.append('n');
}
writer.append('#');
writer.append('n');
}
}
writer.close();
SaveScreenshot("D:\HandwritingCaptures\Screenshots\"+currTimeString+".png");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
void SetStrokeAttributes(ArrayList<StrokeInfo> strokeList) {
double startTime, endTime;
String colour;
StrokeInfo tmpStroke;
ArrayList<PointInfo> points;
if (strokeList.isEmpty() == false) {
for (int index = 0; index < strokeList.size(); index++) {
tmpStroke = strokeList.get(index);
points = tmpStroke.points;
tmpStroke.colour = "black";
tmpStroke.startTime=points.get(0).timestamp;
tmpStroke.endTime=points.get(points.size()-1).timestamp;
strokeList.set(index, tmpStroke);
}
}
}
void SaveScreenshot(String imgFilePath){
try {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File(imgFilePath));
} catch (IOException | AWTException ex) {
System.out.println(ex.getMessage());
}
}
public Dimension getPreferredSize() {
return new Dimension(1366,768);
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.BLACK);
g.drawLine(x, y, x, y);
}
}
class PointInfo {
int x,y;
double timestamp;
public PointInfo(int px, int py, double ts) {
this.x=px;
this.y=py;
this.timestamp=ts;
}
}
class StrokeInfo {
ArrayList<PointInfo> points;
double startTime, endTime;
String colour;
public StrokeInfo() {
points= new ArrayList<>();
}
}
我使用IDE本身生成了.jar文件(项目属性->构建->打包->压缩JAR文件)
然后将.jar文件复制到HP EliteBook 2730P笔记本电脑上,安装JRE 1.7.0.800和Windows 7 Pro操作系统(32位),它可以很好地从屏幕的所有区域收集笔迹
但是,当我将相同的.jar复制到安装JRE 1.8.0_45和Windows 8.1(64位)的HP Elite x2 1011 G1平板电脑上并运行它时,我发现奇怪的是,它只能从屏幕的特定区域捕获手写笔输入-更具体地说是在右上角。其他区域完全没有响应
有人能帮我理解为什么会发生这种情况吗?本想在这里发布几张截图,但我的低声誉使我无法这样做。
附加思考:使用。net或Java FX来开发这样一个在Windows 8.1环境中使用的工具会更好吗?
你的面板似乎有一个固定的大小:
return new Dimension(1366,768);
你的平板电脑的分辨率比这个大吗?
编辑:这应该有帮助:
private static void CreateAndShowGUI() {
frame = new JFrame("Writing Surface v0.1");
// Using the default BorderLayout here.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.LIGHT_GRAY);
frame.getContentPane().add(new MyPanel(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
然而,你的布局仍然存在问题,比如"保存"按钮跳来跳去。你应该看看这个教程:
https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html在运行
时看到.jar文件的行为,可以清楚地看出问题可能与硬编码的维度有关。平板电脑的分辨率(1920 x 1080像素——通过在桌面上右击查看屏幕分辨率)比我最初运行程序的机器(1366 x 768像素)更高。右上角可写区域的尺寸实际上与硬编码的尺寸
一致。因此,我以以下方式修改了重写的方法getPreferredSize():public Dimension getPreferredSize() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return new Dimension((int)screenSize.getWidth(),(int)screenSize.getHeight());
}
这会导致首选大小组件采用执行.jar的设备屏幕的有效高度和宽度。这一点很重要,因为根据使用的布局管理器的不同,它对上述方法的处理确实不同
"SAVE SAMPLE"按钮仍然间歇性地将其图像(不可点击)投射到屏幕的其他区域-这可能与UI组件的布局管理器处理有关。一旦我找到解决方案,我将为这个问题添加一个编辑