【发布时间】:2020-04-19 15:32:25
【问题描述】:
我有这些带分隔符的坐标 :
I/System.out: {X=282,Y=990}:
I/System.out: {X=290,Y=990}:
I/System.out: {X=298,Y=990}:
I/System.out: {X=310,Y=990}:
I/System.out: {X=314,Y=990}:
并想用它们填充Point[] 数组,然后我尝试:
private String[] tokens = coordinates.split(Pattern.quote(":"));
private Point[] moviments = new Point[8192];
for (int i = 0; i < tokens.length; i++)
moviments[i] = new Point(tokens[i]);
但出现“类型不兼容”的错误。怎么解决?
版本:
上面的坐标是从这个代码通过Socket发送的:
C#(发件人)
using System.Net.Sockets;
private List<Point> lstPoints;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lstPoints = new List<Point>();
lstPoints.Add(new Point(e.X, e.Y));
}
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lstPoints.Add(new Point(e.X, e.Y));
}
}
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
lstPoints.Add(new Point(e.X, e.Y));
StringBuilder sb = new StringBuilder();
foreach (Point obj in lstPoints)
{
sb.Append(Convert.ToString(obj) + ":" + Environment.NewLine);
}
serverSocket.Send("MDRAWEVENT" + sb.ToString() + Environment.NewLine);
}
android(接收器)
import java.net.Socket;
String xline;
while (clientSocket.isConnected()) {
BufferedReader xreader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
if (xreader.ready()) {
while ((xline = xreader.readLine()) != null) {
xline = xline.trim();
if (xline != null && !xline.trim().isEmpty()) {
if (xline.contains("MDRAWEVENT")) {
String coordinates = xline.replace("MDRAWEVENT", "");
String[] tokens = coordinates.split(Pattern.quote(":"));
/*
Point[] moviments = new Point[8192];
for (int i = 0; i < tokens.length; i++)
moviments[i] = new Point(tokens[i]);
*/
mouseDraw(moviments);
}
}
}
}
public void mouseDraw(Point[] segments) {
Path path = new Path();
path.moveTo(segments[0].x, segments[0].y);
for (int i = 1; i < segments.length; i++) {
path.lineTo(segments[i].x, segments[i].y);
// After draw line, set the next start point
path.moveTo(segments[i].x, segments[i].y);
}
}
【问题讨论】:
-
请显示类
Point的定义。最有可能的是,不存在将单个String作为参数的 c'tor。 -
@Turing85,
Point是安卓原生的。 -
我假设 Point 是 docs.microsoft.com/en-us/dotnet/api/… Point 需要 2 个整数来创建而不是字符串,您需要将字符串解析为所需的数据类型。
标签: java c# android arrays sockets