我正在创建一个Java程序,该程序通过TCP套接字连接在地图的2D图像上接收和绘制GPS坐标。构造函数创建JFrame和图形组件,然后从SwingWorker线程开始以处理从套接字获取坐标,然后绘制一个椭圆形来表示地图上的修复。
我总是能够通过连接接收数据,但是程序无法可靠地在图像上绘制点。
任何建议,谢谢!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import connectivity.TelitSocketServer;
import calculations.PlottingMath;
import objects.ElephantFix;
@SuppressWarnings("serial")
public class Gui
{
protected JFrame mainWindow;
protected JPanel mapArea = new JPanel();
private ReceiveFix rfix;
public Gui()
{
// TODO Auto-generated constructor stub
mainWindow = new JFrame();
// Load map image
Image map = null;
try
{
File mapImage = new File("map_images/AWE_PLOT.JPG");
map = ImageIO.read(mapImage);
} catch (Exception e)
{
System.out.println(e.toString());
}
JLabel label = new JLabel(new ImageIcon(map));
mapArea.add(label);
// Map Image Dimensions
mainWindow.getContentPane().add(mapArea, "Center");
mainWindow.setSize(471, 670);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setTitle("ElePlotter");
mainWindow.setVisible(true);
rfix = new ReceiveFix();
rfix.execute();
}
public static void main(String args[])
{
new Gui();
}
private class ReceiveFix extends SwingWorker<Void, ElephantFix>
{
@Override
protected Void doInBackground()
{
// Start the server
String fix = "";
TelitSocketServer currentConnection = new TelitSocketServer();
try
{
// Wait for client to connect
currentConnection.intializeConnection();
while (true)
{
// Parse and convert received GPS fix into arc radians
fix = currentConnection.readLine();
String[] split = fix.split(" ");
double latWholeDegrees = Double.parseDouble(split[0]
.substring(0, 3));
double longWholeDegrees = Double.parseDouble(split[1]
.substring(0, 3));
double latMinutes = Double.parseDouble(split[0]
.substring(3)) * .166667;
double longMinutes = Double.parseDouble(split[1]
.substring(3)) * .166667;
double lat = latWholeDegrees - latMinutes / 10;
double lon = longWholeDegrees + longMinutes / 10;
publish(new ElephantFix(lat, lon));
}
} catch (Exception e)
{
e.printStackTrace();
}
// Return null if somehow unable to publish node data
return null;
}
@Override
protected void process(List<ElephantFix> fixes)
{
int x, y;
// Get the most recently published node
ElephantFix aFix = fixes.get(fixes.size() - 1);
// Translate lat/long into map X/Y pixel
x = PlottingMath.getCurrentPixelX(aFix.getLatitude(),
aFix.getLongitude());
y = PlottingMath.getCurrentPixelY(aFix.getLatitude(),
aFix.getLongitude());
// Plot on image
Graphics g = mapArea.getGraphics();
g.setColor(Color.RED);
g.fillOval(x, y, 15, 15);
// mapArea.validate();
//
// mapArea.repaint();
//
// Gui.this.repaint();
}
}
}
参考方案
您不应使用通过在组件上调用getGraphics()
获得的Graphics对象进行绘图。这样做将为您提供一个不会持久的Graphics对象和一个不会持久的图像。取而代之的是使用带有其Graphics对象的BufferedImage并在GUI中显示它,或者遍历JPanel的paintComponent方法中的数据集合并使用获得的数据进行绘制。
我需要使用Java Swing的搜索框,如果单击任何建议,当输入字母时它将显示来自数据库的建议,它将执行一些操作。如果有可能在Java swing中,请提供源代码提前致谢 java大神给出的解决方案 您可以使用DefaultComboBoxModel,输出将是这样。Try this在此代码中,您将找到countries数组,因此您需要从数据库中获取此数组。
Swing-典型桌面应用程序中的并发 - java这可能是一个广泛的问题,但我想知道单用户桌面应用程序通常在何处使用并发。您如何发现理想情况下(在设计阶段)并发问题?附注:从操作系统理论上来说,并发问题通常发生在无法共享资源(例如打印机)的情况下。不过,我仍然对在编程级别上可能发生的情况感到迷惑。 java大神给出的解决方案 Event Dispatch thread与其他逻辑之间的Swing中可能存在线程…
Java-如何使Jtextfield充满整个框架 - java我正在尝试使JTextArea填充整个框架,因此它看起来有点像记事本或textedit。也可以有一个滚动窗格。提前致谢!编辑:JTextArea 参考方案 试试这个 - -import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JTextField; public…
如何使用BorderLayout(Java)扩展JTextField - java我有一个Java程序,其中使用的是JTextField,但如果我未指定默认大小,则其宽度为0。我将其插入BorderLayout中,因此如何制作它展开以填充整个容器? java大神给出的解决方案 在上面的示例中,文本字段将正常工作。但是,如果您插入EAST或WEST,则将不起作用。import java.awt.BorderLayout; import ja…
将新行添加到表时,将JTable行向下移动 - java我必须维护用户在JTable中生成的事件列表。JTable由2列时间和事件以及5行组成。生成第一个事件时,其数据必须放置在JTable的第一行中;生成第二个事件时,必须将第一行中的数据移动到第二行中,并且第二个事件的数据必须放置在,依此类推,直到第5行。事件没有限制(可以生成n个事件)。但是我必须在JTabel中仅显示5个事件,而更早的事件将存储在文本文件中…