Hi all,
I need to draw simple lines on JFrame/JPanel. What is the easiest method to go? Also what do I need to display an image on JFrame/JPanel

Thanks

Here's the minimal implementation of drawing lines on a JPanel. The important part is extending JPanel to override paintComponent() and use the Graphics reference to paint what you need to.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LinePaintDemo extends JPanel{

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.drawLine(0, 0, getWidth(), getHeight());
        g.drawLine(getWidth(), 0, 0, getHeight());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new LinePaintDemo());
                frame.setSize(300,300);
                frame.setVisible(true);
            }
        });
    }
}

There is also a drawImage() method in the Graphics class.

Please explain these to me as I have not understood what they are doing:

#
EventQueue.invokeLater(new Runnable() {
#
public void run() {.....}

Thanks Ezza,
I modified a little bit to get three paralleled lines,
exactly what I needed

package com.steve;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LinePaintDemo extends JPanel{

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.drawLine(getWidth()/2, getHeight()/2, 22, getHeight()/2);
        g.drawLine(getWidth()/2, getHeight()/2+10, 22, getHeight()/2+10);
        g.drawLine(getWidth()/2, getHeight()/2+20, 22, getHeight()/2+20);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new LinePaintDemo());
                frame.setSize(300,300);
                frame.setVisible(true);
            }
        });
    }
}
This question has already been answered. Start a new discussion instead.