My problem statement is to draw a map from the given images whose Latitude and Longitude information is stored in database. Initial point Latitude and Longitude is given and then they will change in every 200 ms. I have to plot a map once and on top of the map I paint the path traveled wrt new changing Latitude and Longitude.
I have plotted the map using JLabel class and then I want to paint upon it. The problem is that in each repaint call old line vanishes and new will draw. I dont want to remove the old line. I have thought of storing all the points in a ArrayList and then appending new points to it and then painting the complete ArrayList after 200 ms. But is there any other more efficient way so that only new line is painted on JLabel and old remains.
Below is certain code I have written to draw the images given the initial images
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
public class MovingObject extends JLabel
{
int centerPointX,centerPointY;
int imageWidth,imageHeight;
float Lat,Lon;
float initLat,initLon;
float oldLat,oldLon;
BufferedImage image;
String image_name,image_name1;
float imageLeftLat,imageLeftLon,imageRightLat,imageRightLon;
JFrame frame;
DatagramPacket packet;
byte[] buffer = new byte[256];
byte[] read_float = new byte[4];
float[] pack_rec = new float[25];
DatagramSocket dsocket;
int img1;
ImageIcon imgicon;
float centerLat,centerLon;
ArrayList ar;
public MovingObject()
{
try {
frame = new JFrame("Map");
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(774, 800);
frame.setResizable(false);
frame.setBackground(Color.BLACK);
initLat = 19.244846f;
initLon = 72.859200f;
Lat = 19.244840f;
Lon = 72.859260f;
oldLat = Lat;
oldLon = Lon;
dsocket = new DatagramSocket(49001);
this.setBackground(null);
init();
new DataGenerator(200).start();
} catch (SocketException ex) {
ex.printStackTrace();
}
}
class Mapimages extends JLabel
{
public Mapimages(Icon icon)
{
setIcon(icon);
}
}
public void init()
{
image_name=GetImageName(initLat,initLon);
if(image_name!=null)
{
imgicon = new ImageIcon(image_name);
JLabel center = new Mapimages(imgicon);
add(center);
System.out.println(this.getBounds());
center.setBounds(256, 256, 256, 256);
center.setOpaque(false);
for(int i=-2;i<=2;i++)
{
for(int j=-2;j<=2;j++)
{
image_name=GetImageName(initLat+j*0.005186f,initLon+i*0.005493f);
if(image_name!=null)
imgicon = new ImageIcon(image_name);
else
imgicon = new ImageIcon("NA.jpg");
JLabel others = new Mapimages(imgicon);
add(others);
others.setBounds(256+i*256,256+j*256,256,256);
others.setOpaque(false);
}
}
}
}
class DataGenerator extends javax.swing.Timer implements ActionListener
{
DataGenerator(int interval)
{
super(interval, null);
addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Lat = Lat+0.00002f;
Lon = Lon+0.00003f;
//UDPListener();
repaint();
}
}
public String GetImageName(float getpointLat,float getpointLon)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:dahisar","","");
Statement stmt = con.createStatement();
ResultSet srs = stmt.executeQuery("SELECT * FROM Map");
while (srs.next())
{
img1=srs.getInt("ImageName");
image_name1 = img1+".png";
imageLeftLat = srs.getFloat("ImageLeftLat");
imageLeftLon = srs.getFloat("ImageLeftLon");
imageRightLat = srs.getFloat("ImageRightLat");
imageRightLon = srs.getFloat("ImageRightLon");
if (imageLeftLat <= getpointLat && imageRightLat > getpointLat && imageLeftLon <= getpointLon && imageRightLon > getpointLon)
{
//System.out.println("File: "+ image_name1 + "\nLeft CornerCoordinates(" +imageLeftX+" , "+imageLeftY+")"+"\nRight CornerCoordinates(" +imageRightX+" , "+imageRightY+")");
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
if (imageLeftLat <= getpointLat && imageRightLat > getpointLat && imageLeftLon <= getpointLon && imageRightLon > getpointLon)
{
//System.out.println(image_name1);
centerLat = imageLeftLat;
centerLon = imageLeftLon;
return image_name1;
}
else
return null;
}
@Override
public void paintChildren(Graphics g)
{
super.paintChildren(g);
// Write the code to paint a line between old Point and new Point
}
public static void main(String[]args)
{
MovingObject sac=new MovingObject();
}
}