Draw objects on a loaded image in java





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I need help with loading an image of a map and drawing a point on the received location.
Here is the code of the server:



import java.awt.Dimension;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Server extends JFrame{

private static int PACKETSIZE = 100 ;
private static int WIDTH = 1340;
private static int HEIGHT = 613;
public DatagramSocket socket;
public DrawPoint drawPoint;

public Server() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(WIDTH, HEIGHT));
add(drawPoint);
pack();
setVisible(true);
}

//This method converts the lat,lon coordinates to the coordinates of the pixels in the image of the map (which is 1319 by 664)

public String GPStoCoord( double lat, double lon){

double latref = 30.631103;
double lonref = -96.358981;
double yref = 0.015128;
double xref = 0.035589;
int coordx = (int)((latref - lat)/yref*1319);
int coordy = (int)((lon - lonref)/xref*664);

return coordx + "," +coordy;
}

private void GPSlocdraw() {
try {
DatagramPacket packet_GPS1 = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
socket.receive(packet_GPS1);
String sGPS1 = new String(packet_GPS1.getData());
String latlonGPS1 = sGPS1.split(",", 2);
double lat1 = Double.parseDouble(latlonGPS1[0]);
double lon1 = Double.parseDouble(latlonGPS1[1]);

//converting the GPS data to coordinates
String coord = GPStoCoord(lat1,lon1);
String latlon = coord.split(",");
int lat = Integer.parseInt(latlon[0]);
int lon = Integer.parseInt(latlon[1]);

drawPoint.setCoordx(lat);
drawPoint.setCoordy(lon);
drawPoint.repaint();
} catch( IOException e){}
}

public static void main( String args ) {

SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
}
}


Here is the code to the DRawPoint class, where I am trying to draw the image loaded and draw the oval graphic on top of it by it seems that whichever is drawn last erases the other.



import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class DrawPoint extends JPanel {
private int coordx, coordy;

@Override
public void paintComponent( Graphics g){
try {
BufferedImage map = ImageIO.read(new File("CSmap.png"));

super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
g.drawImage(map,0,0,this);

} catch(IOException e){}
}

//use setters to change the state
void setCoordy( int coordy) {this.coordy = coordy;}
void setCoordx( int coordx) {this.coordx = coordx;}
}


Also, how can I draw something to permanently stay on the image for the entire run?










