1

In my main class I have the following code to load an image from my machine and display it on the frame to draw things on it:

public class ShowMap extends JPanel {

    private static final int WIDTH = 1340;
    private static final int HEIGHT = 613;

    public void main(String args[]) {
        JFrame frame = new JFrame("MAP");
        frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
        frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = (JPanel)frame.getContentPane();
        JLabel label = new JLabel();
        label.setIcon(new ImageIcon("map.png"));
        panel.add(label);
    }
}

The image I am loading is a map where I would like to indicate the position of some objects by drawing points in the right coordinates. So it is important here to dictate to the DrawPoint class (below) what coordinates should get the point.

Also, I would greatly appreciate an explanation of how to erase a point that has been drawn.

My search led me to the following, but as soon as I add int coordx, int coordy to the arguments of the method, it is no more highlighted, and I don't know how to call this method in ShowMap while passing the coordinates as arguments.

public class DrawPoint extends JPanel {

    private int coordx;
    private int coordy;

    public void paintComponent(Graphics g, int coordx, int coordy){
        g.setColor(Color.BLACK);
        g.fillOval(coordx,coordy,8,8);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again – MadProgrammer Nov 24 '18 at 01:30

1 Answers1

0

Here is a demonstration of what MadProgrammer wrote in his comment : "should be changing a state variable of the component and then calling repaint" :

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingTest extends JFrame {

    private static final int SIZE = 300;
    private DrawPoint drawPoint;

    public SwingTest()  {

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

    //demonstrate change in DrawPoint state
    private void reDraw() {

        Random rnd = new Random();
        Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
            drawPoint.setCoordx(rnd.nextInt(SIZE));
            drawPoint.setCoordy(rnd.nextInt(SIZE));
            drawPoint.repaint();
        });
        timer.start();
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(() ->    new SwingTest().reDraw());
    }
}

class DrawPoint extends JPanel {

    private int coordx, coordy;

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillOval(coordx,coordy,8,8);
    }

    //use setters to change the state 
    void setCoordy(int coordy) {    this.coordy = coordy; }
    void setCoordx(int coordx) {this.coordx = coordx;}
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • c0der, would you please explain how I could do this on a loaded image from my machine? – Taoufik Sekkat Nov 24 '18 at 15:26
  • Assuming your image is `Image bgImage` draw it by `g.drawImage(bgImage, 0, 0, this);`. If you need help with it post a new question and leave me a message here so I can try and help. – c0der Nov 24 '18 at 17:46
  • I posted a new question, link: https://stackoverflow.com/questions/53462122/draw-objects-on-a-loaded-image-in-java. Thank you for all the help. – Taoufik Sekkat Nov 24 '18 at 20:36