Trace a line above an image on JFrame Java

I have the fallowing code:

// set up the GUI for display the PgmImage object
    JFrame f = new JFrame("PGM Image:");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {

That produces this image:

I wonder if it's possible to draw a line above the image on the same JFrame. I have the discover a path with A* algorithm and trace it in this image.



You ask:

I wonder if it's possible to draw a line above the image on the same JFrame.

  • Yes it's possible.
  • As with most all Swing graphics, you wouldn't draw directly in the JFrame but rather in the paintComponent(Graphics g) method of an object that is derived from JComponent such as a JPanel that is held by and displayed in your JFrame. Tutorial Link.
  • The details of your solution will all depend on the details of your problem.

You ask:

So if i draw in two paintComponet i can join the two together?

Yep, you could display your image in an ImageIcon held by a JLabel, override JLabel's paintComponent(Graphics g), and draw your lines or whatevers on top of your image. I've got code that does just that somewhere as an answer on this site... will go look.

Edit Please check out my answer and code here: repaint() not calling PaintComponent to use Graphics2D.

Need Your Help

Data structure for "limited memory map"

memory data-structures hashmap

I want to implement an associative collection, mapping keys K to values V. I further want to associate each value V with a weight, so we have Map[K → (V, Double)].

Java Packet Handler Threading

java multithreading sockets netty

I have a quick general question. I am currently using Netty to handle TCP and UDP packets coming from a client. I have the socket listeners each on a separate thread and it works great.