share|improve this question































    1















    I need help with loading an image of a map and drawing a point on the received location.
    Here is the code of the server:



    import java.awt.Dimension;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class Server extends JFrame{

    private static int PACKETSIZE = 100 ;
    private static int WIDTH = 1340;
    private static int HEIGHT = 613;
    public DatagramSocket socket;
    public DrawPoint drawPoint;

    public Server() {

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    drawPoint = new DrawPoint();
    drawPoint.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    add(drawPoint);
    pack();
    setVisible(true);
    }

    //This method converts the lat,lon coordinates to the coordinates of the pixels in the image of the map (which is 1319 by 664)

    public String GPStoCoord( double lat, double lon){

    double latref = 30.631103;
    double lonref = -96.358981;
    double yref = 0.015128;
    double xref = 0.035589;
    int coordx = (int)((latref - lat)/yref*1319);
    int coordy = (int)((lon - lonref)/xref*664);

    return coordx + "," +coordy;
    }

    private void GPSlocdraw() {
    try {
    DatagramPacket packet_GPS1 = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
    socket.receive(packet_GPS1);
    String sGPS1 = new String(packet_GPS1.getData());
    String latlonGPS1 = sGPS1.split(",", 2);
    double lat1 = Double.parseDouble(latlonGPS1[0]);
    double lon1 = Double.parseDouble(latlonGPS1[1]);

    //converting the GPS data to coordinates
    String coord = GPStoCoord(lat1,lon1);
    String latlon = coord.split(",");
    int lat = Integer.parseInt(latlon[0]);
    int lon = Integer.parseInt(latlon[1]);

    drawPoint.setCoordx(lat);
    drawPoint.setCoordy(lon);
    drawPoint.repaint();
    } catch( IOException e){}
    }

    public static void main( String args ) {

    SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
    }
    }


    Here is the code to the DRawPoint class, where I am trying to draw the image loaded and draw the oval graphic on top of it by it seems that whichever is drawn last erases the other.



    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;

    public class DrawPoint extends JPanel {
    private int coordx, coordy;

    @Override
    public void paintComponent( Graphics g){
    try {
    BufferedImage map = ImageIO.read(new File("CSmap.png"));

    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillOval(coordx,coordy,8,8);
    g.drawImage(map,0,0,this);

    } catch(IOException e){}
    }

    //use setters to change the state
    void setCoordy( int coordy) {this.coordy = coordy;}
    void setCoordx( int coordx) {this.coordx = coordx;}
    }


    Also, how can I draw something to permanently stay on the image for the entire run?










    share|improve this question



























      1












      1








      1








      I need help with loading an image of a map and drawing a point on the received location.
      Here is the code of the server:



      import java.awt.Dimension;
      import java.io.IOException;
      import java.net.DatagramPacket;
      import java.net.DatagramSocket;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;

      public class Server extends JFrame{

      private static int PACKETSIZE = 100 ;
      private static int WIDTH = 1340;
      private static int HEIGHT = 613;
      public DatagramSocket socket;
      public DrawPoint drawPoint;

      public Server() {

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      drawPoint = new DrawPoint();
      drawPoint.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      add(drawPoint);
      pack();
      setVisible(true);
      }

      //This method converts the lat,lon coordinates to the coordinates of the pixels in the image of the map (which is 1319 by 664)

      public String GPStoCoord( double lat, double lon){

      double latref = 30.631103;
      double lonref = -96.358981;
      double yref = 0.015128;
      double xref = 0.035589;
      int coordx = (int)((latref - lat)/yref*1319);
      int coordy = (int)((lon - lonref)/xref*664);

      return coordx + "," +coordy;
      }

      private void GPSlocdraw() {
      try {
      DatagramPacket packet_GPS1 = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
      socket.receive(packet_GPS1);
      String sGPS1 = new String(packet_GPS1.getData());
      String latlonGPS1 = sGPS1.split(",", 2);
      double lat1 = Double.parseDouble(latlonGPS1[0]);
      double lon1 = Double.parseDouble(latlonGPS1[1]);

      //converting the GPS data to coordinates
      String coord = GPStoCoord(lat1,lon1);
      String latlon = coord.split(",");
      int lat = Integer.parseInt(latlon[0]);
      int lon = Integer.parseInt(latlon[1]);

      drawPoint.setCoordx(lat);
      drawPoint.setCoordy(lon);
      drawPoint.repaint();
      } catch( IOException e){}
      }

      public static void main( String args ) {

      SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
      }
      }


      Here is the code to the DRawPoint class, where I am trying to draw the image loaded and draw the oval graphic on top of it by it seems that whichever is drawn last erases the other.



      import java.awt.Color;
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import javax.imageio.ImageIO;
      import javax.swing.JPanel;

      public class DrawPoint extends JPanel {
      private int coordx, coordy;

      @Override
      public void paintComponent( Graphics g){
      try {
      BufferedImage map = ImageIO.read(new File("CSmap.png"));

      super.paintComponent(g);
      g.setColor(Color.BLACK);
      g.fillOval(coordx,coordy,8,8);
      g.drawImage(map,0,0,this);

      } catch(IOException e){}
      }

      //use setters to change the state
      void setCoordy( int coordy) {this.coordy = coordy;}
      void setCoordx( int coordx) {this.coordx = coordx;}
      }


      Also, how can I draw something to permanently stay on the image for the entire run?










      share|improve this question
















      I need help with loading an image of a map and drawing a point on the received location.
      Here is the code of the server:



      import java.awt.Dimension;
      import java.io.IOException;
      import java.net.DatagramPacket;
      import java.net.DatagramSocket;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;

      public class Server extends JFrame{

      private static int PACKETSIZE = 100 ;
      private static int WIDTH = 1340;
      private static int HEIGHT = 613;
      public DatagramSocket socket;
      public DrawPoint drawPoint;

      public Server() {

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      drawPoint = new DrawPoint();
      drawPoint.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      add(drawPoint);
      pack();
      setVisible(true);
      }

      //This method converts the lat,lon coordinates to the coordinates of the pixels in the image of the map (which is 1319 by 664)

      public String GPStoCoord( double lat, double lon){

      double latref = 30.631103;
      double lonref = -96.358981;
      double yref = 0.015128;
      double xref = 0.035589;
      int coordx = (int)((latref - lat)/yref*1319);
      int coordy = (int)((lon - lonref)/xref*664);

      return coordx + "," +coordy;
      }

      private void GPSlocdraw() {
      try {
      DatagramPacket packet_GPS1 = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
      socket.receive(packet_GPS1);
      String sGPS1 = new String(packet_GPS1.getData());
      String latlonGPS1 = sGPS1.split(",", 2);
      double lat1 = Double.parseDouble(latlonGPS1[0]);
      double lon1 = Double.parseDouble(latlonGPS1[1]);

      //converting the GPS data to coordinates
      String coord = GPStoCoord(lat1,lon1);
      String latlon = coord.split(",");
      int lat = Integer.parseInt(latlon[0]);
      int lon = Integer.parseInt(latlon[1]);

      drawPoint.setCoordx(lat);
      drawPoint.setCoordy(lon);
      drawPoint.repaint();
      } catch( IOException e){}
      }

      public static void main( String args ) {

      SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
      }
      }


      Here is the code to the DRawPoint class, where I am trying to draw the image loaded and draw the oval graphic on top of it by it seems that whichever is drawn last erases the other.



      import java.awt.Color;
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import javax.imageio.ImageIO;
      import javax.swing.JPanel;

      public class DrawPoint extends JPanel {
      private int coordx, coordy;

      @Override
      public void paintComponent( Graphics g){
      try {
      BufferedImage map = ImageIO.read(new File("CSmap.png"));

      super.paintComponent(g);
      g.setColor(Color.BLACK);
      g.fillOval(coordx,coordy,8,8);
      g.drawImage(map,0,0,this);

      } catch(IOException e){}
      }

      //use setters to change the state
      void setCoordy( int coordy) {this.coordy = coordy;}
      void setCoordx( int coordx) {this.coordx = coordx;}
      }


      Also, how can I draw something to permanently stay on the image for the entire run?







      java graphics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 4:51









      c0der

      9,74651947




      9,74651947










      asked Nov 24 '18 at 20:33









      Taoufik SekkatTaoufik Sekkat

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The following code demonstrates repeatedly drawing a point in different location on an underlying image.

          It can be copy pasted into one file (Server.java) and executed:



          import java.awt.Color;
          import java.awt.Dimension;
          import java.awt.Graphics;
          import java.awt.Image;
          import java.io.IOException;
          import java.net.URL;
          import java.util.Random;
          import javax.imageio.ImageIO;
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.SwingUtilities;
          import javax.swing.Timer;

          public class Server extends JFrame{

          private DrawPoint drawPoint;

          public Server() {

          setDefaultCloseOperation(EXIT_ON_CLOSE);
          drawPoint = new DrawPoint();
          add(drawPoint);
          pack();
          setVisible(true);
          }

          //This conversion is not essential for the question asked. remove to make it Mcve
          //public String GPStoCoord( double lat, double lon){}

          private void GPSlocdraw() {
          //generate random x,y within an arbitrary range
          Random rnd = new Random(); int maxY = 350, maxX = 250;
          Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
          drawPoint.setCoordx(rnd.nextInt(maxX));
          drawPoint.setCoordy(rnd.nextInt(maxY));
          drawPoint.repaint();
          });
          timer.start();
          }

          public static void main( String args ) {
          SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
          }
          }

          class DrawPoint extends JPanel {

          private int coordx, coordy, width, height;
          private Image map;

          public DrawPoint() {
          try {
          //to make an mcVe always use publicly available resources
          map = ImageIO.read(new URL("http://www.digitalphotoartistry.com/rose1.jpg"));
          height = map.getHeight(this);
          width = map.getWidth(this);
          setPreferredSize(new Dimension(width, height)); //fit panel to image
          } catch ( IOException ex) {ex.printStackTrace(); }
          }

          @Override
          public void paintComponent(Graphics g){
          //read from file once, not every method run BufferedImage map = ImageIO.read(new File("CSmap.png"));
          super.paintComponent(g);
          g.setColor(Color.YELLOW);
          g.drawImage(map,0,0,this);//image first
          g.fillOval(coordx,coordy,8,8); //point on top
          }

          //use setters to change the state
          void setCoordy( int coordy) {this.coordy = coordy;}
          void setCoordx( int coordx) {this.coordx = coordx;}
          }


          As for "how can I draw something to permanently stay on the image for the entire run?" : to get an idea do not change ovals x and y coordinates, and see what happens.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462122%2fdraw-objects-on-a-loaded-image-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The following code demonstrates repeatedly drawing a point in different location on an underlying image.

            It can be copy pasted into one file (Server.java) and executed:



            import java.awt.Color;
            import java.awt.Dimension;
            import java.awt.Graphics;
            import java.awt.Image;
            import java.io.IOException;
            import java.net.URL;
            import java.util.Random;
            import javax.imageio.ImageIO;
            import javax.swing.JFrame;
            import javax.swing.JPanel;
            import javax.swing.SwingUtilities;
            import javax.swing.Timer;

            public class Server extends JFrame{

            private DrawPoint drawPoint;

            public Server() {

            setDefaultCloseOperation(EXIT_ON_CLOSE);
            drawPoint = new DrawPoint();
            add(drawPoint);
            pack();
            setVisible(true);
            }

            //This conversion is not essential for the question asked. remove to make it Mcve
            //public String GPStoCoord( double lat, double lon){}

            private void GPSlocdraw() {
            //generate random x,y within an arbitrary range
            Random rnd = new Random(); int maxY = 350, maxX = 250;
            Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
            drawPoint.setCoordx(rnd.nextInt(maxX));
            drawPoint.setCoordy(rnd.nextInt(maxY));
            drawPoint.repaint();
            });
            timer.start();
            }

            public static void main( String args ) {
            SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
            }
            }

            class DrawPoint extends JPanel {

            private int coordx, coordy, width, height;
            private Image map;

            public DrawPoint() {
            try {
            //to make an mcVe always use publicly available resources
            map = ImageIO.read(new URL("http://www.digitalphotoartistry.com/rose1.jpg"));
            height = map.getHeight(this);
            width = map.getWidth(this);
            setPreferredSize(new Dimension(width, height)); //fit panel to image
            } catch ( IOException ex) {ex.printStackTrace(); }
            }

            @Override
            public void paintComponent(Graphics g){
            //read from file once, not every method run BufferedImage map = ImageIO.read(new File("CSmap.png"));
            super.paintComponent(g);
            g.setColor(Color.YELLOW);
            g.drawImage(map,0,0,this);//image first
            g.fillOval(coordx,coordy,8,8); //point on top
            }

            //use setters to change the state
            void setCoordy( int coordy) {this.coordy = coordy;}
            void setCoordx( int coordx) {this.coordx = coordx;}
            }


            As for "how can I draw something to permanently stay on the image for the entire run?" : to get an idea do not change ovals x and y coordinates, and see what happens.






            share|improve this answer




























              1














              The following code demonstrates repeatedly drawing a point in different location on an underlying image.

              It can be copy pasted into one file (Server.java) and executed:



              import java.awt.Color;
              import java.awt.Dimension;
              import java.awt.Graphics;
              import java.awt.Image;
              import java.io.IOException;
              import java.net.URL;
              import java.util.Random;
              import javax.imageio.ImageIO;
              import javax.swing.JFrame;
              import javax.swing.JPanel;
              import javax.swing.SwingUtilities;
              import javax.swing.Timer;

              public class Server extends JFrame{

              private DrawPoint drawPoint;

              public Server() {

              setDefaultCloseOperation(EXIT_ON_CLOSE);
              drawPoint = new DrawPoint();
              add(drawPoint);
              pack();
              setVisible(true);
              }

              //This conversion is not essential for the question asked. remove to make it Mcve
              //public String GPStoCoord( double lat, double lon){}

              private void GPSlocdraw() {
              //generate random x,y within an arbitrary range
              Random rnd = new Random(); int maxY = 350, maxX = 250;
              Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
              drawPoint.setCoordx(rnd.nextInt(maxX));
              drawPoint.setCoordy(rnd.nextInt(maxY));
              drawPoint.repaint();
              });
              timer.start();
              }

              public static void main( String args ) {
              SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
              }
              }

              class DrawPoint extends JPanel {

              private int coordx, coordy, width, height;
              private Image map;

              public DrawPoint() {
              try {
              //to make an mcVe always use publicly available resources
              map = ImageIO.read(new URL("http://www.digitalphotoartistry.com/rose1.jpg"));
              height = map.getHeight(this);
              width = map.getWidth(this);
              setPreferredSize(new Dimension(width, height)); //fit panel to image
              } catch ( IOException ex) {ex.printStackTrace(); }
              }

              @Override
              public void paintComponent(Graphics g){
              //read from file once, not every method run BufferedImage map = ImageIO.read(new File("CSmap.png"));
              super.paintComponent(g);
              g.setColor(Color.YELLOW);
              g.drawImage(map,0,0,this);//image first
              g.fillOval(coordx,coordy,8,8); //point on top
              }

              //use setters to change the state
              void setCoordy( int coordy) {this.coordy = coordy;}
              void setCoordx( int coordx) {this.coordx = coordx;}
              }


              As for "how can I draw something to permanently stay on the image for the entire run?" : to get an idea do not change ovals x and y coordinates, and see what happens.






              share|improve this answer


























                1












                1








                1







                The following code demonstrates repeatedly drawing a point in different location on an underlying image.

                It can be copy pasted into one file (Server.java) and executed:



                import java.awt.Color;
                import java.awt.Dimension;
                import java.awt.Graphics;
                import java.awt.Image;
                import java.io.IOException;
                import java.net.URL;
                import java.util.Random;
                import javax.imageio.ImageIO;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.SwingUtilities;
                import javax.swing.Timer;

                public class Server extends JFrame{

                private DrawPoint drawPoint;

                public Server() {

                setDefaultCloseOperation(EXIT_ON_CLOSE);
                drawPoint = new DrawPoint();
                add(drawPoint);
                pack();
                setVisible(true);
                }

                //This conversion is not essential for the question asked. remove to make it Mcve
                //public String GPStoCoord( double lat, double lon){}

                private void GPSlocdraw() {
                //generate random x,y within an arbitrary range
                Random rnd = new Random(); int maxY = 350, maxX = 250;
                Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
                drawPoint.setCoordx(rnd.nextInt(maxX));
                drawPoint.setCoordy(rnd.nextInt(maxY));
                drawPoint.repaint();
                });
                timer.start();
                }

                public static void main( String args ) {
                SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
                }
                }

                class DrawPoint extends JPanel {

                private int coordx, coordy, width, height;
                private Image map;

                public DrawPoint() {
                try {
                //to make an mcVe always use publicly available resources
                map = ImageIO.read(new URL("http://www.digitalphotoartistry.com/rose1.jpg"));
                height = map.getHeight(this);
                width = map.getWidth(this);
                setPreferredSize(new Dimension(width, height)); //fit panel to image
                } catch ( IOException ex) {ex.printStackTrace(); }
                }

                @Override
                public void paintComponent(Graphics g){
                //read from file once, not every method run BufferedImage map = ImageIO.read(new File("CSmap.png"));
                super.paintComponent(g);
                g.setColor(Color.YELLOW);
                g.drawImage(map,0,0,this);//image first
                g.fillOval(coordx,coordy,8,8); //point on top
                }

                //use setters to change the state
                void setCoordy( int coordy) {this.coordy = coordy;}
                void setCoordx( int coordx) {this.coordx = coordx;}
                }


                As for "how can I draw something to permanently stay on the image for the entire run?" : to get an idea do not change ovals x and y coordinates, and see what happens.






                share|improve this answer













                The following code demonstrates repeatedly drawing a point in different location on an underlying image.

                It can be copy pasted into one file (Server.java) and executed:



                import java.awt.Color;
                import java.awt.Dimension;
                import java.awt.Graphics;
                import java.awt.Image;
                import java.io.IOException;
                import java.net.URL;
                import java.util.Random;
                import javax.imageio.ImageIO;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.SwingUtilities;
                import javax.swing.Timer;

                public class Server extends JFrame{

                private DrawPoint drawPoint;

                public Server() {

                setDefaultCloseOperation(EXIT_ON_CLOSE);
                drawPoint = new DrawPoint();
                add(drawPoint);
                pack();
                setVisible(true);
                }

                //This conversion is not essential for the question asked. remove to make it Mcve
                //public String GPStoCoord( double lat, double lon){}

                private void GPSlocdraw() {
                //generate random x,y within an arbitrary range
                Random rnd = new Random(); int maxY = 350, maxX = 250;
                Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
                drawPoint.setCoordx(rnd.nextInt(maxX));
                drawPoint.setCoordy(rnd.nextInt(maxY));
                drawPoint.repaint();
                });
                timer.start();
                }

                public static void main( String args ) {
                SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
                }
                }

                class DrawPoint extends JPanel {

                private int coordx, coordy, width, height;
                private Image map;

                public DrawPoint() {
                try {
                //to make an mcVe always use publicly available resources
                map = ImageIO.read(new URL("http://www.digitalphotoartistry.com/rose1.jpg"));
                height = map.getHeight(this);
                width = map.getWidth(this);
                setPreferredSize(new Dimension(width, height)); //fit panel to image
                } catch ( IOException ex) {ex.printStackTrace(); }
                }

                @Override
                public void paintComponent(Graphics g){
                //read from file once, not every method run BufferedImage map = ImageIO.read(new File("CSmap.png"));
                super.paintComponent(g);
                g.setColor(Color.YELLOW);
                g.drawImage(map,0,0,this);//image first
                g.fillOval(coordx,coordy,8,8); //point on top
                }

                //use setters to change the state
                void setCoordy( int coordy) {this.coordy = coordy;}
                void setCoordx( int coordx) {this.coordx = coordx;}
                }


                As for "how can I draw something to permanently stay on the image for the entire run?" : to get an idea do not change ovals x and y coordinates, and see what happens.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 5:35









                c0derc0der

                9,74651947




                9,74651947
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462122%2fdraw-objects-on-a-loaded-image-in-java%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini