0

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

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Stefano Mtangoo
Featured Replies
  • 1
    Ezzaral 2,714   7 Years Ago

    Those are actually unrelated to the painting example. That code just makes sure that the GUI initialization occurs on the event dispatch thread. You can read more about that here if you are curious: [url]http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html[/url] Read More

0

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.

0

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

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

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.