지도의 2D 이미지에서 TCP 소켓 연결을 통해 GPS 좌표를 받아서 그려주는 Java 프로그램을 만듭니다. 생성자는 JFrame과 그래픽 구성 요소를 생성 한 다음 SwingWorker 스레드를 시작하여 소켓에서 좌표를 가져 오는 작업을 처리 한 다음지도에서 수정 사항을 나타내는 타원을 그립니다.스윙 그래픽 객체가 JLabel의 이미지 위에 그려지지 않습니다.
연결을 통해 항상 데이터를 수신 할 수 있지만 프로그램에서 이미지 위에 점을 확실히 그려 내지 못합니다.
의견을 보내 주셔서 감사합니다.
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();
}
}
}
감사합니다.나는 Swing에 가장 익숙하지 않기 때문에 내 패널을 다른 클래스로 분리하거나 페인트 메서드를 구현하려고 할 때 사용자 정의 페인팅과 관련하여해야할까요? 또한 컨테이너에 대한 마지막 두 점을 정하고 레이블 위에 페인트 구성 요소를 배치 할 수 있습니까? – akibapower711
@ akibapower711 그 결과를 얻기 위해 여러 구성 요소가 필요하다면 – MadProgrammer
이 예제를 고맙게 생각하며 단일 구성 요소 렌더링이 모두 필요한 것입니다. – akibapower711