java - Swing Graphics object not drawing over image in JLabel -
i creating java program receives , plots gps coordinates on tcp socket connection on 2d image of map. constructor creates jframe , graphical components , starts off swingworker thread handle getting coordinates socket , drawing oval represent fix on map.
i able receive data on connection program not reliably draw points on image.
any suggestions , thank you!
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 server string fix = ""; telitsocketserver currentconnection = new telitsocketserver(); try { // wait client connect currentconnection.intializeconnection(); while (true) { // parse , convert received gps fix 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 publish node data return null; } @override protected void process(list<elephantfix> fixes) { int x, y; // published node elephantfix afix = fixes.get(fixes.size() - 1); // translate lat/long 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(); } } }
you should not drawing graphics object obtained calling getgraphics() on component. doing give graphics object doesn't persist , image doesn't persist. instead either draw on bufferedimage graphics object , display in gui, or iterate through data collection in jpanel's paintcomponent method , draw data obtained.
Comments
Post a Comment